initial attempt to integrate surf http client

This commit is contained in:
2026-01-24 14:59:48 -06:00
parent 2f1019d170
commit 538d52d064
45 changed files with 196 additions and 109 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"context"
"net/http"
"github.com/enetx/http"
"net/url"
)
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"context"
"errors"
"net/http"
"github.com/enetx/http"
"net/url"
"strings"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"context"
"errors"
"net/http"
"github.com/enetx/http"
"net/url"
"strings"
)
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+8 -5
View File
@@ -6,16 +6,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/enetx/http"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/google/go-querystring/query"
"golang.org/x/net/context/ctxhttp"
)
// EmojiService handles communication with the emoji
@@ -242,14 +241,18 @@ func (s *EmojiService) Upload(ctx context.Context, subreddit string, createReque
return nil, err
}
httpResponse, err := ctxhttp.Post(ctx, nil, uploadURL, writer.FormDataContentType(), body)
req, err := http.NewRequest(http.MethodPost, uploadURL, body)
if err != nil {
return nil, err
}
resp, err = s.client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
err = CheckResponse(httpResponse)
err = CheckResponse(resp.Response)
if err != nil {
return newResponse(httpResponse), err
return newResponse(resp.Response), err
}
return s.upload(ctx, subreddit, createRequest, fields["key"])
+1 -1
View File
@@ -3,9 +3,9 @@ package reddit
import (
"bytes"
"fmt"
"github.com/enetx/http"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"strings"
"time"
)
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"encoding/csv"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"github.com/google/go-querystring/query"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"strconv"
)
+1 -1
View File
@@ -1,7 +1,7 @@
package reddit
import (
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/enetx/http"
"io"
"net/http"
"net/url"
"reflect"
"strings"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"net/http"
"github.com/enetx/http"
"net/url"
"strings"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"context"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"reflect"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"github.com/google/go-querystring/query"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"context"
"net/http"
"github.com/enetx/http"
"net/url"
"strconv"
)
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"strings"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+3 -1
View File
@@ -33,9 +33,10 @@ to https://www.reddit.com/api/v1/access_token with the following form values:
package reddit
/*
import (
"context"
"net/http"
"github.com/enetx/http"
"golang.org/x/oauth2"
)
@@ -75,3 +76,4 @@ func oauthTransport(client *Client) http.RoundTripper {
Base: client.client.Transport,
}
}
*/
-13
View File
@@ -1,8 +1,6 @@
package reddit
import (
"errors"
"net/http"
"net/url"
"os"
)
@@ -10,17 +8,6 @@ import (
// Opt is used to further configure a client upon initialization.
type Opt func(*Client) error
// WithHTTPClient sets the HTTP client which will be used to make requests.
func WithHTTPClient(httpClient *http.Client) Opt {
return func(c *Client) error {
if httpClient == nil {
return errors.New("*http.Client: cannot be nil")
}
c.client = httpClient
return nil
}
}
// WithUserAgent sets the User-Agent header for requests made with the client.
// Reddit recommends the following format for the user agent:
// <platform>:<app ID>:<version string> (by /u/<reddit username>)
-9
View File
@@ -2,7 +2,6 @@ package reddit
import (
"fmt"
"net/http"
"net/url"
"os"
"testing"
@@ -10,14 +9,6 @@ import (
"github.com/stretchr/testify/require"
)
func TestWithHTTPClient(t *testing.T) {
_, err := NewClient(Credentials{}, WithHTTPClient(nil))
require.EqualError(t, err, "*http.Client: cannot be nil")
_, err = NewClient(Credentials{}, WithHTTPClient(&http.Client{}))
require.NoError(t, err)
}
func TestWithUserAgent(t *testing.T) {
c, err := NewClient(Credentials{}, WithUserAgent("test"))
require.NoError(t, err)
+1 -1
View File
@@ -1,6 +1,6 @@
package reddit
import "net/http"
import "github.com/enetx/http"
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map,
+63 -22
View File
@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
@@ -15,6 +14,9 @@ import (
"sync"
"time"
"github.com/enetx/g"
"github.com/enetx/http"
"github.com/enetx/surf"
"github.com/google/go-querystring/query"
"golang.org/x/oauth2"
)
@@ -60,7 +62,7 @@ type Credentials struct {
// Client manages communication with the Reddit API.
type Client struct {
// HTTP client used to communicate with the Reddit API.
client *http.Client
client *surf.Client
BaseURL *url.URL
TokenURL *url.URL
@@ -110,7 +112,15 @@ func newClient() *Client {
baseURL, _ := url.Parse(defaultBaseURL)
tokenURL, _ := url.Parse(defaultTokenURL)
client := &Client{client: &http.Client{}, BaseURL: baseURL, TokenURL: tokenURL}
surf_client := surf.NewClient().
Builder().
Impersonate().
RandomOS().
Firefox().
Build().
Unwrap()
client := &Client{client: surf_client, BaseURL: baseURL, TokenURL: tokenURL}
client.Account = &AccountService{client: client}
client.Collection = &CollectionService{client: client}
@@ -153,18 +163,20 @@ func NewClient(credentials Credentials, opts ...Opt) (*Client, error) {
}
}
userAgentTransport := &userAgentTransport{
userAgent: client.UserAgent(),
Base: client.client.Transport,
}
client.client.Transport = userAgentTransport
/*
userAgentTransport := &userAgentTransport{
userAgent: client.UserAgent(),
Base: client.client.Transport,
}
client.client.Transport = userAgentTransport
if client.client.CheckRedirect == nil {
client.client.CheckRedirect = client.redirect
}
if client.client.CheckRedirect == nil {
client.client.CheckRedirect = client.redirect
}
oauthTransport := oauthTransport(client)
client.client.Transport = oauthTransport
oauthTransport := oauthTransport(client)
client.client.Transport = oauthTransport
*/
return client, nil
}
@@ -182,15 +194,17 @@ func NewReadonlyClient(opts ...Opt) (*Client, error) {
}
}
if client.client == nil {
client.client = &http.Client{}
}
/*
if client.client == nil {
client.client = &http.Client{}
}
userAgentTransport := &userAgentTransport{
userAgent: client.UserAgent(),
Base: client.client.Transport,
}
client.client.Transport = userAgentTransport
userAgentTransport := &userAgentTransport{
userAgent: client.UserAgent(),
Base: client.client.Transport,
}
client.client.Transport = userAgentTransport
*/
return client, nil
}
@@ -334,6 +348,33 @@ func parseRate(r *http.Response) Rate {
return rate
}
func (c *Client) request(ctx context.Context, req *http.Request) (*http.Response, error) {
switch req.Method {
case http.MethodGet:
resp, err := c.client.
Get(g.String(req.URL.String())).
WithContext(ctx).
Do().
Result()
if err != nil {
return nil, err
}
return resp.GetResponse(), nil
case http.MethodPost:
resp, err := c.client.
Post(g.String(req.URL.String()), nil).
WithContext(ctx).
Do().
Result()
if err != nil {
return nil, err
}
return resp.GetResponse(), nil
default:
return nil, fmt.Errorf("fork does not support %s http method right now", req.Method)
}
}
// Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value
// pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface,
// the raw response will be written to v, without attempting to decode it.
@@ -345,7 +386,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
}, err
}
resp, err := DoRequestWithClient(ctx, c.client, req)
resp, err := c.request(ctx, req)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -4,9 +4,9 @@ import (
"context"
"errors"
"fmt"
"github.com/enetx/http"
"github.com/enetx/http/httptest"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
+1
View File
@@ -16,6 +16,7 @@ type StreamService struct {
// - a channel into which new posts will be sent
// - a channel into which any errors will be sent
// - a function that the client can call once to stop the streaming and close the channels
//
// Because of the 100 post limit imposed by Reddit when fetching posts, some high-traffic
// streams might drop submissions between API requests, such as when streaming r/all.
func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post, <-chan error, func()) {
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"testing"
"time"
+1 -1
View File
@@ -6,9 +6,9 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/enetx/http"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
+1 -1
View File
@@ -3,9 +3,9 @@ package reddit
import (
"bytes"
"fmt"
"github.com/enetx/http"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"context"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
)
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
)
// WidgetService handles communication with the widget
+1 -1
View File
@@ -3,7 +3,7 @@ package reddit
import (
"encoding/json"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"strings"
+1 -1
View File
@@ -2,7 +2,7 @@ package reddit
import (
"fmt"
"net/http"
"github.com/enetx/http"
"net/url"
"testing"
"time"