Revert "initial attempt to integrate surf http client"
This reverts commit 538d52d064.
This commit is contained in:
+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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user