-
Notifications
You must be signed in to change notification settings - Fork 1
refator(BREAKING)!: add support to create new requests Client #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4bb30b
feat: allow creating custom clients
Kybxd a2fd835
feat: use context.Background() as default ctx in Options
Kybxd 9997ac5
feat: add test cases
Kybxd 47b23b0
feat: add test cases
Kybxd 23ae247
fix: cr
Kybxd 0a8c7aa
fix: file rename
Kybxd cc3bdd4
fix: cr
Kybxd cbc86e1
feat: add notes
Kybxd a708a3e
refactor: clean code
wenchy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package requests | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestClientOption(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| t.Logf("query strings: %v", r.URL.Query()) | ||
| t.Logf("headers: %v", r.Header) | ||
| time.Sleep(100 * time.Millisecond) | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer testServer.Close() | ||
| type args struct { | ||
| url string | ||
| options []ClientOption | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "with timeout", | ||
| args: args{ | ||
| url: testServer.URL, | ||
| options: []ClientOption{ | ||
| WithTimeout(time.Millisecond), | ||
| }, | ||
| }, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "with transport", | ||
| args: args{ | ||
| url: testServer.URL, | ||
| options: []ClientOption{ | ||
| WithTransport(func() http.RoundTripper { | ||
| transport := http.DefaultTransport.(*http.Transport).Clone() | ||
| transport.DisableKeepAlives = true | ||
| return transport | ||
| }()), | ||
| }, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "with interceptor", | ||
| args: args{ | ||
| url: testServer.URL, | ||
| options: []ClientOption{ | ||
| WithInterceptor(func(ctx context.Context, r *Request, do Do) (*Response, error) { | ||
| t.Logf("method: %s", r.Method) | ||
| return do(ctx, r) | ||
| }), | ||
| }, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cli := NewClient(tt.args.options...) | ||
| got, err := cli.Get(tt.args.url) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if err == nil { | ||
| t.Logf("response body: %+v\n", got.Text()) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package requests | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "sync" | ||
|
|
||
| "github.com/Wenchy/requests/internal/auth/redirector" | ||
| ) | ||
|
|
||
| var ( | ||
| once sync.Once | ||
| defaultClient *Client | ||
| ) | ||
|
|
||
| func newDefaultClient() *Client { | ||
| return &Client{ | ||
| client: &http.Client{ | ||
| CheckRedirect: redirector.RedirectPolicyFunc, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func GetDefaultClient() *Client { | ||
| once.Do(func() { | ||
| defaultClient = newDefaultClient() | ||
| }) | ||
| return defaultClient | ||
| } | ||
|
Comment on lines
15
to
30
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| func InitDefaultClient(options ...ClientOption) { | ||
| client := GetDefaultClient() | ||
| for _, setter := range options { | ||
| setter(client) | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package requests | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| // Do is called by Interceptor to complete HTTP requests. | ||
| type Do func(ctx context.Context, r *Request) (*Response, error) | ||
|
|
||
| // InterceptorFunc provides a hook to intercept the execution of an HTTP request | ||
| // invocation. When an interceptor(s) is set, requests delegates all HTTP | ||
| // client invocations to the interceptor, and it is the responsibility of the | ||
| // interceptor to call do to complete the processing of the HTTP request. | ||
| type InterceptorFunc func(ctx context.Context, r *Request, do Do) (*Response, error) | ||
|
|
||
| // ChainInterceptors chains multiple interceptors into one. | ||
| func ChainInterceptors(interceptors ...InterceptorFunc) InterceptorFunc { | ||
| switch len(interceptors) { | ||
| case 0: | ||
| return nil | ||
| case 1: | ||
| return interceptors[0] | ||
| default: | ||
| return func(ctx context.Context, r *Request, do Do) (*Response, error) { | ||
| return interceptors[0](ctx, r, getChainDo(interceptors, 0, do)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // getChainDo generates the chained do recursively. | ||
| func getChainDo(interceptors []InterceptorFunc, curr int, finalDo Do) Do { | ||
| if curr == len(interceptors)-1 { | ||
| return finalDo | ||
| } | ||
| return func(ctx context.Context, r *Request) (*Response, error) { | ||
| return interceptors[curr+1](ctx, r, getChainDo(interceptors, curr+1, finalDo)) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.