func statusCodeToErr(resp *Response) (body []byte, err error) {
	if resp.StatusCode/100 == 2 {
		return nil, nil
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("body read on HTTP error %d: %v", resp.StatusCode, err)
	}
	switch resp.StatusCode {
	case 403:
		return data, errors.New("403 Forbidden")
	case 404:
		return data, errors.New("404 Not Found")
	case 409:
		return data, errors.New("409 Conflict")
	case 422:
		return data, fmt.Errorf("422 Unprocessable Entity: %s", string(data))
	}
	path := resp.Request.URL.Path
	method := resp.Request.Method
	header := resp.Request.Header
	errMap := make(map[string]interface{})
	if err = json.Unmarshal(data, &errMap); err != nil {
		return data, fmt.Errorf("Unknown API Error: %d\nRequest: '%s' with '%s' method '%s' header and '%s' body", resp.StatusCode, path, method, header, string(data))
	}
	return data, errors.New(errMap["message"].(string))
}
Converts a response for a HTTP status code indicating an error condition
(non-2XX) to a well-known error value and response body. For non-problematic
(2XX) status codes nil will be returned. Note that on a non-2XX response, the
response body stream will have been read and, hence, is closed on return.
package gitea
import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"github.com/hashicorp/go-version"
)
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
func Version() string {
	return "0.14.0"
}
type Client struct {
	url            string
	accessToken    string
	username       string
	password       string
	otp            string
	sudo           string
	debug          bool
	client         *http.Client
	ctx            context.Context
	mutex          sync.RWMutex
	serverVersion  *version.Version
	getVersionOnce sync.Once
}
type Response struct {
	*http.Response
}
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)
	}
	if err := client.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
		return nil, err
	}
	return client, nil
}
func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
	client, _ := NewClient(url, SetHTTPClient(httpClient))
	return client
}
func SetHTTPClient(httpClient *http.Client) func(client *Client) {
	return func(client *Client) {
		client.SetHTTPClient(httpClient)
	}
}
func (c *Client) SetHTTPClient(client *http.Client) {
	c.mutex.Lock()
	c.client = client
	c.mutex.Unlock()
}
func SetToken(token string) func(client *Client) {
	return func(client *Client) {
		client.mutex.Lock()
		client.accessToken = token
		client.mutex.Unlock()
	}
}
func SetBasicAuth(username, password string) func(client *Client) {
	return func(client *Client) {
		client.SetBasicAuth(username, password)
	}
}
func (c *Client) SetBasicAuth(username, password string) {
	c.mutex.Lock()
	c.username, c.password = username, password
	c.mutex.Unlock()
}
func SetOTP(otp string) func(client *Client) {
	return func(client *Client) {
		client.SetOTP(otp)
	}
}
func (c *Client) SetOTP(otp string) {
	c.mutex.Lock()
	c.otp = otp
	c.mutex.Unlock()
}
func SetContext(ctx context.Context) func(client *Client) {
	return func(client *Client) {
		client.SetContext(ctx)
	}
}
func (c *Client) SetContext(ctx context.Context) {
	c.mutex.Lock()
	c.ctx = ctx
	c.mutex.Unlock()
}
func SetSudo(sudo string) func(client *Client) {
	return func(client *Client) {
		client.SetSudo(sudo)
	}
}
func (c *Client) SetSudo(sudo string) {
	c.mutex.Lock()
	c.sudo = sudo
	c.mutex.Unlock()
}
func SetDebugMode() func(client *Client) {
	return func(client *Client) {
		client.mutex.Lock()
		client.debug = true
		client.mutex.Unlock()
	}
}
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
	c.mutex.RLock()
	debug := c.debug
	if debug {
		fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body)
	}
	req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body)
	client := c.client  
	c.mutex.RUnlock()
	if err != nil {
		return nil, nil, err
	}
	resp, err := client.Do(req)
	if err != nil {
		return nil, nil, err
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if debug {
		fmt.Printf("Response: %v\n\n", resp)
	}
	return data, &Response{resp}, nil
}
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
	c.mutex.RLock()
	debug := c.debug
	if debug {
		fmt.Printf("%s: %s\nHeader: %v\nBody: %s\n", method, c.url+"/api/v1"+path, header, body)
	}
	req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body)
	if err != nil {
		c.mutex.RUnlock()
		return nil, err
	}
	if len(c.accessToken) != 0 {
		req.Header.Set("Authorization", "token "+c.accessToken)
	}
	if len(c.otp) != 0 {
		req.Header.Set("X-GITEA-OTP", c.otp)
	}
	if len(c.username) != 0 {
		req.SetBasicAuth(c.username, c.password)
	}
	if len(c.sudo) != 0 {
		req.Header.Set("Sudo", c.sudo)
	}
	client := c.client  
	c.mutex.RUnlock()
	for k, v := range header {
		req.Header[k] = v
	}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	if debug {
		fmt.Printf("Response: %v\n\n", resp)
	}
	return &Response{resp}, nil
}