2020-10-14 09:36:00 +05:30
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
2021-03-22 22:33:18 +05:30
"net/url"
2020-10-14 09:36:00 +05:30
"strings"
"sync"
"github.com/hashicorp/go-version"
)
var jsonHeader = http . Header { "content-type" : [ ] string { "application/json" } }
// Version return the library version
func Version ( ) string {
2022-01-05 10:21:12 +05:30
return "0.15.1"
2020-10-14 09:36:00 +05:30
}
2021-03-22 22:33:18 +05:30
// Client represents a thread-safe Gitea API client.
2020-10-14 09:36:00 +05:30
type Client struct {
2022-01-05 10:21:12 +05:30
url string
accessToken string
username string
password string
otp string
sudo string
debug bool
client * http . Client
ctx context . Context
mutex sync . RWMutex
2021-03-22 22:33:18 +05:30
serverVersion * version . Version
getVersionOnce sync . Once
2022-01-05 10:21:12 +05:30
ignoreVersion bool // only set by SetGiteaVersion so don't need a mutex lock
2020-10-14 09:36:00 +05:30
}
// Response represents the gitea response
type Response struct {
* http . Response
}
2022-01-05 10:21:12 +05:30
// ClientOption are functions used to init a new client
type ClientOption func ( * Client ) error
2020-10-14 09:36:00 +05:30
// NewClient initializes and returns a API client.
2021-03-22 22:33:18 +05:30
// Usage of all gitea.Client methods is concurrency-safe.
2022-01-05 10:21:12 +05:30
func NewClient ( url string , options ... ClientOption ) ( * Client , error ) {
2020-10-14 09:36:00 +05:30
client := & Client {
url : strings . TrimSuffix ( url , "/" ) ,
client : & http . Client { } ,
ctx : context . Background ( ) ,
}
for _ , opt := range options {
2022-01-05 10:21:12 +05:30
if err := opt ( client ) ; err != nil {
return nil , err
}
2020-10-14 09:36:00 +05:30
}
2021-03-22 22:33:18 +05:30
if err := client . checkServerVersionGreaterThanOrEqual ( version1_11_0 ) ; err != nil {
2020-10-14 09:36:00 +05:30
return nil , err
}
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 , SetHTTPClient ( httpClient ) )
return client
}
// SetHTTPClient is an option for NewClient to set custom http client
2022-01-05 10:21:12 +05:30
func SetHTTPClient ( httpClient * http . Client ) ClientOption {
return func ( client * Client ) error {
2021-03-22 22:33:18 +05:30
client . SetHTTPClient ( httpClient )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
2021-03-22 22:33:18 +05:30
// SetHTTPClient replaces default http.Client with user given one.
func ( c * Client ) SetHTTPClient ( client * http . Client ) {
c . mutex . Lock ( )
c . client = client
c . mutex . Unlock ( )
}
2020-10-14 09:36:00 +05:30
// SetToken is an option for NewClient to set token
2022-01-05 10:21:12 +05:30
func SetToken ( token string ) ClientOption {
return func ( client * Client ) error {
2021-03-22 22:33:18 +05:30
client . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
client . accessToken = token
2021-03-22 22:33:18 +05:30
client . mutex . Unlock ( )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
// SetBasicAuth is an option for NewClient to set username and password
2022-01-05 10:21:12 +05:30
func SetBasicAuth ( username , password string ) ClientOption {
return func ( client * Client ) error {
2020-10-14 09:36:00 +05:30
client . SetBasicAuth ( username , password )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
// SetBasicAuth sets username and password
func ( c * Client ) SetBasicAuth ( username , password string ) {
2021-03-22 22:33:18 +05:30
c . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
c . username , c . password = username , password
2021-03-22 22:33:18 +05:30
c . mutex . Unlock ( )
2020-10-14 09:36:00 +05:30
}
// SetOTP is an option for NewClient to set OTP for 2FA
2022-01-05 10:21:12 +05:30
func SetOTP ( otp string ) ClientOption {
return func ( client * Client ) error {
2020-10-14 09:36:00 +05:30
client . SetOTP ( otp )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
// SetOTP sets OTP for 2FA
func ( c * Client ) SetOTP ( otp string ) {
2021-03-22 22:33:18 +05:30
c . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
c . otp = otp
2021-03-22 22:33:18 +05:30
c . mutex . Unlock ( )
2020-10-14 09:36:00 +05:30
}
2022-01-05 10:21:12 +05:30
// SetContext is an option for NewClient to set the default context
func SetContext ( ctx context . Context ) ClientOption {
return func ( client * Client ) error {
2020-10-14 09:36:00 +05:30
client . SetContext ( ctx )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
2022-01-05 10:21:12 +05:30
// SetContext set default context witch is used for http requests
2020-10-14 09:36:00 +05:30
func ( c * Client ) SetContext ( ctx context . Context ) {
2021-03-22 22:33:18 +05:30
c . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
c . ctx = ctx
2021-03-22 22:33:18 +05:30
c . mutex . Unlock ( )
2020-10-14 09:36:00 +05:30
}
// SetSudo is an option for NewClient to set sudo header
2022-01-05 10:21:12 +05:30
func SetSudo ( sudo string ) ClientOption {
return func ( client * Client ) error {
2020-10-14 09:36:00 +05:30
client . SetSudo ( sudo )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
// SetSudo sets username to impersonate.
func ( c * Client ) SetSudo ( sudo string ) {
2021-03-22 22:33:18 +05:30
c . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
c . sudo = sudo
2021-03-22 22:33:18 +05:30
c . mutex . Unlock ( )
2020-10-14 09:36:00 +05:30
}
// SetDebugMode is an option for NewClient to enable debug mode
2022-01-05 10:21:12 +05:30
func SetDebugMode ( ) ClientOption {
return func ( client * Client ) error {
2021-03-22 22:33:18 +05:30
client . mutex . Lock ( )
2020-10-14 09:36:00 +05:30
client . debug = true
2021-03-22 22:33:18 +05:30
client . mutex . Unlock ( )
2022-01-05 10:21:12 +05:30
return nil
2020-10-14 09:36:00 +05:30
}
}
func ( c * Client ) getWebResponse ( method , path string , body io . Reader ) ( [ ] byte , * Response , error ) {
2021-03-22 22:33:18 +05:30
c . mutex . RLock ( )
debug := c . debug
if debug {
2020-10-14 09:36:00 +05:30
fmt . Printf ( "%s: %s\nBody: %v\n" , method , c . url + path , body )
}
req , err := http . NewRequestWithContext ( c . ctx , method , c . url + path , body )
2021-03-22 22:33:18 +05:30
client := c . client // client ref can change from this point on so safe it
c . mutex . RUnlock ( )
2020-10-14 09:36:00 +05:30
if err != nil {
return nil , nil , err
}
2021-03-22 22:33:18 +05:30
resp , err := client . Do ( req )
2020-10-14 09:36:00 +05:30
if err != nil {
return nil , nil , err
}
defer resp . Body . Close ( )
data , err := ioutil . ReadAll ( resp . Body )
2021-03-22 22:33:18 +05:30
if debug {
2020-10-14 09:36:00 +05:30
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 ) {
2021-03-22 22:33:18 +05:30
c . mutex . RLock ( )
debug := c . debug
if debug {
2020-10-14 09:36:00 +05:30
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 {
2021-03-22 22:33:18 +05:30
c . mutex . RUnlock ( )
2020-10-14 09:36:00 +05:30
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 )
}
2021-03-22 22:33:18 +05:30
client := c . client // client ref can change from this point on so safe it
c . mutex . RUnlock ( )
2020-10-14 09:36:00 +05:30
for k , v := range header {
req . Header [ k ] = v
}
2021-03-22 22:33:18 +05:30
resp , err := client . Do ( req )
2020-10-14 09:36:00 +05:30
if err != nil {
return nil , err
}
2021-03-22 22:33:18 +05:30
if debug {
2020-10-14 09:36:00 +05:30
fmt . Printf ( "Response: %v\n\n" , resp )
}
return & Response { resp } , nil
}
2021-03-22 22:33:18 +05:30
// 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.
func statusCodeToErr ( resp * Response ) ( body [ ] byte , err error ) {
// no error
if resp . StatusCode / 100 == 2 {
return nil , nil
2020-10-14 09:36:00 +05:30
}
2021-03-22 22:33:18 +05:30
//
// error: body will be read for details
//
defer resp . Body . Close ( )
2020-10-14 09:36:00 +05:30
data , err := ioutil . ReadAll ( resp . Body )
if err != nil {
2021-03-22 22:33:18 +05:30
return nil , fmt . Errorf ( "body read on HTTP error %d: %v" , resp . StatusCode , err )
2020-10-14 09:36:00 +05:30
}
switch resp . StatusCode {
case 403 :
2021-03-22 22:33:18 +05:30
return data , errors . New ( "403 Forbidden" )
2020-10-14 09:36:00 +05:30
case 404 :
2021-03-22 22:33:18 +05:30
return data , errors . New ( "404 Not Found" )
2020-10-14 09:36:00 +05:30
case 409 :
2021-03-22 22:33:18 +05:30
return data , errors . New ( "409 Conflict" )
2020-10-14 09:36:00 +05:30
case 422 :
2021-03-22 22:33:18 +05:30
return data , fmt . Errorf ( "422 Unprocessable Entity: %s" , string ( data ) )
2020-10-14 09:36:00 +05:30
}
2021-03-22 22:33:18 +05:30
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 {
// when the JSON can't be parsed, data was probably empty or a
// plain string, so we try to return a helpful error anyway
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 ) )
}
func ( c * Client ) getResponse ( method , path string , header http . Header , body io . Reader ) ( [ ] byte , * Response , error ) {
resp , err := c . doRequest ( method , path , header , body )
if err != nil {
return nil , nil , err
}
defer resp . Body . Close ( )
// check for errors
data , err := statusCodeToErr ( resp )
if err != nil {
return data , resp , err
}
// success (2XX), read body
data , err = ioutil . ReadAll ( resp . Body )
if err != nil {
return nil , resp , err
2020-10-14 09:36:00 +05:30
}
return data , resp , nil
}
func ( c * Client ) getParsedResponse ( method , path string , header http . Header , body io . Reader , obj interface { } ) ( * Response , error ) {
data , resp , err := c . getResponse ( method , path , header , body )
if err != nil {
return resp , err
}
return resp , json . Unmarshal ( data , obj )
}
func ( c * Client ) getStatusCode ( method , path string , header http . Header , body io . Reader ) ( int , * Response , error ) {
resp , err := c . doRequest ( method , path , header , body )
if err != nil {
return - 1 , resp , err
}
defer resp . Body . Close ( )
return resp . StatusCode , resp , nil
}
2021-03-22 22:33:18 +05:30
// pathEscapeSegments escapes segments of a path while not escaping forward slash
func pathEscapeSegments ( path string ) string {
slice := strings . Split ( path , "/" )
for index := range slice {
slice [ index ] = url . PathEscape ( slice [ index ] )
}
escapedPath := strings . Join ( slice , "/" )
return escapedPath
}
// escapeValidatePathSegments is a help function to validate and encode url path segments
func escapeValidatePathSegments ( seg ... * string ) error {
for i := range seg {
if seg [ i ] == nil || len ( * seg [ i ] ) == 0 {
return fmt . Errorf ( "path segment [%d] is empty" , i )
}
* seg [ i ] = url . PathEscape ( * seg [ i ] )
}
return nil
}