author | Mikael Berthe <mikael@lilotux.net> |
Thu, 13 Apr 2017 20:20:50 +0200 | |
changeset 111 | fc7cd6c90b2e |
parent 107 | f0db7634e540 |
child 120 | 579912e9d0ef |
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 ( |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
4 |
"errors" |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
5 |
"fmt" |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
6 |
|
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
7 |
"github.com/sendgrid/rest" |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
8 |
) |
7 | 9 |
|
10 |
const ( |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
11 |
// 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
|
12 |
GondoleVersion = "0.0" |
12
4f58e9f1e2bc
Add APIEndpoint. Fill in Gondole variable.
Ollivier Robert <roberto@keltia.net>
parents:
7
diff
changeset
|
13 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
14 |
defaultInstanceURL = "https://mastodon.social" |
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
15 |
apiVersion = "v1" // That is not overridable |
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
16 |
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
|
17 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
18 |
// 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
|
19 |
NoRedirect = "urn:ietf:wg:oauth:2.0:oob" |
7 | 20 |
) |
21 |
||
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
22 |
// Error codes |
33
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
23 |
var ( |
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
24 |
ErrAlreadyRegistered = errors.New("app already registered") |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
25 |
ErrEntityNotFound = errors.New("entity not found") |
104
fc338e606d37
Introduce new error message constant
Mikael Berthe <mikael@lilotux.net>
parents:
101
diff
changeset
|
26 |
ErrInvalidParameter = errors.New("incorrect parameter") |
101
16d7500c19dd
Add a generic error identifier
Mikael Berthe <mikael@lilotux.net>
parents:
99
diff
changeset
|
27 |
ErrInvalidID = errors.New("incorrect entity ID") |
33
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
28 |
) |
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
29 |
|
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
30 |
// prepareRequest insert all pre-defined stuff |
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
|
31 |
func (g *Client) prepareRequest(what string) (req rest.Request) { |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
32 |
var endPoint string |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
33 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
34 |
endPoint = g.APIBase + "/" + what |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
35 |
// Add at least one option, the APIkey if present |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
36 |
hdrs := make(map[string]string) |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
37 |
opts := make(map[string]string) |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
38 |
|
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
39 |
// Insert our sig |
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, |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
48 |
QueryParams: opts, |
13 | 49 |
} |
50 |
return |
|
7 | 51 |
} |