github.com/daeMOn63/bitclient@v0.0.0-20190425080230-bfee94efac35/requests.go (about)

     1  package bitclient
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  const BASE_URI = "/rest/api/1.0"
     9  
    10  type PagedRequest struct {
    11  	Limit uint `url:"limit,omitempty"`
    12  	Start uint `url:"start,omitempty"`
    13  }
    14  
    15  type RequestError struct {
    16  	Code    int
    17  	Message string
    18  }
    19  
    20  type ErrorResponse struct {
    21  	Errors []Error
    22  }
    23  
    24  type PagedResponse struct {
    25  	Size       uint `json:"size"`
    26  	Limit      uint `json:"limit"`
    27  	IsLastPage bool `json:"isLastPage"`
    28  	Start      uint `json:"start"`
    29  }
    30  
    31  func (r RequestError) Error() string {
    32  	return r.Message
    33  }
    34  
    35  func (bc *BitClient) checkReponse(resp *http.Response, errorResponse *ErrorResponse) (*http.Response, error) {
    36  
    37  	if resp != nil && resp.StatusCode > 299 {
    38  
    39  		message := fmt.Sprintf("%s - %s\n", resp.Status, resp.Request.URL.String())
    40  		for _, e := range errorResponse.Errors {
    41  			message += e.Context + ": " + e.Message + "\n"
    42  		}
    43  
    44  		return nil, RequestError{
    45  			Code:    resp.StatusCode,
    46  			Message: message,
    47  		}
    48  	}
    49  	return resp, nil
    50  }
    51  
    52  func (bc *BitClient) DoGet(uri string, params interface{}, rData interface{}) (*http.Response, error) {
    53  
    54  	rError := new(ErrorResponse)
    55  
    56  	resp, _ := bc.sling.New().Get(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
    57  
    58  	return bc.checkReponse(resp, rError)
    59  }
    60  
    61  func (bc *BitClient) DoPostUrl(uri string, params interface{}, rData interface{}) (*http.Response, error) {
    62  
    63  	rError := new(ErrorResponse)
    64  
    65  	resp, _ := bc.sling.New().Post(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
    66  
    67  	return bc.checkReponse(resp, rError)
    68  }
    69  
    70  func (bc *BitClient) DoPost(uri string, data interface{}, rData interface{}) (*http.Response, error) {
    71  
    72  	rError := new(ErrorResponse)
    73  
    74  	resp, _ := bc.sling.New().Post(BASE_URI+uri).BodyJSON(data).Receive(rData, rError)
    75  
    76  	return bc.checkReponse(resp, rError)
    77  }
    78  
    79  func (bc *BitClient) DoPut(uri string, data interface{}, rData interface{}) (*http.Response, error) {
    80  
    81  	rError := new(ErrorResponse)
    82  
    83  	resp, _ := bc.sling.New().Put(BASE_URI+uri).BodyJSON(data).Receive(rData, rError)
    84  
    85  	return bc.checkReponse(resp, rError)
    86  }
    87  
    88  func (bc *BitClient) DoPutUrl(uri string, data interface{}, rData interface{}) (*http.Response, error) {
    89  
    90  	rError := new(ErrorResponse)
    91  
    92  	resp, _ := bc.sling.New().Put(BASE_URI+uri).QueryStruct(data).Receive(rData, rError)
    93  
    94  	return bc.checkReponse(resp, rError)
    95  }
    96  
    97  func (bc *BitClient) DoDeleteUrl(uri string, params interface{}, rData interface{}) (*http.Response, error) {
    98  
    99  	rError := new(ErrorResponse)
   100  
   101  	resp, _ := bc.sling.New().Delete(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
   102  
   103  	return bc.checkReponse(resp, rError)
   104  }