Documentation
¶
Index ¶
- Constants
- func DeserializeJSON(resp *http.Response, target any) error
- func InterceptRequestBody(r *http.Request) ([]byte, error)
- func InterceptResponseBody(r *http.Response) ([]byte, error)
- func MustInterceptRequestBody(r *http.Request) []byte
- func MustInterceptResponseBody(r *http.Response) []byte
- func NewRequest(ctx context.Context, method string, rawURL string, body io.Reader, ...) (*http.Request, error)
- type BaseError
- type Client
- func (c *Client) BaseURL() string
- func (c *Client) Delete(ctx context.Context, url string, parameters ...RequestParameter) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, url string, parameters ...RequestParameter) (*http.Response, error)
- func (c *Client) Head(ctx context.Context, url string, parameters ...RequestParameter) (*http.Response, error)
- func (c *Client) Patch(ctx context.Context, url string, body io.Reader, ...) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, url string, body io.Reader, ...) (*http.Response, error)
- func (c *Client) WithBaseTransport(base http.RoundTripper) *Client
- func (c *Client) WithBaseURL(baseURL string) (*Client, error)
- func (c *Client) WithDefaultHeaders(headers map[string]string) *Client
- func (c *Client) WithJSONContentType() *Client
- func (c *Client) WithTimeout(timeout time.Duration) *Client
- type ErrorTag
- type ErrorTagCollection
- type RequestParameter
- type RequestParameters
Examples ¶
Constants ¶
const DefaultTimeout = 30 * time.Second
Variables ¶
This section is empty.
Functions ¶
func DeserializeJSON ¶
DeserializeJSON unmarshals the response body payload to the object referenced by the `target` pointer. If `target` is not a pointer, an error is returned. The body stream is restored as a NopCloser, so subsequent calls to `Body.Close()` will never fail. Note that the above behavior may have impact on memory requirements since memory must be reserved for the full lifecycle of the http.Response object.
func InterceptResponseBody ¶
InterceptResponseBody will read the full contents of the http.Response.Body stream and release any resources associated with the Response object while allowing the Body stream to be accessed again. The Body stream is restored as a NopCloser, so subsequent calls to `Body.Close()` will never fail. Note that the above behavior may have impact on memory requirements since memory must be reserved for the full lifecycle of the http.Response object.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewWithTransport ¶
func NewWithTransport(transport http.RoundTripper) *Client
NewWithTransport creates a new Client object that uses the given http.Roundtripper as a transport in the underlying net/http Client.
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, url string, parameters ...RequestParameter) (*http.Response, error)
Example ¶
package main
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
)
func main() {
sdk := NewSDK()
user, err := sdk.GetUserByUsername(context.Background(), "georgepsarakis")
panicOnError(err)
m, err := json.MarshalIndent(user, "", " ")
panicOnError(err)
fmt.Println(string(m))
}
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
}
Output: { "id": 963304, "bio": "Software Engineer", "blog": "https://controlflow.substack.com/", "created_at": "2011-08-06T16:57:12Z", "login": "georgepsarakis", "name": "George Psarakis" }
func (*Client) Head ¶
func (c *Client) Head(ctx context.Context, url string, parameters ...RequestParameter) (*http.Response, error)
Head sends a HEAD Request.
func (*Client) WithBaseTransport ¶
func (c *Client) WithBaseTransport(base http.RoundTripper) *Client
func (*Client) WithDefaultHeaders ¶
WithDefaultHeaders adds the given name-value pairs as request headers on every Request. Headers can be added or overridden using the WithHeaders functional option parameter on a per-request basis.
func (*Client) WithJSONContentType ¶
WithJSONContentType sets the Content-Type default header to `application/json; charset=utf-8`.
type ErrorTagCollection ¶
type ErrorTagCollection []ErrorTag
func (ErrorTagCollection) String ¶
func (c ErrorTagCollection) String(delimiter string) string
type RequestParameter ¶
type RequestParameter func(opts *RequestParameters)
func WithHeaders ¶
func WithHeaders(headers map[string]string) RequestParameter
WithHeaders allows headers to be set on the request. Multiple calls using the same header name will overwrite existing header values.
func WithQueryParameters ¶
func WithQueryParameters(params map[string]string) RequestParameter
WithQueryParameters configures the given name-value pairs as Query String parameters for the request. Multiple calls will override values for existing keys.
type RequestParameters ¶
type RequestParameters struct {
// contains filtered or unexported fields
}
func NewRequestParameters ¶
func NewRequestParameters(opts ...RequestParameter) *RequestParameters
func (*RequestParameters) ErrorCodes ¶
func (rp *RequestParameters) ErrorCodes() []int
func (*RequestParameters) Headers ¶
func (rp *RequestParameters) Headers() http.Header
func (*RequestParameters) QueryParameters ¶
func (rp *RequestParameters) QueryParameters() url.Values
QueryParameters returns a clone of the currently configured query parameters. Multiple calls will override already existing keys.