This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
/
example_test.go
85 lines (72 loc) · 2.32 KB
/
example_test.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
package uber_test
import (
"fmt"
uber "github.com/chrisenytc/uber"
)
var (
client *uber.Client
)
// In order to use the Uber API you must register an application at
// https://developer.uber.com/. In turn you will receive a `client_id`, `secret`, and
// `server_token`.
func ExampleClient() {
client = uber.NewClient("your server_token")
}
// All requests require a valid server_token. Requests that require latitude or longitude
// as arguments are float64's (and should be valid coordinates).
func ExampleClient_requests() {
products, err := client.GetProducts(37.7759792, -122.41823)
if err != nil {
fmt.Println(err)
} else {
for _, product := range products {
fmt.Println(*product)
}
}
prices, err := client.GetPrices(41.827896, -71.393034, 41.826025, -71.406892)
if err != nil {
fmt.Println(err)
} else {
for _, price := range prices {
fmt.Println(*price)
}
}
}
// Uber's OAuth 2.0 flow requires the user go to URL they provide. You can generate this
// URL programatically by calling `client.OAuth`. After the user goes to URL and grants
// your application permissions, you need to figure out a way for the user to input the
// second argument of the URL to which they are redirected (ie: the `code`
// argument`AuthCode` in `RedirectUrl/?state=go-uber&code=AuthCode`). You then need to set
// the access token with `client.SetAccessToken`.
func ExampleClient_auth() {
url, err := client.OAuth(
"your client_id", "your client_secret", "your redirect_url", "profile",
// "profile" is justthe scope. You may also pass another argument "history" (or no
// scope arguments).
)
if err != nil {
fmt.Printf("OAuth error: %+v\n", err)
return
}
fmt.Printf("Please go to %+v to authorize this app.\n", url)
client.SetAccessToken("AuthCode")
}
// Alternatively, you can automate the whole thing with the `client.AutoAuth`. This opens
// up a browser for the user.
func ExampleClient_autoAuth() {
err := client.AutOAuth(
"your client_id", "your client_secret", "your redirect_url", "profile",
)
if err != nil {
fmt.Printf("OAuth error: %+v\n", err)
}
}
// After authorizing, you can call the scoped endpoints to which your app has access.
func ExampleClient_userProfile() {
profile, err := client.GetUserProfile()
if err != nil {
fmt.Printf("GetUserProfile error: %+v\n", err)
return
}
fmt.Println(profile)
}