Make http requests with context (#417)
Functional Options dont have a reason for an error (at the moment) fix test & code comment md format add SetSudo always use Context Add Migration Docu Use Functional Options fo NewClient Merge branch 'master' into with-ctx optional exec http requests with context Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/417 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com>
This commit is contained in:
parent
688ee1978e
commit
51d1bddc8a
3 changed files with 77 additions and 11 deletions
|
@ -11,6 +11,7 @@ feel free to create an issue.
|
|||
- [Variable Renames (#386)](#Variable-Renames)
|
||||
- [Change Type of Permission Field (#408)](#Change-Type-of-Permission-Field)
|
||||
- [All Function return http responce (#416)](#All-Function-return-http-responce)
|
||||
- [NewClient has new Option Interface (#417)](#NewClient-has-new-Option-Interface)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
|
@ -74,3 +75,14 @@ If an error ocure before an http request (e.g. gitea is to old), it will be nil.
|
|||
|
||||
Pulls:
|
||||
- [#416 All Function return http responce](https://gitea.com/gitea/go-sdk/pulls/416)
|
||||
|
||||
|
||||
## NewClient has new Option Interface
|
||||
|
||||
function `NewClient` use functional options now.
|
||||
If you simply like to migrate replace `client := NewClient(giteaUrl, token)` with `client, _ := NewClient(giteaURL, SetToken(token))`.
|
||||
|
||||
If you like tu utilize them, currently there are: SetContext, SetBasicAuth, SetOTP, SetToken, SetHTTPClient, SetSudo
|
||||
|
||||
Pulls:
|
||||
- [#417 Make http requests with context](https://gitea.com/gitea/go-sdk/pulls/417)
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -34,6 +35,7 @@ type Client struct {
|
|||
otp string
|
||||
sudo string
|
||||
client *http.Client
|
||||
ctx context.Context
|
||||
serverVersion *version.Version
|
||||
versionLock sync.RWMutex
|
||||
}
|
||||
|
@ -44,43 +46,94 @@ type Response struct {
|
|||
}
|
||||
|
||||
// NewClient initializes and returns a API client.
|
||||
func NewClient(url, token string) *Client {
|
||||
return &Client{
|
||||
url: strings.TrimSuffix(url, "/"),
|
||||
accessToken: token,
|
||||
client: &http.Client{},
|
||||
func NewClient(url string, options ...func(*Client)) (*Client, error) {
|
||||
client := &Client{
|
||||
url: strings.TrimSuffix(url, "/"),
|
||||
client: &http.Client{},
|
||||
ctx: context.Background(),
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(client)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// NewClientWithHTTP creates an API client with a custom http client
|
||||
// Deprecated use SetHTTPClient option
|
||||
func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
|
||||
client := NewClient(url, "")
|
||||
client.client = httpClient
|
||||
client, _ := NewClient(url, SetHTTPClient(httpClient))
|
||||
return client
|
||||
}
|
||||
|
||||
// SetBasicAuth sets basicauth
|
||||
// SetHTTPClient is an option for NewClient to set custom http client
|
||||
func SetHTTPClient(httpClient *http.Client) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.client = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// SetToken is an option for NewClient to set token
|
||||
func SetToken(token string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.accessToken = token
|
||||
}
|
||||
}
|
||||
|
||||
// SetBasicAuth is an option for NewClient to set username and password
|
||||
func SetBasicAuth(username, password string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetBasicAuth(username, password)
|
||||
}
|
||||
}
|
||||
|
||||
// SetBasicAuth sets username and password
|
||||
func (c *Client) SetBasicAuth(username, password string) {
|
||||
c.username, c.password = username, password
|
||||
}
|
||||
|
||||
// SetOTP is an option for NewClient to set OTP for 2FA
|
||||
func SetOTP(otp string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetOTP(otp)
|
||||
}
|
||||
}
|
||||
|
||||
// SetOTP sets OTP for 2FA
|
||||
func (c *Client) SetOTP(otp string) {
|
||||
c.otp = otp
|
||||
}
|
||||
|
||||
// SetContext is an option for NewClient to set context
|
||||
func SetContext(ctx context.Context) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetContext(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// SetContext set context witch is used for http requests
|
||||
func (c *Client) SetContext(ctx context.Context) {
|
||||
c.ctx = ctx
|
||||
}
|
||||
|
||||
// SetHTTPClient replaces default http.Client with user given one.
|
||||
func (c *Client) SetHTTPClient(client *http.Client) {
|
||||
c.client = client
|
||||
}
|
||||
|
||||
// SetSudo is an option for NewClient to set sudo header
|
||||
func SetSudo(sudo string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetSudo(sudo)
|
||||
}
|
||||
}
|
||||
|
||||
// SetSudo sets username to impersonate.
|
||||
func (c *Client) SetSudo(sudo string) {
|
||||
c.sudo = sudo
|
||||
}
|
||||
|
||||
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
|
||||
req, err := http.NewRequest(method, c.url+path, body)
|
||||
req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -95,7 +148,7 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R
|
|||
}
|
||||
|
||||
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
|
||||
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
|
||||
req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -46,7 +46,8 @@ func newTestClient() *Client {
|
|||
client.SetBasicAuth(getGiteaUsername(), getGiteaPassword())
|
||||
return client
|
||||
}
|
||||
return NewClient(getGiteaURL(), getGiteaToken())
|
||||
c, _ := NewClient(getGiteaURL(), SetToken(getGiteaToken()))
|
||||
return c
|
||||
}
|
||||
|
||||
func giteaMasterPath() string {
|
||||
|
|
Loading…
Reference in a new issue