Skip to content

Commit

Permalink
products: implement ListProducts, ProductByID
Browse files Browse the repository at this point in the history
Fixes #38.

a) Implements ListProducts, which given a place (lat, lon) lists
available products
b) Implements ProductByID
c) Cleans up authentication problems that I noticed while testing
out a bunch of client methods. Added "Content-Type": "application/json"
headers for PUT and POST.

Exhibits:
* ProductByID:
```go
func Example_client_ProductByID() {
        client, err :=
uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
        if err != nil {
                log.Fatal(err)
        }

        product, err :=
client.ProductByID("bc300c14-c30d-4d3f-afcb-19b240c16a13")
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("The Product information: %#v\n", product)
}
```

Giving
```shell
The Product information: &uber.Product{UpfrontFareEnabled:true,
Capacity:2, ID:"bc300c14-c30d-4d3f-afcb-19b240c16a13",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-uberpool-2.png",
CashEnabled:false, Shared:true, ShortDescription:"uberPOOL",
DisplayName:"POOL", Description:"Share the ride, share the cost"}
```

* ListProducts:
```go
func Example_client_ListProducts() {
        client, err :=
uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
        if err != nil {
                log.Fatal(err)
        }

        products, err := client.ListProducts(&uber.Place{
                Latitude:  38.8971,
                Longitude: -77.0366,
        })
        if err != nil {
                log.Fatal(err)
        }

        for i, product := range products {
                fmt.Printf("#%d: ID: %q Product: %#v\n", i, product.ID,
product)
        }
}
```
Produces
```shell
&uber.Product{UpfrontFareEnabled:true, Capacity:2,
ID:"bc300c14-c30d-4d3f-afcb-19b240c16a13",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-uberpool-2.png",
CashEnabled:false, Shared:true, ShortDescription:"uberPOOL",
DisplayName:"POOL", Description:"Share the ride, share the cost"}
&uber.Product{UpfrontFareEnabled:true, Capacity:4,
ID:"dee8691c-8b48-4637-b048-300eee72d58d",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-uberx.png",
CashEnabled:false, Shared:false, ShortDescription:"uberX",
DisplayName:"uberX", Description:"The low-cost Uber"}
&uber.Product{UpfrontFareEnabled:true, Capacity:6,
ID:"9ffa937e-7d2e-4bcf-bc2b-ffec4ef24380",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-uberxl2.png",
CashEnabled:false, Shared:false, ShortDescription:"uberXL",
DisplayName:"uberXL", Description:"Low-cost rides for large groups"}
&uber.Product{UpfrontFareEnabled:true, Capacity:4,
ID:"bc98a16f-ad72-41a3-8624-809ce654ac57",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-family.png",
CashEnabled:false, Shared:false, ShortDescription:"uberX + Car Seat",
DisplayName:"uberX + Car Seat", Description:"uberX + Car Seat"}
&uber.Product{UpfrontFareEnabled:true, Capacity:4,
ID:"a52a9012-d73e-4127-8440-f273cddfd307",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-black.png",
CashEnabled:false, Shared:false, ShortDescription:"BLACK CAR",
DisplayName:"UberBLACK", Description:"The Original Uber"}
&uber.Product{UpfrontFareEnabled:true, Capacity:4,
ID:"2a299c73-098d-47cd-b32c-825cb155f82a",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-family.png",
CashEnabled:false, Shared:false, ShortDescription:"BLACK CAR + Car
Seat", DisplayName:"BLACK CAR + Car Seat", Description:"BLACK CAR + Car
Seat"}
&uber.Product{UpfrontFareEnabled:true, Capacity:6,
ID:"4e6fd14c-3866-40f1-b173-f12aeb8fbbd0",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-suv.png",
CashEnabled:false, Shared:false, ShortDescription:"SUV",
DisplayName:"UberSUV", Description:"Room for everyone"}
&uber.Product{UpfrontFareEnabled:true, Capacity:6,
ID:"74766497-b951-4eae-98c9-a67d87e2c0c4",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-family.png",
CashEnabled:false, Shared:false, ShortDescription:"SUV + Car Seat",
DisplayName:"SUV + Car Seat", Description:"SUV + Car Seat"}
&uber.Product{UpfrontFareEnabled:false, Capacity:4,
ID:"89f38d7a-d184-4054-9f2e-6b57c94143d6",
PriceDetails:(*uber.PriceDetails)(0xc4201d8120),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-uberx.png",
CashEnabled:false, Shared:false, ShortDescription:"Wheelchair",
DisplayName:"Wheelchair", Description:"TAXI WITHOUT THE HASSLE"}
&uber.Product{UpfrontFareEnabled:false, Capacity:4,
ID:"f67c83fb-4668-42eb-9aa1-ab32e710c8bf",
PriceDetails:(*uber.PriceDetails)(nil),
ImageURL:"http://d1a3f4spazzrp4.cloudfront.net/car-types/mono/mono-taxi.png",
CashEnabled:false, Shared:false, ShortDescription:"TAXI",
DisplayName:"uberTAXI", Description:"Taxi without the hassle"}
```
  • Loading branch information
