api.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 10 May 2017 20:12:26 +0200
changeset 179 fbe21b4aabda
parent 169 d84b2b83813d
child 207 301d5b94be3f
permissions -rw-r--r--
Version 1.5.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     2
Copyright 2017 Ollivier Robert
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     3
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     4
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     5
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     6
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     7
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     8
package madon
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
import (
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"bytes"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"encoding/json"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"fmt"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"net/http"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"net/url"
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    16
	"regexp"
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    17
	"strconv"
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	"strings"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    20
	"github.com/pkg/errors"
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	"github.com/sendgrid/rest"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    24
type apiLinks struct {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    25
	next, prev *LimitParams
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    26
}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    27
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    28
func parseLink(links []string) (*apiLinks, error) {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    29
	if len(links) == 0 {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    30
		return nil, nil
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    31
	}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    32
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    33
	al := new(apiLinks)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    34
	linkRegex := regexp.MustCompile(`<([^>]+)>; rel="([^"]+)`)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    35
	for _, l := range links {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    36
		m := linkRegex.FindAllStringSubmatch(l, -1)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    37
		for _, submatch := range m {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    38
			if len(submatch) != 3 {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    39
				continue
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    40
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    41
			// Parse URL
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    42
			u, err := url.Parse(submatch[1])
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    43
			if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    44
				return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    45
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    46
			var lp *LimitParams
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    47
			since := u.Query().Get("since_id")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    48
			max := u.Query().Get("max_id")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    49
			lim := u.Query().Get("limit")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    50
			if since == "" && max == "" {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    51
				continue
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    52
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    53
			lp = new(LimitParams)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    54
			if since != "" {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    55
				lp.SinceID, err = strconv.ParseInt(since, 10, 64)
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    56
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    57
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    58
				}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    59
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    60
			if max != "" {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    61
				lp.MaxID, err = strconv.ParseInt(max, 10, 64)
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    62
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    63
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    64
				}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    65
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    66
			if lim != "" {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    67
				lp.Limit, err = strconv.Atoi(lim)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    68
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    69
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    70
				}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    71
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    72
			switch submatch[2] {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    73
			case "prev":
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    74
				al.prev = lp
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    75
			case "next":
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    76
				al.next = lp
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    77
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    78
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    79
	}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    80
	return al, nil
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    81
}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    82
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
// restAPI actually does the HTTP query
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
// It is a copy of rest.API with better handling of parameters with multiple values
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
func restAPI(request rest.Request) (*rest.Response, error) {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	c := &rest.Client{HTTPClient: http.DefaultClient}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	// Build the HTTP request object.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
	if len(request.QueryParams) != 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		// Add parameters to the URL
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
		request.BaseURL += "?"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
		urlp := url.Values{}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
		for key, value := range request.QueryParams {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
			// It seems Mastodon doesn't like parameters with index
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
			// numbers, but it needs the brackets.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
			// Let's check if the key matches '^.+\[.*\]$'
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
			klen := len(key)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
			if klen == 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
				continue
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
			}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
			i := strings.Index(key, "[")
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
			if key[klen-1] == ']' && i > 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
				// This is an array, let's remove the index number
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
				key = key[:i] + "[]"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
			}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
			urlp.Add(key, value)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
		urlpstr := urlp.Encode()
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
		request.BaseURL += urlpstr
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
	req, err := http.NewRequest(string(request.Method), request.BaseURL, bytes.NewBuffer(request.Body))
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		return nil, err
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
	for key, value := range request.Headers {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
		req.Header.Set(key, value)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
	_, exists := req.Header["Content-Type"]
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
	if len(request.Body) > 0 && !exists {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
		req.Header.Set("Content-Type", "application/json")
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
	// Build the HTTP client and make the request.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	res, err := c.MakeRequest(req)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
		return nil, err
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
	}
169
d84b2b83813d Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents: 162
diff changeset
   130
	if res.StatusCode < 200 || res.StatusCode >= 300 {
d84b2b83813d Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents: 162
diff changeset
   131
		return nil, errors.Errorf("bad server status code (%d): %s",
d84b2b83813d Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents: 162
diff changeset
   132
			res.StatusCode, http.StatusText(res.StatusCode))
d84b2b83813d Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents: 162
diff changeset
   133
	}
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
	// Build Response object.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	response, err := rest.BuildResponse(res)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
		return nil, err
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
	return response, nil
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
// prepareRequest inserts all pre-defined stuff
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   145
func (mc *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (rest.Request, error) {
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   146
	var req rest.Request
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   147
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   148
	if mc == nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   149
		return req, ErrUninitializedClient
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   150
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   151
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   152
	endPoint := mc.APIBase + "/" + target
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
	// Request headers
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
	hdrs := make(map[string]string)
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   156
	hdrs["User-Agent"] = fmt.Sprintf("madon/%s", MadonVersion)
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   157
	if mc.UserToken != nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   158
		hdrs["Authorization"] = fmt.Sprintf("Bearer %s", mc.UserToken.AccessToken)
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
	req = rest.Request{
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
		BaseURL:     endPoint,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
		Headers:     hdrs,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
		Method:      method,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
		QueryParams: params,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
	}
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   167
	return req, nil
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   168
}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
// apiCall makes a call to the Mastodon API server
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   171
// If links is not nil, the prev/next links from the API response headers
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   172
// will be set (if they exist) in the structure.
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   173
func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, links *apiLinks, data interface{}) error {
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   174
	if mc == nil {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
   175
		return errors.New("use of uninitialized madon client")
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   176
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   177
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   178
	if limitOptions != nil {
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   179
		if params == nil {
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   180
			params = make(apiCallParams)
150
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   181
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   182
		if limitOptions.Limit > 0 {
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   183
			params["limit"] = strconv.Itoa(limitOptions.Limit)
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   184
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   185
		if limitOptions.SinceID > 0 {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   186
			params["since_id"] = strconv.FormatInt(limitOptions.SinceID, 10)
150
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   187
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   188
		if limitOptions.MaxID > 0 {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   189
			params["max_id"] = strconv.FormatInt(limitOptions.MaxID, 10)
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   190
		}
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   191
	}
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   192
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   193
	// Prepare query
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   194
	req, err := mc.prepareRequest(endPoint, method, params)
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   195
	if err != nil {
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   196
		return err
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   197
	}
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   199
	// Make API call
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   200
	r, err := restAPI(req)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   201
	if err != nil {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
   202
		return errors.Wrapf(err, "API query (%s) failed", endPoint)
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   203
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   204
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   205
	if links != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   206
		pLinks, err := parseLink(r.Headers["Link"])
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   207
		if err != nil {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
   208
			return errors.Wrapf(err, "cannot decode header links (%s)", method)
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   209
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   210
		if pLinks != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   211
			*links = *pLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   212
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   213
	}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   214
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   215
	// Check for error reply
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   216
	var errorResult Error
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   217
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   218
		// The empty object is not an error
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   219
		if errorResult.Text != "" {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
   220
			return errors.New(errorResult.Text)
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   221
		}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   222
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   223
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   224
	// Not an error reply; let's unmarshal the data
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   225
	err = json.Unmarshal([]byte(r.Body), &data)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   226
	if err != nil {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
   227
		return errors.Wrapf(err, "cannot decode API response (%s)", method)
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   228
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   229
	return nil
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   230
}