Reputation: 2496
First, I am a total n00b to Python. I am using github-flask, and flask obviously, to pull data from the GitHub API. I am trying to use the contents_url
and retrieve a file. The URL from the GitHub API is something like:
// json
{
...
"contents_url": "https://api.github.com/repos/<org_name>/<repo_name>/contents/{+path}"
...
}
... and when I try and give that to the github-flask instance I get a TypeError, "TypeError: request() got an unexpected keyword argument 'path'" using:
# python
contents = github.get(repo['contents_url'], path='.gitignore')
I'm pretty sure I am missing something simple. I don't have to resort to string manipulation do I?
Upvotes: 1
Views: 322
Reputation: 401
Python's recommended string interpolation is the .format
method. Your code will work with that with just a few minor changes:
contents = github.get(repo['contents_url'].format(path='.gitignore'))
But you'll also have to change your contents_url
slightly:
https://api.github.com/repos/<org_name>/<repo_name>/contents/{path}
Just be careful - .format
interpolates based on curly braces, so any literal curly braces need to be escaped. More information is available here: https://docs.python.org/3/library/string.html#formatstrings
Edit: As you mentioned in the comment below, the URL is coming directly from GitHub's API and you can't/shouldn't change it. It turns out they're using RFC 6570 URL templates (see https://developer.github.com/v3/#hypermedia). If you use the uritemplate library I suggested below, the code will look like this:
from uritemplate import expand
# ...
contents = github.get(expand(repo['contents_url'], {'path': '.gitignore'}))
Upvotes: 4