Starring a repository with the github API

239 views Asked by At

I'm trying to star a repository using the GithubAPI. This is done via a PUT request to /user/starred/:owner/:repo. I attempted to implement this feature in python using the requests library, but it isn't working. Here is a minimum working example:

The constant are defined as GITHUB_API = api.github.com, GITHUB_USER = the username of the owner of the repo to be starred, and GITHUB_REPO = the name of the repo to be starred

url = urljoin(GITHUB_API, (user + '/starred/' + GITHUB_USER + '/' + GITHUB_REPO))
r = requests.put(url,auth=(user,password))
print r.text

This code results in an error that reads:

{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

I think that I'm missing something fundamental about the process of issuing a PUT request.

1

There are 1 answers

1
olav On BEST ANSWER

The problem here is the parameters you pass to urljoin(). The first parameter is supposed to be an absolute URL, while the second parameter is a relative URL. urljoin() then creates an absolute URL from that.

Also, "user" in this case is supposed to be a literal part of the URL, and not the username.

In this case, I would forgo the urljoin()-function completely, and instead use simple string substitution:

url = 'https://api.github.com/user/starred/{owner}/{repo}'.format(
          owner=GITHUB_USER,
          repo=GITHUB_REPO
      )