How To Use Requests Python Module To Make Curl Calls
Solution 1:
It looks like the first call you should make is to the '/me' endpoint, and then pull the API token from the response:
importrequestsusername='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 responser = 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.
Post a Comment for "How To Use Requests Python Module To Make Curl Calls"