author | Mikael Berthe <mikael@lilotux.net> |
Sat, 15 Apr 2017 10:26:36 +0200 | |
changeset 120 | 579912e9d0ef |
parent 107 | f0db7634e540 |
child 123 | 9b566c020a17 |
permissions | -rw-r--r-- |
7 | 1 |
package gondole |
2 |
||
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
3 |
import ( |
120 | 4 |
"encoding/json" |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
5 |
"errors" |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
6 |
"fmt" |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
7 |
|
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
8 |
"github.com/sendgrid/rest" |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
9 |
) |
7 | 10 |
|
120 | 11 |
// apiCallParams is a map with the parameters for an API call |
12 |
type apiCallParams map[string]string |
|
13 |
||
7 | 14 |
const ( |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
15 |
// GondoleVersion contains the version of the Gondole implementation |
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
16 |
GondoleVersion = "0.0" |
12
4f58e9f1e2bc
Add APIEndpoint. Fill in Gondole variable.
Ollivier Robert <roberto@keltia.net>
parents:
7
diff
changeset
|
17 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
18 |
defaultInstanceURL = "https://mastodon.social" |
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
19 |
apiVersion = "v1" // That is not overridable |
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
20 |
defaultAPIPath = "/api/" + apiVersion |
77
603dd20de7e1
Use site part of the endpoint but fix "/api/v1". Use FallBack if necessary.
Ollivier Robert <roberto@keltia.net>
parents:
67
diff
changeset
|
21 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
22 |
// NoRedirect is the URI for no redirection in the App registration |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
23 |
NoRedirect = "urn:ietf:wg:oauth:2.0:oob" |
7 | 24 |
) |
25 |
||
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
26 |
// Error codes |
33
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
27 |
var ( |
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
28 |
ErrAlreadyRegistered = errors.New("app already registered") |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
29 |
ErrEntityNotFound = errors.New("entity not found") |
104
fc338e606d37
Introduce new error message constant
Mikael Berthe <mikael@lilotux.net>
parents:
101
diff
changeset
|
30 |
ErrInvalidParameter = errors.New("incorrect parameter") |
101
16d7500c19dd
Add a generic error identifier
Mikael Berthe <mikael@lilotux.net>
parents:
99
diff
changeset
|
31 |
ErrInvalidID = errors.New("incorrect entity ID") |
33
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
32 |
) |
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
33 |
|
120 | 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 |
|
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
37 |
|
120 | 38 |
// Request headers |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
39 |
hdrs := make(map[string]string) |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
40 |
hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion) |
87 | 41 |
if g.userToken != nil { |
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
104
diff
changeset
|
42 |
hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.userToken.AccessToken) |
87 | 43 |
} |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
44 |
|
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
45 |
req = rest.Request{ |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
46 |
BaseURL: endPoint, |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
47 |
Headers: hdrs, |
120 | 48 |
Method: method, |
49 |
QueryParams: params, |
|
13 | 50 |
} |
51 |
return |
|
7 | 52 |
} |
120 | 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 |
} |