api.go
changeset 128 a5a00fad7a32
parent 125 2bbb72b9ebf6
child 130 c450bb73f59a
equal deleted inserted replaced
127:96a7f2432d27 128:a5a00fad7a32
    67 
    67 
    68 	return response, nil
    68 	return response, nil
    69 }
    69 }
    70 
    70 
    71 // prepareRequest inserts all pre-defined stuff
    71 // prepareRequest inserts all pre-defined stuff
    72 func (g *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (req rest.Request) {
    72 func (g *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (rest.Request, error) {
       
    73 	var req rest.Request
       
    74 
       
    75 	if g == nil {
       
    76 		return req, fmt.Errorf("use of uninitialized gondole client")
       
    77 	}
       
    78 
    73 	endPoint := g.APIBase + "/" + target
    79 	endPoint := g.APIBase + "/" + target
    74 
    80 
    75 	// Request headers
    81 	// Request headers
    76 	hdrs := make(map[string]string)
    82 	hdrs := make(map[string]string)
    77 	hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion)
    83 	hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion)
    83 		BaseURL:     endPoint,
    89 		BaseURL:     endPoint,
    84 		Headers:     hdrs,
    90 		Headers:     hdrs,
    85 		Method:      method,
    91 		Method:      method,
    86 		QueryParams: params,
    92 		QueryParams: params,
    87 	}
    93 	}
    88 	return
    94 	return req, nil
    89 }
    95 }
    90 
    96 
    91 // apiCall makes a call to the Mastodon API server
    97 // apiCall makes a call to the Mastodon API server
    92 func (g *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, data interface{}) error {
    98 func (g *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, data interface{}) error {
       
    99 	if g == nil {
       
   100 		return fmt.Errorf("use of uninitialized gondole client")
       
   101 	}
       
   102 
    93 	// Prepare query
   103 	// Prepare query
    94 	req := g.prepareRequest(endPoint, method, params)
   104 	req, err := g.prepareRequest(endPoint, method, params)
       
   105 	if err != nil {
       
   106 		return err
       
   107 	}
    95 
   108 
    96 	// Make API call
   109 	// Make API call
    97 	r, err := restAPI(req)
   110 	r, err := restAPI(req)
    98 	if err != nil {
   111 	if err != nil {
    99 		return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error())
   112 		return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error())