author | Mikael Berthe <mikael@lilotux.net> |
Wed, 12 Apr 2017 16:40:02 +0200 | |
changeset 83 | adc39ae774c0 |
parent 77 | 603dd20de7e1 |
child 87 | 1b4653f07bfc |
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 |
||
33
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
22 |
var ( |
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
23 |
ErrAlreadyRegistered = errors.New("App already registered") |
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
24 |
) |
bce8b17415ae
Add new error ErrAlreadyRegistered.
Ollivier Robert <roberto@keltia.net>
parents:
26
diff
changeset
|
25 |
|
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
26 |
// 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
|
27 |
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
|
28 |
var endPoint string |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
29 |
|
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
30 |
endPoint = g.APIBase + "/" + what |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
31 |
// 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
|
32 |
hdrs := make(map[string]string) |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
33 |
opts := make(map[string]string) |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
34 |
|
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
35 |
// Insert our sig |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
36 |
hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion) |
67
1ff7afce37fe
Add a baseURL parameter to NewApp().
Ollivier Robert <roberto@keltia.net>
parents:
43
diff
changeset
|
37 |
hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.Secret) |
21
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 |
req = rest.Request{ |
83
adc39ae774c0
Make it work with non-defaut instances
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
40 |
BaseURL: endPoint, |
21
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
41 |
Headers: hdrs, |
555716349f64
Move App stuff into app.go.
Ollivier Robert <roberto@keltia.net>
parents:
13
diff
changeset
|
42 |
QueryParams: opts, |
13 | 43 |
} |
44 |
return |
|
7 | 45 |
} |