-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
103 lines (91 loc) · 2.54 KB
/
api.go
File metadata and controls
103 lines (91 loc) · 2.54 KB
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var NoApiTokenError error = errors.New("error: client object has no api token")
type TinderClient struct {
FbToken string `json:"facebook_token"`
FbId int64 `json:"facebook_id"`
apiToken string
}
type Recommendation struct {
Id string `json:"_id"`
Bio string `json:"bio"`
BirthDate string `json:"birth_date"`
DistanceMi int `json:"distance_mi"`
Gender int `json:"gender"`
Name string `json:"name"`
Photos []struct {
Id string `json:"id"`
Url string `json:"url"`
ProcessedFiles []struct {
Height int `json:"height"`
Width int `json:"width"`
Url string `json:"url"`
} `json:"processedFiles"`
} `json:"photos"`
}
const apiRoot = "https://api.gotinder.com"
// NewTinderClient create new Tinder client.
func NewTinderClient(fbToken string, fbId int64) TinderClient {
return TinderClient{FbToken: fbToken, FbId: fbId}
}
// Connect to Facebook to get a Tinder API key.
func (tc *TinderClient) Connect() error {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(*tc)
res, err := http.Post(fmt.Sprintf("%s/auth", apiRoot), "application/json", buf)
if err != nil {
return err
}
var response struct {
User struct {
ApiToken string `json:"api_token"`
} `json:"user"`
}
json.NewDecoder(res.Body).Decode(&response)
if response.User.ApiToken == "" {
return NoApiTokenError
}
tc.apiToken = response.User.ApiToken
return nil
}
var httpClient http.Client = http.Client{}
// Do an http get with auth headers.
func (tc *TinderClient) httpGet(url string) (*http.Response, error) {
if tc.apiToken == "" {
return nil, NoApiTokenError
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-Auth-Token", tc.apiToken)
return httpClient.Do(req)
}
// GetRecs gets recommendations.
func (tc *TinderClient) GetRecs() ([]Recommendation, error) {
res, err := tc.httpGet(fmt.Sprintf("%s/user/recs", apiRoot))
if err != nil {
return nil, err
}
var response struct {
Results []Recommendation `json:"results"`
}
json.NewDecoder(res.Body).Decode(&response)
return response.Results, nil
}
// SwipeRight swipes a user right.
func (tc *TinderClient) SwipeRight(user *Recommendation) error {
_, err := tc.httpGet(fmt.Sprintf("%s/like/%s", apiRoot, user.Id))
return err
}
// SwipeLeft swipes a user right.
func (tc *TinderClient) SwipeLeft(user *Recommendation) error {
_, err := tc.httpGet(fmt.Sprintf("%s/pass/%s", apiRoot, user.Id))
return err
}