Skip to content

Use PRIVATE-TOKEN header for passing the auth token #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ def setCredentials(self, email, password):

def rawGet(self, path, with_token=False):
url = '%s%s' % (self._url, path)
if with_token:
url += "?private_token=%s" % self.private_token

try:
r = requests.get(url)
if with_token:
r = requests.get(url, headers={"PRIVATE-TOKEN": self.private_token})
else:
r = requests.get(url)
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand All @@ -158,11 +158,12 @@ def rawPost(self, path, data):

def rawPut(self, path, with_token=False):
url = '%s%s' % (self._url, path)
if with_token:
url += "?private_token=%s" % self.private_token

try:
r = requests.put(url)
if with_token:
r = requests.put(url, headers={"PRIVATE-TOKEN": self.private_token})
else:
r = requests.put(url)
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand All @@ -181,13 +182,13 @@ def list(self, obj_class, **kwargs):
url = obj_class._url
if kwargs:
url = obj_class._url % kwargs
url = '%s%s?private_token=%s' % (self._url, url, self.private_token)
url = '%s%s' % (self._url, url)
if kwargs:
url += "&%s" % ("&".join(
["%s=%s" % (k, v) for k, v in kwargs.items()]))

try:
r = requests.get(url)
r = requests.get(url, headers={"PRIVATE-TOKEN": self.private_token})
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand Down Expand Up @@ -223,17 +224,17 @@ def get(self, obj_class, id=None, **kwargs):
url = obj_class._url % kwargs
if id is not None:
try:
url = '%s%s/%d?private_token=%s' % \
(self._url, url, id, self.private_token)
url = '%s%s/%d' % \
(self._url, url, id)
except TypeError: # id might be a str (ProjectBranch)
url = '%s%s/%s?private_token=%s' % \
(self._url, url, id, self.private_token)
url = '%s%s/%s' % \
(self._url, url, id)
else:
url = '%s%s?private_token=%s' % \
(self._url, url, self.private_token)
url = '%s%s' % \
(self._url, url)

try:
r = requests.get(url)
r = requests.get(url, headers={"PRIVATE-TOKEN": self.private_token})
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand All @@ -249,11 +250,11 @@ def get(self, obj_class, id=None, **kwargs):

def delete(self, obj):
url = obj._url % obj.__dict__
url = '%s%s/%d?private_token=%s' % \
(self._url, url, obj.id, self.private_token)
url = '%s%s/%d' % \
(self._url, url, obj.id)

try:
r = requests.delete(url)
r = requests.delete(url, headers={"PRIVATE-TOKEN": self.private_token})
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand All @@ -276,15 +277,15 @@ def create(self, obj):
", ".join(missing))

url = obj._url % obj.__dict__
url = '%s%s?private_token=%s' % (self._url, url, self.private_token)
url = '%s%s' % (self._url, url)

print url
print obj.__dict__

try:
# TODO: avoid too much work on the server side by filtering the
# __dict__ keys
r = requests.post(url, obj.__dict__)
r = requests.post(url, obj.__dict__, headers={"PRIVATE-TOKEN": self.private_token})
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand All @@ -298,8 +299,8 @@ def create(self, obj):

def update(self, obj):
url = obj._url % obj.__dict__
url = '%s%s/%d?private_token=%s' % \
(self._url, url, obj.id, self.private_token)
url = '%s%s/%d' % \
(self._url, url, obj.id)

# build a dict of data that can really be sent to server
d = {}
Expand All @@ -308,7 +309,7 @@ def update(self, obj):
d[k] = str(v)

try:
r = requests.put(url, d)
r = requests.put(url, d, headers={"PRIVATE-TOKEN": self.private_token})
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)
Expand Down Expand Up @@ -576,9 +577,9 @@ class Group(GitlabObject):
shortPrintAttr = 'name'

def transfer_project(self, id):
url = '/groups/%d/projects/%d?private_token=%s' % \
(self.id, id, self.gitlab.private_token)
r = self.gitlab.rawPost(url, None)
url = '/groups/%d/projects/%d' % \
(self.id, id)
r = self.gitlab.rawPost(url, None, headers={"PRIVATE-TOKEN": self.gitlab.private_token})
if r.status_code != 201:
raise GitlabTransferProjectError()

Expand Down