odeke-em committed Jun 24, 2017
1 parent 50f2a7c commit f96061b
Show file tree
Hide file tree
Showing 15 changed files with 530 additions and 32 deletions.
72 changes: 53 additions & 19 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package uber_test
import (
"fmt"
"log"
"os"

"github.com/orijtech/uber/v1"
)

func Example_client_ListPaymentMethods() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -41,7 +42,7 @@ func Example_client_ListPaymentMethods() {
}

func Example_client_ListHistory() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func Example_client_ListHistory() {
}

func Example_client_ListAllMyHistory() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func Example_client_ListAllMyHistory() {
}

func Example_client_EstimatePrice() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func Example_client_EstimatePrice() {
}

func Example_client_EstimateTime() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -184,7 +185,7 @@ func Example_client_EstimateTime() {
}

func Example_client_RetrieveMyProfile() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -198,7 +199,7 @@ func Example_client_RetrieveMyProfile() {
}

func Example_client_ApplyPromoCode() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -226,7 +227,7 @@ func Example_client_RequestReceipt() {
}

func Example_client_RetrieveHomeAddress() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -240,7 +241,7 @@ func Example_client_RetrieveHomeAddress() {
}

func Example_client_RetrieveWorkAddress() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -254,7 +255,7 @@ func Example_client_RetrieveWorkAddress() {
}

func Example_client_UpdateHomeAddress() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -271,7 +272,7 @@ func Example_client_UpdateHomeAddress() {
}

func Example_client_UpdateWorkAddress() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -288,7 +289,7 @@ func Example_client_UpdateWorkAddress() {
}

func Example_client_RequestMap() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -302,18 +303,18 @@ func Example_client_RequestMap() {
}

func Example_client_OpenMap() {
client, err := uber.NewClient()
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}

if err := client.OpenMapForTrip("b5512127-a134-4bf4-b1ba-fe9f48f56d9d"); err != nil {
if err := client.OpenMapForTrip("64561dfe-87fa-41d7-807e-f364527b11cb"); err != nil {
log.Fatal(err)
}
}

func Example_client_UpfrontFare() {
client, err := uber.NewClientFromOAuth2File("./testdata/.uber/credentials.json")
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -341,7 +342,7 @@ func Example_client_UpfrontFare() {
}

func Example_client_RequestRide() {
client, err := uber.NewClientFromOAuth2File("./testdata/.uber/credentials.json")
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -366,7 +367,7 @@ func Example_client_RequestRide() {
}

func Example_client_RequestDelivery() {
client, err := uber.NewClientFromOAuth2File("./testdata/.uber/credentials.json")
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -421,15 +422,48 @@ func Example_client_RequestDelivery() {
}

func Example_client_CancelDelivery() {
client, err := uber.NewClientFromOAuth2File("./testdata/.uber/credentials.json")
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}

err := client.CancelDelivery("71a969ca-5359-4334-a7b7-5a1705869c51")
err = client.CancelDelivery("71a969ca-5359-4334-a7b7-5a1705869c51")
if err == nil {
log.Printf("Successfully canceled that delivery!")
} else {
log.Printf("Failed to cancel that delivery, err: %v", err)
}
}

func Example_client_ListProducts() {
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}

products, err := client.ListProducts(&uber.Place{
Latitude: 38.8971,
Longitude: -77.0366,
})
if err != nil {
log.Fatal(err)
}

for i, product := range products {
fmt.Printf("#%d: ID: %q Product: %#v\n", i, product.ID, product)
}
}

func Example_client_ProductByID() {
client, err := uber.NewClientFromOAuth2File(os.ExpandEnv("$HOME/.uber/credentials.json"))
if err != nil {
log.Fatal(err)
}

product, err := client.ProductByID("bc300c14-c30d-4d3f-afcb-19b240c16a13")
if err != nil {
log.Fatal(err)
}

fmt.Printf("The Product information: %#v\n", product)
}
12 changes: 9 additions & 3 deletions oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
type OAuth2AppConfig struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
}

