-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (88 loc) · 2.99 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/oyvinddd/apple-maps-server-sdk/location"
"github.com/oyvinddd/apple-maps-server-sdk/token"
"log"
"net/http"
"net/url"
)
const (
apiURL string = "https://maps-api.apple.com/v1"
tokenPath string = "/token"
geocodePath string = "/geocode"
reverseGeocodePath string = "/reverseGeocode"
searchPath string = "/search"
)
type AppleMapsSDK interface {
// GenerateAccessToken generates a JWT token for accessing Apple APIs
GenerateAccessToken() (token.AccessToken, error)
// Geocode geocodes the specified address and returns the location (lat/long)
Geocode(address string, countries []string, lang string, searchLocation location.Location) error
// ReverseGeocode returns the address located at a specific location (lat/long)
ReverseGeocode(loc location.Location, lang string) ([]location.Place, error)
}
type appleMapsSDK struct {
httpClient http.Client
tokenManager token.Manager
}
func (sdk appleMapsSDK) GenerateAccessToken() (token.AccessToken, error) {
req, err := buildAuthenticatedRequest(tokenPath, sdk.authorizationToken)
if err != nil {
return token.AccessToken{}, err
}
res, err := sdk.client.Do(req)
if err != nil {
return token.AccessToken{}, err
}
var accessToken token.AccessToken
if err := json.NewDecoder(res.Body).Decode(&accessToken); err != nil {
return token.AccessToken{}, err
}
return accessToken, nil
}
func (sdk appleMapsSDK) Geocode(query string, limitToCountries []string, lang string, searchLocation location.Location) error {
_, err := buildAuthenticatedRequest(http.MethodGet, sdk.accessToken)
if err != nil {
return err
}
return nil
}
func (sdk appleMapsSDK) ReverseGeocode(loc location.Location, lang string) ([]location.Place, error) {
req, err := buildAuthenticatedRequest(reverseGeocodePath, sdk.accessToken)
req.URL.RawQuery = url.Values{
"loc": {loc.String()},
"lang": {lang}, // default = en-US
}.Encode()
res, err := sdk.client.Do(req)
if err != nil {
return nil, err
}
var places []location.Place
if err := json.NewDecoder(res.Body).Decode(&places); err != nil {
fmt.Print(res.StatusCode)
return nil, err
}
return places, nil
}
func NewWithToken(token string) AppleMapsSDK {
return &appleMapsSDK{token, "", http.Client{}}
}
func (sdk appleMapsSDK) buildAuthenticatedRequest(path string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", apiURL, path), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return req, nil
}
func main() {
jwtToken := "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlpHSk5IWTQ4N1MifQ.eyJpc3MiOiIyU01GTE02NlI5IiwiaWF0IjoxNjg2MTM5MzkzLCJleHAiOjE2ODg3MzExODh9.NhMY58eABMdHw366XCX5dlH2nWFUjqJ20Pye7UTk3gy9ADH3eFhGBvJAIue3SCdKkPOPfqBYjitFIM4V67ES0g"
appleMapsSDK := NewWithToken(jwtToken)
places, err := appleMapsSDK.ReverseGeocode(location.New(60.0, 5.0), "en-US")
if err != nil {
log.Fatal(err)
}
fmt.Println(places)
}