Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trip experiences: implement the trip experiences API #31

Open
odeke-em opened this issue Jun 10, 2017 · 1 comment
Open

trip experiences: implement the trip experiences API #31

odeke-em opened this issue Jun 10, 2017 · 1 comment

Comments

@odeke-em
Copy link
Member

The trip experiences API allows Uber to send contextual information about the user, for example:

  • How long they have left on their trip
  • The current location of the Uber(where the user and driver are at the moment)
  • Where the user is coming from and also going to
    Now that webhooks have been added with webhooks: implemented Uber webhooks #30, it is now possible to implement a trip experiences app that say integrates with their:
  • Nest app at home to turn on the thermostat
  • Find local news
  • Find tourist attractions nearby and make recommendations
@odeke-em
Copy link
Member Author

So in regards to the trip experiences, checklist of already implemented features:

  • /GET authorize

    uber/cmd/uber/main.go

    Lines 41 to 71 in 5b520a3

    func authorize() {
    uberCredsDirPath, err := ensureUberCredsDirExists()
    if err != nil {
    log.Fatal(err)
    }
    scopes := []string{
    oauth2.ScopeProfile, oauth2.ScopeRequest,
    oauth2.ScopeHistory, oauth2.ScopePlaces,
    oauth2.ScopeRequestReceipt,
    }
    token, err := oauth2.AuthorizeByEnvApp(scopes...)
    if err != nil {
    log.Fatal(err)
    }
    blob, err := json.Marshal(token)
    if err != nil {
    log.Fatal(err)
    }
    credsPath := filepath.Join(uberCredsDirPath, "credentials.json")
    f, err := os.Create(credsPath)
    if err != nil {
    log.Fatal(err)
    }
    f.Write(blob)
    log.Printf("Successfully saved your OAuth2.0 token to %q", credsPath)
    }
  • /POST token

    uber/cmd/uber/main.go

    Lines 41 to 71 in 5b520a3

    func authorize() {
    uberCredsDirPath, err := ensureUberCredsDirExists()
    if err != nil {
    log.Fatal(err)
    }
    scopes := []string{
    oauth2.ScopeProfile, oauth2.ScopeRequest,
    oauth2.ScopeHistory, oauth2.ScopePlaces,
    oauth2.ScopeRequestReceipt,
    }
    token, err := oauth2.AuthorizeByEnvApp(scopes...)
    if err != nil {
    log.Fatal(err)
    }
    blob, err := json.Marshal(token)
    if err != nil {
    log.Fatal(err)
    }
    credsPath := filepath.Join(uberCredsDirPath, "credentials.json")
    f, err := os.Create(credsPath)
    if err != nil {
    log.Fatal(err)
    }
    f.Write(blob)
    log.Printf("Successfully saved your OAuth2.0 token to %q", credsPath)
    }
    but implemented in

    uber/oauth2/oauth2.go

    Lines 189 to 232 in e28016a

    func Authorize(oconfig *OAuth2AppConfig, scopes ...string) (*oauth2.Token, error) {
    config := &oauth2.Config{
    ClientID: oconfig.ClientID,
    ClientSecret: oconfig.ClientSecret,
    Scopes: scopes,
    Endpoint: oauth2.Endpoint{
    AuthURL: OAuth2AuthURL,
    TokenURL: OAuth2TokenURL,
    },
    }
    state := fmt.Sprintf("%v%s", time.Now().Unix(), rand.Float32())
    urlToVisit := config.AuthCodeURL(state, oauth2.AccessTypeOffline)
    fmt.Printf("Please visit this URL for the auth dialog: %v\n", urlToVisit)
    callbackURLChan := make(chan url.Values)
    go func() {
    http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
    query := req.URL.Query()
    callbackURLChan <- query
    fmt.Fprintf(rw, "Received the token successfully. Please return to your terminal")
    })
    defer close(callbackURLChan)
    addr := ":8889"
    if err := http.ListenAndServe(addr, nil); err != nil {
    log.Fatal(err)
    }
    }()
    urlValues := <-callbackURLChan
    gotState, wantState := urlValues.Get("state"), state
    if gotState != wantState {
    return nil, fmt.Errorf("states do not match: got: %q want: %q", gotState, wantState)
    }
    code := urlValues.Get("code")
    ctx := context.Background()
    token, err := config.Exchange(ctx, code)
    if err != nil {
    return nil, err
    }
    return token, nil
    }
  • Ride products /GET me: Already implemented client.MyProfile
func Example_client_RetrieveMyProfile() {
	client, err := uber.NewClient()
	if err != nil {
		log.Fatal(err)
	}

	myProfile, err := client.RetrieveMyProfile()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Here is my profile: %#v\n", myProfile)
}
  • Ride products /GET History, already implemented
func Example_client_ListHistory() {
	client, err := uber.NewClient()
	if err != nil {
		log.Fatal(err)
	}

	pagesChan, cancelPaging, err := client.ListHistory(&uber.Pager{
		MaxPages:     4,
		LimitPerPage: 10,
		StartOffset:  0,
	})

	if err != nil {
		log.Fatal(err)
	}

	for page := range pagesChan {
		if page.Err != nil {
			fmt.Printf("Page: #%d err: %v\n", page.PageNumber, page.Err)
			continue
		}

		fmt.Printf("Page: #%d\n\n", page.PageNumber)
		for i, trip := range page.Trips {
			startCity := trip.StartCity
			if startCity.Name == "Tokyo" {
				fmt.Printf("aha found the first Tokyo trip, canceling any more requests!: %#v\n", trip)
				cancelPaging()
				break
			}

			// Otherwise, continue listing
			fmt.Printf("Trip: #%d ==> %#v place: %#v\n", i, trip, startCity)
		}
	}
}

All the necessary methods have been implemented, now what's needed is an app to glue all the pieces together simple ideas:

  • Linking up with a Phillips Hue bulb and turn it on on when the car approaches
  • Implement Brad Fitzpatrick's garage door opener for Uber
  • Link up with Nest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant