api.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 20:43:17 +0200
changeset 159 408aa794d9bb
parent 155 0c581e0108da
child 162 68df3a01e1a7
permissions -rw-r--r--
s/int/int64/ for IDs and time integers
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
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	"github.com/sendgrid/rest"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    23
type apiLinks struct {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    24
	next, prev *LimitParams
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    25
}
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
func parseLink(links []string) (*apiLinks, error) {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    28
	if len(links) == 0 {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    29
		return nil, nil
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    30
	}
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
	al := new(apiLinks)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    33
	linkRegex := regexp.MustCompile(`<([^>]+)>; rel="([^"]+)`)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    34
	for _, l := range links {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    35
		m := linkRegex.FindAllStringSubmatch(l, -1)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    36
		for _, submatch := range m {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    37
			if len(submatch) != 3 {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    38
				continue
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    39
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    40
			// Parse URL
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    41
			u, err := url.Parse(submatch[1])
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    42
			if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    43
				return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    44
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    45
			var lp *LimitParams
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    46
			since := u.Query().Get("since_id")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    47
			max := u.Query().Get("max_id")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    48
			lim := u.Query().Get("limit")
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    49
			if since == "" && max == "" {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    50
				continue
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    51
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    52
			lp = new(LimitParams)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    53
			if since != "" {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    54
				lp.SinceID, err = strconv.ParseInt(since, 10, 64)
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    55
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    56
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    57
				}
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
			if max != "" {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    60
				lp.MaxID, err = strconv.ParseInt(max, 10, 64)
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    61
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    62
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    63
				}
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
			if lim != "" {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    66
				lp.Limit, err = strconv.Atoi(lim)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    67
				if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    68
					return al, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    69
				}
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
			switch submatch[2] {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    72
			case "prev":
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    73
				al.prev = lp
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    74
			case "next":
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    75
				al.next = lp
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    76
			}
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
	return al, nil
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    80
}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
    81
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
// restAPI actually does the HTTP query
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
// 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
    84
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
    85
	c := &rest.Client{HTTPClient: http.DefaultClient}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
	// Build the HTTP request object.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	if len(request.QueryParams) != 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
		// Add parameters to the URL
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		request.BaseURL += "?"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
		urlp := url.Values{}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
		for key, value := range request.QueryParams {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
			// 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
    94
			// numbers, but it needs the brackets.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
			// 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
    96
			klen := len(key)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
			if klen == 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
				continue
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
			}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
			i := strings.Index(key, "[")
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
			if key[klen-1] == ']' && i > 0 {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
				// 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
   103
				key = key[:i] + "[]"
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
			}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
			urlp.Add(key, value)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		urlpstr := urlp.Encode()
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
		request.BaseURL += urlpstr
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
	}
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
	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
   112
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
		return nil, err
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
	}
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
	for key, value := range request.Headers {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
		req.Header.Set(key, value)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
	_, exists := req.Header["Content-Type"]
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
	if len(request.Body) > 0 && !exists {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
		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
   122
	}
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
	// 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
   125
	res, err := c.MakeRequest(req)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		return nil, err
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
	// Build Response object.
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
	response, err := rest.BuildResponse(res)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
		return nil, err
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
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	return response, nil
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
// prepareRequest inserts all pre-defined stuff
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   140
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
   141
	var req rest.Request
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   142
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   143
	if mc == nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   144
		return req, ErrUninitializedClient
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   145
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   146
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   147
	endPoint := mc.APIBase + "/" + target
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
	// Request headers
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
	hdrs := make(map[string]string)
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   151
	hdrs["User-Agent"] = fmt.Sprintf("madon/%s", MadonVersion)
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   152
	if mc.UserToken != nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   153
		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
   154
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
	req = rest.Request{
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
		BaseURL:     endPoint,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
		Headers:     hdrs,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
		Method:      method,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
		QueryParams: params,
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
	}
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   162
	return req, nil
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
// apiCall makes a call to the Mastodon API server
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   166
// 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
   167
// will be set (if they exist) in the structure.
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   168
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
   169
	if mc == nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   170
		return fmt.Errorf("use of uninitialized madon client")
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   171
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   172
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   173
	if limitOptions != nil {
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   174
		if params == nil {
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   175
			params = make(apiCallParams)
150
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   176
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   177
		if limitOptions.Limit > 0 {
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   178
			params["limit"] = strconv.Itoa(limitOptions.Limit)
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   179
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   180
		if limitOptions.SinceID > 0 {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   181
			params["since_id"] = strconv.FormatInt(limitOptions.SinceID, 10)
150
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   182
		}
cd328b30af77 Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   183
		if limitOptions.MaxID > 0 {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   184
			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
   185
		}
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   186
	}
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
   187
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
	// Prepare query
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   189
	req, err := mc.prepareRequest(endPoint, method, params)
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   190
	if err != nil {
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   191
		return err
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   192
	}
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   193
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   194
	// Make API call
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   195
	r, err := restAPI(req)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
		return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error())
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
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   200
	if links != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   201
		pLinks, err := parseLink(r.Headers["Link"])
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   202
		if err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   203
			return fmt.Errorf("cannot decode header links (%s): %s", method, err.Error())
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   204
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   205
		if pLinks != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   206
			*links = *pLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   207
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   208
	}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 150
diff changeset
   209
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   210
	// Check for error reply
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   211
	var errorResult Error
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   212
	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
   213
		// 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
   214
		if errorResult.Text != "" {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   215
			return fmt.Errorf("%s", errorResult.Text)
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   216
		}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   217
	}
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   218
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   219
	// 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
   220
	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
   221
	if err != nil {
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   222
		return fmt.Errorf("cannot decode API response (%s): %s", method, err.Error())
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
	return nil
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   225
}