gondole.go
changeset 125 2bbb72b9ebf6
parent 123 9b566c020a17
child 130 c450bb73f59a
equal deleted inserted replaced
124:5ee3f23205af 125:2bbb72b9ebf6
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
       
     5 	"errors"
     4 	"errors"
     6 	"fmt"
       
     7 
       
     8 	"github.com/sendgrid/rest"
       
     9 )
     5 )
    10 
     6 
    11 // apiCallParams is a map with the parameters for an API call
     7 // apiCallParams is a map with the parameters for an API call
    12 type apiCallParams map[string]string
     8 type apiCallParams map[string]string
    13 
     9 
    28 	ErrAlreadyRegistered = errors.New("app already registered")
    24 	ErrAlreadyRegistered = errors.New("app already registered")
    29 	ErrEntityNotFound    = errors.New("entity not found")
    25 	ErrEntityNotFound    = errors.New("entity not found")
    30 	ErrInvalidParameter  = errors.New("incorrect parameter")
    26 	ErrInvalidParameter  = errors.New("incorrect parameter")
    31 	ErrInvalidID         = errors.New("incorrect entity ID")
    27 	ErrInvalidID         = errors.New("incorrect entity ID")
    32 )
    28 )
    33 
       
    34 // prepareRequest inserts all pre-defined stuff
       
    35 func (g *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (req rest.Request) {
       
    36 	endPoint := g.APIBase + "/" + target
       
    37 
       
    38 	// Request headers
       
    39 	hdrs := make(map[string]string)
       
    40 	hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion)
       
    41 	if g.UserToken != nil {
       
    42 		hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.UserToken.AccessToken)
       
    43 	}
       
    44 
       
    45 	req = rest.Request{
       
    46 		BaseURL:     endPoint,
       
    47 		Headers:     hdrs,
       
    48 		Method:      method,
       
    49 		QueryParams: params,
       
    50 	}
       
    51 	return
       
    52 }
       
    53 
       
    54 // apiCall makes a call to the Mastodon API server
       
    55 func (g *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, data interface{}) error {
       
    56 	// Prepare query
       
    57 	req := g.prepareRequest(endPoint, method, params)
       
    58 
       
    59 	// Make API call
       
    60 	r, err := rest.API(req)
       
    61 	if err != nil {
       
    62 		return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error())
       
    63 	}
       
    64 
       
    65 	// Check for error reply
       
    66 	var errorResult Error
       
    67 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
       
    68 		// The empty object is not an error
       
    69 		if errorResult.Text != "" {
       
    70 			return fmt.Errorf("%s", errorResult.Text)
       
    71 		}
       
    72 	}
       
    73 
       
    74 	// Not an error reply; let's unmarshal the data
       
    75 	err = json.Unmarshal([]byte(r.Body), &data)
       
    76 	if err != nil {
       
    77 		return fmt.Errorf("cannot decode API response (%s): %s", method, err.Error())
       
    78 	}
       
    79 	return nil
       
    80 }