-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.go
197 lines (155 loc) · 3.51 KB
/
params.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package nasa
import (
"fmt"
"net/url"
"strconv"
"time"
)
// ParamEncoder is the interface passed to most API methods.
type ParamEncoder interface {
Encode() (string, error)
}
// APIParam is used when only an APIKey is needed.
type APIParam struct {
APIKey string
}
// Encode returns a string representation for the given API type.
func (p *APIParam) Encode() (string, error) {
v := url.Values{}
if p.APIKey == "" {
return "", ErrorNoAPIKey
}
v.Set("api_key", p.APIKey)
return v.Encode(), nil
}
// APODParams wraps the APOD API params.
type APODParams struct {
APIKey string
Date time.Time
HD bool
}
// Encode returns a string representation for the given API type.
func (p *APODParams) Encode() (string, error) {
v := url.Values{}
if p.APIKey == "" {
return "", ErrorNoAPIKey
}
v.Set("api_key", p.APIKey)
if !p.Date.IsZero() {
v.Set("date", p.Date.Format("2006-01-02"))
}
if p.HD {
v.Set("hd", "true")
}
return v.Encode(), nil
}
// EPICParams wraps the EPIC API params.
type EPICParams struct {
APIKey string
Date time.Time
}
// Encode returns a string representation for the given API type.
func (p *EPICParams) Encode() (string, error) {
if p.APIKey == "" {
return "", ErrorNoAPIKey
}
val := "api/natural"
if !p.Date.IsZero() {
val += fmt.Sprintf("/date/%s", p.Date.Format("2006-01-02"))
}
val += fmt.Sprintf("?api_key=%s", p.APIKey)
return val, nil
}
// MarsPhotosParams wraps the Mars Photos API params.
type MarsPhotosParams struct {
APIKey string
Sol int
EarthDate time.Time
Camera RoverCamera
Page int
}
// Encode returns a string representation for the given API type.
func (p *MarsPhotosParams) Encode() (string, error) {
v := url.Values{}
if p.APIKey == "" {
return "", ErrorNoAPIKey
}
v.Set("api_key", p.APIKey)
if !p.EarthDate.IsZero() {
v.Set("earth_date", p.EarthDate.Format("2006-01-02"))
} else {
v.Set("sol", strconv.Itoa(p.Sol))
}
if p.Camera.Slug != "" {
v.Set("camera", p.Camera.Slug)
}
if p.Page > 0 {
v.Set("page", strconv.Itoa(p.Page))
}
return v.Encode(), nil
}
// MediaParams wraps the Image and Video Library (media) params.
type MediaParams struct {
Query string
Center string
Description string
Description508 string
Keywords string
Location string
MediaType string
NasaID string
Page int
Photographer string
SecondaryCreator string
Title string
YearStart string
YearEnd string
}
// Encode returns a string representation for the given API type.
func (p *MediaParams) Encode() (string, error) {
v := url.Values{}
if p.Query == "" {
return "", ErrorNoQuery
}
v.Set("q", p.Query)
if p.Center != "" {
v.Set("center", p.Center)
}
if p.Description != "" {
v.Set("description", p.Description)
}
if p.Description508 != "" {
v.Set("description_508", p.Description508)
}
if p.Keywords != "" {
v.Set("keywords", p.Keywords)
}
if p.Location != "" {
v.Set("location", p.Location)
}
if p.MediaType != "" {
v.Set("media_type", p.MediaType)
}
if p.NasaID != "" {
v.Set("nasa_id", p.NasaID)
}
if p.Page > 0 {
v.Set("page", strconv.Itoa(p.Page))
}
if p.Photographer != "" {
v.Set("photographer", p.Photographer)
}
if p.SecondaryCreator != "" {
v.Set("secondary_creator", p.SecondaryCreator)
}
if p.Title != "" {
v.Set("title", p.Title)
}
if p.YearStart != "" {
v.Set("year_start", p.YearStart)
}
if p.YearEnd != "" {
v.Set("year_end", p.YearEnd)
}
return v.Encode(), nil
}