-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomadlist.go
119 lines (95 loc) · 2.18 KB
/
nomadlist.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
package nomadlist
import (
"encoding/json"
"fmt"
"net/http"
)
func (c *Client) Profile(username string) (*Profile, error) {
url := fmt.Sprintf("https://nomadlist.com/@%s.json", username)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var profile Profile
if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
return nil, err
}
return &profile, nil
}
func (c *Client) Stats(username string) (*Stats, error) {
profile, err := c.Profile(username)
if err != nil {
return nil, err
}
return &profile.Stats, nil
}
func (c *Client) Location(username string) (*Location, error) {
profile, err := c.Profile(username)
if err != nil {
return nil, err
}
return &profile.Location, nil
}
func (c *Client) Trips(username string) ([]Trip, error) {
profile, err := c.Profile(username)
if err != nil {
return nil, err
}
return profile.Trips, nil
}
func (c *Client) Trip(username, tripID string) (*Trip, error) {
trips, err := c.Trips(username)
if err != nil {
return nil, err
}
for _, trip := range trips {
if trip.TripID == tripID {
return &trip, nil
}
}
return nil, fmt.Errorf("trip with id %s not found", tripID)
}
func (c *Client) TripsInYear(username, year string) ([]Trip, error) {
trips, err := c.Trips(username)
if err != nil {
return nil, err
}
var tripsInYear []Trip
for _, trip := range trips {
if trip.DateStart[:4] == year {
tripsInYear = append(tripsInYear, trip)
}
}
return tripsInYear, nil
}
func (c *Client) TripsInCountry(username, country string) ([]Trip, error) {
trips, err := c.Trips(username)
if err != nil {
return nil, err
}
var tripsInCountry []Trip
for _, trip := range trips {
if trip.Country == country {
tripsInCountry = append(tripsInCountry, trip)
}
}
return tripsInCountry, nil
}
func (c *Client) TripsInCity(username, city string) ([]Trip, error) {
trips, err := c.Trips(username)
if err != nil {
return nil, err
}
var tripsInCity []Trip
for _, trip := range trips {
if trip.Place == city {
tripsInCity = append(tripsInCity, trip)
}
}
return tripsInCity, nil
}