This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
121 lines (100 loc) · 5.35 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2012 Brian Hetro <[email protected]>
// Use of this source code is governed by the ISC
// license that can be found in the LICENSE file.
package adn
import (
"fmt"
"text/template"
)
const (
apiHost = "https://alpha-api.app.net"
apiAuthHost = "https://alpha.app.net"
)
type httpMethod string
const (
httpGet httpMethod = "GET"
httpPost = "POST"
httpDelete = "DELETE"
)
type endpoint struct {
Method httpMethod
Path string
Options *epOptions
}
var ApiEndpoints = map[string]endpoint{
// Users
"retrieve user": {httpGet, apiHost + "/stream/0/users/{{.User}}", nil},
"follow user": {httpPost, apiHost + "/stream/0/users/{{.User}}/follow", nil},
"unfollow user": {httpDelete, apiHost + "/stream/0/users/{{.User}}/follow", nil},
"list followed users": {httpGet, apiHost + "/stream/0/users/{{.User}}/following", nil},
"list following users": {httpGet, apiHost + "/stream/0/users/{{.User}}/followers", nil},
"mute user": {httpPost, apiHost + "/stream/0/users/{{.User}}/mute", nil},
"unmute user": {httpDelete, apiHost + "/stream/0/users/{{.User}}/mute", nil},
"list muted users": {httpGet, apiHost + "/stream/0/users/me/muted", nil},
"search for users": {httpGet, apiHost + "/stream/0/users/search", nil},
"list reposters": {httpGet, apiHost + "/stream/0/posts/{{.Post}}/reposters", nil},
"list starrers": {httpGet, apiHost + "/stream/0/posts/{{.Post}}/star", nil},
// Tokens
"check current token": {httpGet, apiHost + "/stream/0/token", nil},
// Posts
"create post": {httpPost, apiHost + "/stream/0/posts", nil},
"retrieve post": {httpGet, apiHost + "/stream/0/posts/{{.Post}}", nil},
"delete post": {httpDelete, apiHost + "/stream/0/posts/{{.Post}}", nil},
"retrieve post replies": {httpGet, apiHost + "/stream/0/posts/{{.Post}}/replies", nil},
"retrieve user posts": {httpGet, apiHost + "/stream/0/users/{{.User}}/posts", nil},
"repost post": {httpPost, apiHost + "/stream/0/posts/{{.Post}}/repost", nil},
"unrepost post": {httpDelete, apiHost + "/stream/0/posts/{{.Post}}/repost", nil},
"star post": {httpPost, apiHost + "/stream/0/posts/{{.Post}}/star", nil},
"unstar post": {httpDelete, apiHost + "/stream/0/posts/{{.Post}}/star", nil},
"retrieve user starred posts": {httpGet, apiHost + "/stream/0/users/{{.User}}/stars", nil},
"retrieve posts mentioning user": {httpGet, apiHost + "/stream/0/users/{{.User}}/mentions", nil},
"retrieve user personalized stream": {httpGet, apiHost + "/stream/0/posts/stream", nil},
"retrieve global stream": {httpGet, apiHost + "/stream/0/posts/stream/global", nil},
"retrieve tagged posts": {httpGet, apiHost + "/stream/0/posts/tag/{{.Hashtag}}", nil},
// Streams
"retrieve realtime user personalized stream": {httpGet, apiHost + "/stream/0/streams/user", nil},
"retrieve realtime multiple user personalized stream": {httpGet, apiHost + "/stream/0/streams/app", nil},
"retrieve realtime public stream": {httpGet, apiHost + "/stream/0/streams/public", nil},
"retrieve stream status": {httpGet, apiHost + "/stream/0/streams/{{.Stream}}", nil},
"control stream": {httpPost, apiHost + "/stream/0/streams/{{.Stream}}", nil},
// Real-time updates
"list subscriptions": {httpGet, apiHost + "/stream/0/subscriptions", nil},
"create subscription": {httpPost, apiHost + "/stream/0/subscriptions", nil},
"delete subscription": {httpDelete, apiHost + "/stream/0/subscriptions/{{.Subscription}}", nil},
"delete all subscriptions": {httpDelete, apiHost + "/stream/0/subscriptions", nil},
// Filters
"retrieve current user filters": {httpGet, apiHost + "/stream/0/filters", nil},
"create filter": {httpPost, apiHost + "/stream/0/filters", nil},
"retrieve filter": {httpGet, apiHost + "/stream/0/filters/{{.Filter}}", nil},
"delete filter": {httpGet, apiHost + "/stream/0/filters/{{.Filter}}", nil},
// Authentication (Server-side flow)
"authentication url": {httpGet, apiAuthHost + "/oauth/authenticate?client_id={{urlquery .Id}}&response_type=code&redirect_uri={{urlquery .RedirectURI}}&scope={{urlquery .Scopes.Spaced}}{{if .State}}&state={{urlquery .State}}{{end}}", nil},
"get access token": {httpPost, apiAuthHost + "/oauth/access_token", &epOptions{ResponseEnvelope: false}},
}
// These fields are used to fill the endpoint template.
type EpArgs struct {
User, Post, Hashtag, Stream, Subscription, Filter string
}
type epOptions struct {
ResponseEnvelope bool // Do we expect a response envelope?
}
var epTemplates = new(template.Template)
func init() {
for k, v := range ApiEndpoints {
template.Must(epTemplates.New(k).Parse(v.Path))
}
}
type responseEnvelope struct {
Data interface{} `json:"data"`
Meta responseMeta `json:"meta"`
}
type responseMeta struct {
Code int `json:"code"`
ErrorId string `json:"error_id"`
ErrorMessage string `json:"error_message"`
}
// Represents errors returned by ADN as a result of an API call.
type APIError responseMeta
func (e APIError) Error() string {
return fmt.Sprintf("%d: %s (%s)", e.Code, e.ErrorMessage, e.ErrorId)
}