-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow request building outside of httpclient.Client
Requests can now be built independently, while taking advantage of the functional option toolkit, without requiring use of the Client API. - Properly clone query parameters in `RequestParameters.QueryParameters`. - Documentation enhancements. - Add a top-level example.
- Loading branch information
1 parent
3f63c0b
commit 4c6857e
Showing
8 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 httpclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
func ExampleClient_Get() { | ||
sdk := NewSDK() | ||
user, err := sdk.GetUserByUsername(context.Background(), "georgepsarakis") | ||
panicOnError(err) | ||
|
||
m, err := json.MarshalIndent(user, "", " ") | ||
panicOnError(err) | ||
|
||
fmt.Println(string(m)) | ||
// Output: | ||
//{ | ||
// "id": 963304, | ||
// "bio": "Software Engineer", | ||
// "blog": "https://controlflow.substack.com/", | ||
// "created_at": "2011-08-06T16:57:12Z", | ||
// "login": "georgepsarakis", | ||
// "name": "George Psarakis" | ||
//} | ||
} | ||
|
||
func panicOnError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type GitHubSDK struct { | ||
Client *Client | ||
} | ||
|
||
func NewSDK() GitHubSDK { | ||
return NewSDKWithClient(New()) | ||
} | ||
|
||
func NewSDKWithClient(c *Client) GitHubSDK { | ||
c.WithDefaultHeaders(map[string]string{ | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
"Accept": "application/vnd.github+json", | ||
}) | ||
c, _ = c.WithBaseURL("https://api.github.com") | ||
return GitHubSDK{Client: c} | ||
} | ||
|
||
type User struct { | ||
ID int `json:"id"` | ||
Bio string `json:"bio"` | ||
Blog string `json:"blog"` | ||
CreatedAt time.Time `json:"created_at"` | ||
Login string `json:"login"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// GetUserByUsername retrieves a user based on their public username. | ||
// See https://docs.github.com/en/rest/users/users | ||
func (g GitHubSDK) GetUserByUsername(ctx context.Context, username string) (User, error) { | ||
path, err := url.JoinPath("/users", username) | ||
if err != nil { | ||
return User{}, err | ||
} | ||
resp, err := g.Client.Get(ctx, path) | ||
if err != nil { | ||
return User{}, err | ||
} | ||
u := User{} | ||
if err := DeserializeJSON(resp, &u); err != nil { | ||
return u, err | ||
} | ||
return u, nil | ||
} |
This file contains 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 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 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 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 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