Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
97 lines (78 sloc) 2.85 KB
# -*- coding: utf-8 -*-
@brief Wrapper around GitLab API.
import json
class GitLabException(Exception):
specific exception, stores the request
def __init__(self, mes, req=None):
@param mes message
@param req request which caused the failure
Exception.__init__(self, mes)
self.request = req
def __str__(self):
if self.request is None:
return Exception.__str__(self)
return "{0}\nCODE: {1}\n[giterror]\n{2}".format(
Exception.__str__(self), self.request.status_code, self.request.content)
class GitLabAPI:
Wrapper around GitLab Server.
The API is defined at `gitlabhq/doc/api <>`_
def __init__(self, host, verify_ssl=True):
@param host git lab host
@param verify_ssl use_ssl (SSL connection)
""" = host.rstrip("/")
if not
"https://") and not"http://"):
raise GitLabException("host should start with https:// or http://")
self.api_url = + "/api/v3"
self.verify_ssl = verify_ssl
def login(self, user, password):
@param user user
@param password password
import requests
data = {"login": user, "password": password}
url = "{0}/Session".format(self.api_url)
request =, data=data, verify=self.verify_ssl,
headers={"connection": "close"})
if request.status_code == 201:
self.token = json.loads(
self.headers = {"PRIVATE-TOKEN": self.token, "connection": "close"}
elif request.status_code == 404:
raise GitLabException("unable to login to " + url, request)
msg = json.loads(request.content.decode("utf-8"))['message']
raise GitLabException(
"unable to login to " + url + "\n" + msg, request)
def get_projects(self, page=1, per_page=100):
returns a list of dictionaries
@return list of dictionaries
import requests
data = {'page': page, 'per_page': per_page}
request = requests.get(
self.api_url, params=data, headers=self.headers, verify=self.verify_ssl)
if request.status_code == 200:
return json.loads(request.content.decode("utf-8"))
raise GitLabException(
"unable to retreive the list of projects: {0}".format(request), request)
You can’t perform that action at this time.