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)
|
- [Variable Renames (#386)](#Variable-Renames)
|
||||||
- [Change Type of Permission Field (#408)](#Change-Type-of-Permission-Field)
|
- [Change Type of Permission Field (#408)](#Change-Type-of-Permission-Field)
|
||||||
- [All Function return http responce (#416)](#All-Function-return-http-responce)
|
- [All Function return http responce (#416)](#All-Function-return-http-responce)
|
||||||
|
- [NewClient has new Option Interface (#417)](#NewClient-has-new-Option-Interface)
|
||||||
|
|
||||||
<!-- tocstop -->
|
<!-- tocstop -->
|
||||||
|
|
||||||
|
@ -74,3 +75,14 @@ If an error ocure before an http request (e.g. gitea is to old), it will be nil.
|
||||||
|
|
||||||
Pulls:
|
Pulls:
|
||||||
- [#416 All Function return http responce](https://gitea.com/gitea/go-sdk/pulls/416)
|
- [#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
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -34,6 +35,7 @@ type Client struct {
|
||||||
otp string
|
otp string
|
||||||
sudo string
|
sudo string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
ctx context.Context
|
||||||
serverVersion *version.Version
|
serverVersion *version.Version
|
||||||
versionLock sync.RWMutex
|
versionLock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -44,43 +46,94 @@ type Response struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient initializes and returns a API client.
|
// NewClient initializes and returns a API client.
|
||||||
func NewClient(url, token string) *Client {
|
func NewClient(url string, options ...func(*Client)) (*Client, error) {
|
||||||
return &Client{
|
client := &Client{
|
||||||
url: strings.TrimSuffix(url, "/"),
|
url: strings.TrimSuffix(url, "/"),
|
||||||
accessToken: token,
|
|
||||||
client: &http.Client{},
|
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
|
// NewClientWithHTTP creates an API client with a custom http client
|
||||||
|
// Deprecated use SetHTTPClient option
|
||||||
func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
|
func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
|
||||||
client := NewClient(url, "")
|
client, _ := NewClient(url, SetHTTPClient(httpClient))
|
||||||
client.client = httpClient
|
|
||||||
return client
|
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) {
|
func (c *Client) SetBasicAuth(username, password string) {
|
||||||
c.username, c.password = username, password
|
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
|
// SetOTP sets OTP for 2FA
|
||||||
func (c *Client) SetOTP(otp string) {
|
func (c *Client) SetOTP(otp string) {
|
||||||
c.otp = otp
|
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.
|
// SetHTTPClient replaces default http.Client with user given one.
|
||||||
func (c *Client) SetHTTPClient(client *http.Client) {
|
func (c *Client) SetHTTPClient(client *http.Client) {
|
||||||
c.client = 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.
|
// SetSudo sets username to impersonate.
|
||||||
func (c *Client) SetSudo(sudo string) {
|
func (c *Client) SetSudo(sudo string) {
|
||||||
c.sudo = sudo
|
c.sudo = sudo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,8 @@ func newTestClient() *Client {
|
||||||
client.SetBasicAuth(getGiteaUsername(), getGiteaPassword())
|
client.SetBasicAuth(getGiteaUsername(), getGiteaPassword())
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
return NewClient(getGiteaURL(), getGiteaToken())
|
c, _ := NewClient(getGiteaURL(), SetToken(getGiteaToken()))
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func giteaMasterPath() string {
|
func giteaMasterPath() string {
|
||||||
|
|
Loading…
Reference in a new issue