4

How would I convert the following curl commands from the Lyft api to http interfaced requests (so they can be executed over web like https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY)? If http requests translation is not possible, how would I integrate and process these curl commands in R?

 #Authentication code 
 curl -X POST -H "Content-Type: application/json" \
 --user "<client_id>:<client_secret>" \
 -d '{"grant_type": "client_credentials", "scope": "public"}' \
 'https://api.lyft.com/oauth/token'

 #Search query
 curl --include -X GET -H 'Authorization: Bearer <access_token>' \
 'https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167'
sparkh2o
  • 254
  • 1
  • 4
  • 16

3 Answers3

3

Hi you could use https://curl.trillworks.com/ to convert curl commands to the language of your choice or you could use lyft SDK's (for Python use https://pypi.python.org/pypi/lyft_rides).

Here is the corresponding Python version

import requests

headers = {
    'Content-Type': 'application/json',
}

data = '{"grant_type": "client_credentials", "scope": "public"}'

requests.post('https://api.lyft.com/oauth/token', headers=headers, data=data, auth=('<client_id>', '<client_secret>'))

From this post request you will get access token that has to be used for subsequent requests.

headers = {
    'Authorization': 'Bearer <access_token>',
}

requests.get('https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167', headers=headers)

Note: I haven't tested this as I am unable to create a lyft developer account so there might be some minor changes in the code given here.

Jithu R Jacob
  • 368
  • 2
  • 17
  • Thanks for the answer! My application, however, is in R so I am looking for a more generic https implementation so that programming language doesn't become an impediment. – sparkh2o Aug 22 '17 at 06:25
  • This is language independent at a high level. You need to send an http POST request to get bearer token then use the token to send a GET request. You could use httr package for this https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html – Jithu R Jacob Aug 22 '17 at 09:50
1

[Full disclosure: I'm a Developer Advocate at Lyft] I'm not very familiar with R, but could you integrate the responses/calls using the method described in this blog post?

https://www.r-bloggers.com/accessing-apis-from-r-and-a-little-r-programming/

  • 1
    Thanks for the article! Through this and some surfing, I was able to find https://cran.r-project.org/web/packages/jsonlite/vignettes/json-apis.html. Lyft api is similar to Twitter's in its format so above was definitely helpful in inspiring the solution. – sparkh2o Aug 29 '17 at 02:40
0

ReqBin can automatically convert Curl commands to HTTP requests

An example of such request: Convert Curl to HTTP Request

Khachatur
  • 921
  • 1
  • 12
  • 30