var (
Expand Down Expand Up @@ -195,9 +196,15 @@ func Authorize(oconfig *OAuth2AppConfig, scopes ...string) (*oauth2.Token, error
AuthURL: OAuth2AuthURL,
TokenURL: OAuth2TokenURL,
},
RedirectURL: oconfig.RedirectURL,
}

state := fmt.Sprintf("%v%s", time.Now().Unix(), rand.Float32())
srvAddr := ":8889"
if config.RedirectURL == "" {
config.RedirectURL = fmt.Sprintf("http://localhost%s/", srvAddr)
}

state := fmt.Sprintf("%v%f", time.Now().Unix(), rand.Float32())
urlToVisit := config.AuthCodeURL(state, oauth2.AccessTypeOffline)
fmt.Printf("Please visit this URL for the auth dialog: %v\n", urlToVisit)

Expand All @@ -210,8 +217,7 @@ func Authorize(oconfig *OAuth2AppConfig, scopes ...string) (*oauth2.Token, error
})

defer close(callbackURLChan)
addr := ":8889"
if err := http.ListenAndServe(addr, nil); err != nil {
if err := http.ListenAndServe(srvAddr, nil); err != nil {
log.Fatal(err)
}
}()
Expand Down
14 changes: 14 additions & 0 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type Client struct {
sandboxed bool
}

func (c *Client) hasServerToken() bool {
c.RLock()
defer c.RUnlock()

return c.token != ""
}

// Sandboxed if set to true, the client will send requests
// to the sandboxed API endpoint.
// See:
Expand Down Expand Up @@ -151,6 +158,13 @@ func (c *Client) doAuthAndHTTPReq(req *http.Request) ([]byte, http.Header, error
return c.doHTTPReq(req)
}

func (c *Client) doReq(req *http.Request) ([]byte, http.Header, error) {
if c.hasServerToken() {
req.Header.Set("Authorization", c.bearerToken())
}
return c.doHTTPReq(req)
}

func (c *Client) doHTTPReq(req *http.Request) ([]byte, http.Header, error) {
res, err := c.httpClient().Do(req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion v1/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *Client) ListHistory(threq *Pager) (thChan chan *TripThreadPage, cancelF
return
}

slurp, _, err := c.doAuthAndHTTPReq(req)
slurp, _, err := c.doReq(req)
if err != nil {
ttp.Err = err
historyChan <- ttp
Expand Down
2 changes: 1 addition & 1 deletion v1/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) RequestMap(tripID string) (*Map, error) {
return nil, err
}

slurp, _, err := c.doAuthAndHTTPReq(req)
slurp, _, err := c.doReq(req)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion v1/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (pm *PaymentMethod) PaymentMethodToString() string {
return paymentMethodToString[*pm]
}

func (pm PaymentMethod) String() string {
return pm.PaymentMethodToString()
}

func StringToPaymentMethod(str string) PaymentMethod {
pm, ok := stringToPaymentMethod[str]
if !ok {
Expand Down Expand Up @@ -138,7 +142,7 @@ func (c *Client) ListPaymentMethods() (*PaymentListing, error) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Language", "en_US")

slurp, _, err := c.doAuthAndHTTPReq(req)
slurp, _, err := c.doReq(req)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion v1/places.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Client) Place(placeName PlaceName) (*Place, error) {
}

func (c *Client) doPlaceReq(req *http.Request) (*Place, error) {
slurp, _, err := c.doAuthAndHTTPReq(req)
slurp, _, err := c.doReq(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,5 +90,6 @@ func (c *Client) UpdatePlace(pp *PlaceParams) (*Place, error) {
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.doPlaceReq(req)
}
5 changes: 3 additions & 2 deletions v1/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (c *Client) EstimatePrice(ereq *EstimateRequest) (pagesChan chan *PriceEsti
return
}

slurp, _, err := c.doAuthAndHTTPReq(req)
slurp, _, err := c.doReq(req)
if err != nil {
ep.Err = err
estimatesPageChan <- ep
Expand Down Expand Up @@ -283,7 +283,8 @@ func (c *Client) UpfrontFare(esReq *EstimateRequest) (*UpfrontFare, error) {
if err != nil {
return nil, err
}
slurp, _, err := c.doHTTPReq(req)
req.Header.Set("Content-Type", "application/json")
slurp, _, err := c.doReq(req)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit f96061b

Please sign in to comment.