1919from __future__ import print_function
2020from __future__ import division
2121from __future__ import absolute_import
22+ import importlib
2223import inspect
2324import itertools
2425import json
3132import gitlab .config
3233from gitlab .const import * # noqa
3334from gitlab .exceptions import * # noqa
34- from gitlab .objects import * # noqa
35+ from gitlab .v3 . objects import * # noqa
3536
3637__title__ = 'python-gitlab'
3738__version__ = '0.20'
@@ -65,13 +66,15 @@ class Gitlab(object):
6566 timeout (float): Timeout to use for requests to the GitLab server.
6667 http_username (str): Username for HTTP authentication
6768 http_password (str): Password for HTTP authentication
69+ api_version (str): Gitlab API version to use (3 or 4)
6870 """
6971
7072 def __init__ (self , url , private_token = None , email = None , password = None ,
7173 ssl_verify = True , http_username = None , http_password = None ,
72- timeout = None ):
74+ timeout = None , api_version = '3' ):
7375
74- self ._url = '%s/api/v3' % url
76+ self ._api_version = str (api_version )
77+ self ._url = '%s/api/v%s' % (url , api_version )
7578 #: Timeout to use for requests to gitlab server
7679 self .timeout = timeout
7780 #: Headers that will be used in request to GitLab
@@ -89,42 +92,50 @@ def __init__(self, url, private_token=None, email=None, password=None,
8992 #: Create a session object for requests
9093 self .session = requests .Session ()
9194
92- self .broadcastmessages = BroadcastMessageManager (self )
93- self .keys = KeyManager (self )
94- self .deploykeys = DeployKeyManager (self )
95- self .gitlabciymls = GitlabciymlManager (self )
96- self .gitignores = GitignoreManager (self )
97- self .groups = GroupManager (self )
98- self .hooks = HookManager (self )
99- self .issues = IssueManager (self )
100- self .licenses = LicenseManager (self )
101- self .namespaces = NamespaceManager (self )
102- self .notificationsettings = NotificationSettingsManager (self )
103- self .projects = ProjectManager (self )
104- self .runners = RunnerManager (self )
105- self .settings = ApplicationSettingsManager (self )
106- self .sidekiq = SidekiqManager (self )
107- self .snippets = SnippetManager (self )
108- self .users = UserManager (self )
109- self .teams = TeamManager (self )
110- self .todos = TodoManager (self )
95+ objects = importlib .import_module ('gitlab.v%s.objects' %
96+ self ._api_version )
97+
98+ self .broadcastmessages = objects .BroadcastMessageManager (self )
99+ self .deploykeys = objects .DeployKeyManager (self )
100+ self .gitlabciymls = objects .GitlabciymlManager (self )
101+ self .gitignores = objects .GitignoreManager (self )
102+ self .groups = objects .GroupManager (self )
103+ self .hooks = objects .HookManager (self )
104+ self .issues = objects .IssueManager (self )
105+ self .licenses = objects .LicenseManager (self )
106+ self .namespaces = objects .NamespaceManager (self )
107+ self .notificationsettings = objects .NotificationSettingsManager (self )
108+ self .projects = objects .ProjectManager (self )
109+ self .runners = objects .RunnerManager (self )
110+ self .settings = objects .ApplicationSettingsManager (self )
111+ self .sidekiq = objects .SidekiqManager (self )
112+ self .snippets = objects .SnippetManager (self )
113+ self .users = objects .UserManager (self )
114+ self .todos = objects .TodoManager (self )
115+ if self ._api_version == '3' :
116+ self .keys = objects .KeyManager (self )
117+ self .teams = objects .TeamManager (self )
111118
112119 # build the "submanagers"
113- for parent_cls in six .itervalues (globals ( )):
120+ for parent_cls in six .itervalues (vars ( objects )):
114121 if (not inspect .isclass (parent_cls )
115- or not issubclass (parent_cls , GitlabObject )
116- or parent_cls == CurrentUser ):
122+ or not issubclass (parent_cls , objects . GitlabObject )
123+ or parent_cls == objects . CurrentUser ):
117124 continue
118125
119126 if not parent_cls .managers :
120127 continue
121128
122- for var , cls , attrs in parent_cls .managers :
129+ for var , cls_name , attrs in parent_cls .managers :
123130 var_name = '%s_%s' % (self ._cls_to_manager_prefix (parent_cls ),
124131 var )
125- manager = cls (self )
132+ manager = getattr ( objects , cls_name ) (self )
126133 setattr (self , var_name , manager )
127134
135+ @property
136+ def api_version (self ):
137+ return self ._api_version
138+
128139 def _cls_to_manager_prefix (self , cls ):
129140 # Manage bad naming decisions
130141 camel_case = (cls .__name__
@@ -152,7 +163,8 @@ def from_config(gitlab_id=None, config_files=None):
152163 return Gitlab (config .url , private_token = config .token ,
153164 ssl_verify = config .ssl_verify , timeout = config .timeout ,
154165 http_username = config .http_username ,
155- http_password = config .http_password )
166+ http_password = config .http_password ,
167+ api_version = config .api_version )
156168
157169 def auth (self ):
158170 """Performs an authentication.
@@ -225,7 +237,7 @@ def set_url(self, url):
225237 warnings .warn ('set_url() is deprecated, create a new Gitlab instance '
226238 'if you need an updated URL.' ,
227239 DeprecationWarning )
228- self ._url = '%s/api/v3 ' % url
240+ self ._url = '%s/api/v%s ' % ( url , self . _api_version )
229241
230242 def _construct_url (self , id_ , obj , parameters , action = None ):
231243 if 'next_url' in parameters :
@@ -505,7 +517,7 @@ def delete(self, obj, id=None, **kwargs):
505517
506518 r = self ._raw_delete (url , ** params )
507519 raise_error_from_response (r , GitlabDeleteError ,
508- expected_code = [200 , 204 ])
520+ expected_code = [200 , 202 , 204 ])
509521 return True
510522
511523 def create (self , obj , ** kwargs ):
0 commit comments