Revert "initial attempt to integrate surf http client"
This reverts commit 538d52d064.
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
+5
-8
@@ -6,15 +6,16 @@ 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
|
||||
@@ -241,18 +242,14 @@ func (s *EmojiService) Upload(ctx context.Context, subreddit string, createReque
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, uploadURL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err = s.client.Do(ctx, req, nil)
|
||||
httpResponse, err := ctxhttp.Post(ctx, nil, uploadURL, writer.FormDataContentType(), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = CheckResponse(resp.Response)
|
||||
err = CheckResponse(httpResponse)
|
||||
if err != nil {
|
||||
return newResponse(resp.Response), err
|
||||
return newResponse(httpResponse), err
|
||||
}
|
||||
|
||||
return s.upload(ctx, subreddit, createRequest, fields["key"])
|
||||
|
||||
@@ -3,9 +3,9 @@ package reddit
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package reddit
|
||||
|
||||
import (
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -33,10 +33,9 @@ to https://www.reddit.com/api/v1/access_token with the following form values:
|
||||
|
||||
package reddit
|
||||
|
||||
/*
|
||||
import (
|
||||
"context"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
@@ -76,4 +75,3 @@ func oauthTransport(client *Client) http.RoundTripper {
|
||||
Base: client.client.Transport,
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package reddit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
@@ -8,6 +10,17 @@ 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>)
|
||||
|
||||
@@ -2,6 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -9,6 +10,14 @@ 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,6 +1,6 @@
|
||||
package reddit
|
||||
|
||||
import "github.com/enetx/http"
|
||||
import "net/http"
|
||||
|
||||
// cloneRequest returns a clone of the provided *http.Request.
|
||||
// The clone is a shallow copy of the struct and its Header map,
|
||||
|
||||
+22
-63
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -14,9 +15,6 @@ 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"
|
||||
)
|
||||
@@ -62,7 +60,7 @@ type Credentials struct {
|
||||
// Client manages communication with the Reddit API.
|
||||
type Client struct {
|
||||
// HTTP client used to communicate with the Reddit API.
|
||||
client *surf.Client
|
||||
client *http.Client
|
||||
|
||||
BaseURL *url.URL
|
||||
TokenURL *url.URL
|
||||
@@ -112,15 +110,7 @@ func newClient() *Client {
|
||||
baseURL, _ := url.Parse(defaultBaseURL)
|
||||
tokenURL, _ := url.Parse(defaultTokenURL)
|
||||
|
||||
surf_client := surf.NewClient().
|
||||
Builder().
|
||||
Impersonate().
|
||||
RandomOS().
|
||||
Firefox().
|
||||
Build().
|
||||
Unwrap()
|
||||
|
||||
client := &Client{client: surf_client, BaseURL: baseURL, TokenURL: tokenURL}
|
||||
client := &Client{client: &http.Client{}, BaseURL: baseURL, TokenURL: tokenURL}
|
||||
|
||||
client.Account = &AccountService{client: client}
|
||||
client.Collection = &CollectionService{client: client}
|
||||
@@ -163,20 +153,18 @@ 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
|
||||
}
|
||||
@@ -194,17 +182,15 @@ 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
|
||||
}
|
||||
@@ -348,33 +334,6 @@ 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.
|
||||
@@ -386,7 +345,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
|
||||
}, err
|
||||
}
|
||||
|
||||
resp, err := c.request(ctx, req)
|
||||
resp, err := DoRequestWithClient(ctx, c.client, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -16,7 +16,6 @@ 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()) {
|
||||
|
||||
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
||||
+1
-1
@@ -6,9 +6,9 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -3,9 +3,9 @@ package reddit
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// WidgetService handles communication with the widget
|
||||
|
||||
@@ -3,7 +3,7 @@ package reddit
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ package reddit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enetx/http"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
Reference in New Issue
Block a user