How to use Requests Python module to make curl calls

不想你离开。 提交于 2019-12-01 01:50:21

It looks like the first call you should make is to the '/me' endpoint, and then pull the API token from the response:

import requests

username = 'my_username'
password = 'my_password'
url = 'https://www.pivotaltracker.com/services/v5/me'

r = requests.get(url, auth=(username, password))
response_json = r.json()
token = response_json['api_token']

You can get some other stuff besides your API token from that endpoint. Take a look at the documentation for that endpoint to see if there is anything else you will need.

Once you've gotten your API token, all the other calls will be fairly simple. For example:

project_id = 'your_project_id' # could get this from the previous response
r = requests.get('https://www.pivotaltracker.com/services/v5/projects/{}/epics/4'.format(project_id), headers={'X-TrackerToken':token})

I'll explain the parts of the cURL call they have for this example and how they translate:

export VARNAME

Set a variable for the cURL call to use. Where you see $VARNAME is where the variables are being used.

-X GET

I don't know why they include this. This just specifies to use a GET, which is the default for cURL. Using requests.get takes care of this. However, for ones that have -X POST, you'd use requests.post, etc.

-H "X-TrackerToken: $TOKEN"

This specifies a header. For Requests, we use the headers keyword argument - headers={key:value}. In this specific example, we have headers={'X-TrackerToken':token}.

"https://..."

The url. That goes in as the first argument. Variables (like $PROJECT_ID in your example) can be inserted using the format method of strings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!