2
2
Getting started with the API
3
3
############################
4
4
5
- The ``gitlab `` package provides 3 base types:
5
+ python-gitlab supports both GitLab v3 and v4 APIs.
6
+
7
+ v3 being deprecated by GitLab, its support in python-gitlab will be minimal.
8
+ The development team will focus on v4.
9
+
10
+ v3 is still the default API used by python-gitlab, for compatibility reasons..
11
+
12
+
13
+ Base types
14
+ ==========
15
+
16
+ The ``gitlab `` package provides some base types.
6
17
7
18
* ``gitlab.Gitlab `` is the primary class, handling the HTTP requests. It holds
8
19
the GitLab URL and authentication information.
9
- * ``gitlab.GitlabObject `` is the base class for all the GitLab objects. These
10
- objects provide an abstraction for GitLab resources (projects, groups, and so
11
- on).
12
- * ``gitlab.BaseManager `` is the base class for objects managers, providing the
13
- API to manipulate the resources and their attributes.
20
+
21
+ For v4 the following types are defined:
22
+
23
+ * ``gitlab.base.RESTObject `` is the base class for all the GitLab v4 objects.
24
+ These objects provide an abstraction for GitLab resources (projects, groups,
25
+ and so on).
26
+ * ``gitlab.base.RESTManager `` is the base class for v4 objects managers,
27
+ providing the API to manipulate the resources and their attributes.
28
+
29
+ For v3 the following types are defined:
30
+
31
+ * ``gitlab.base.GitlabObject `` is the base class for all the GitLab v3 objects.
32
+ These objects provide an abstraction for GitLab resources (projects, groups,
33
+ and so on).
34
+ * ``gitlab.base.BaseManager `` is the base class for v3 objects managers,
35
+ providing the API to manipulate the resources and their attributes.
36
+
14
37
15
38
``gitlab.Gitlab `` class
16
39
=======================
@@ -40,7 +63,9 @@ You can also use configuration files to create ``gitlab.Gitlab`` objects:
40
63
See the :ref: `cli_configuration ` section for more information about
41
64
configuration files.
42
65
43
- **GitLab v4 support **
66
+
67
+ API version
68
+ ===========
44
69
45
70
``python-gitlab `` uses the v3 GitLab API by default. Use the ``api_version ``
46
71
parameter to switch to v4:
@@ -53,15 +78,17 @@ parameter to switch to v4:
53
78
54
79
.. warning ::
55
80
56
- The v4 support is experimental.
81
+ The python-gitlab API is not the same for v3 and v4. Make sure to read
82
+ :ref: `switching_to_v4 ` before upgrading.
83
+
84
+ v4 will become the default in python-gitlab.
57
85
58
86
Managers
59
87
========
60
88
61
89
The ``gitlab.Gitlab `` class provides managers to access the GitLab resources.
62
90
Each manager provides a set of methods to act on the resources. The available
63
- methods depend on the resource type. Resources are represented as
64
- ``gitlab.GitlabObject ``-derived objects.
91
+ methods depend on the resource type.
65
92
66
93
Examples:
67
94
@@ -84,17 +111,22 @@ Examples:
84
111
85
112
The attributes of objects are defined upon object creation, and depend on the
86
113
GitLab API itself. To list the available information associated with an object
87
- use the python introspection tools:
114
+ use the python introspection tools for v3, or the ``attributes `` attribute for
115
+ v4:
88
116
89
117
.. code-block :: python
90
118
91
119
project = gl.projects.get(1 )
120
+
121
+ # v3
92
122
print (vars (project))
93
123
# or
94
124
print (project.__dict__ )
95
125
96
- Some ``gitlab.GitlabObject `` classes also provide managers to access related
97
- GitLab resources:
126
+ # v4
127
+ print (project.attributes)
128
+
129
+ Some objects also provide managers to access related GitLab resources:
98
130
99
131
.. code-block :: python
100
132
@@ -105,7 +137,7 @@ GitLab resources:
105
137
Gitlab Objects
106
138
==============
107
139
108
- You can update or delete an object when it exists as a `` GitlabObject `` object :
140
+ You can update or delete a remote object when it exists locally :
109
141
110
142
.. code-block :: python
111
143
@@ -119,15 +151,31 @@ You can update or delete an object when it exists as a ``GitlabObject`` object:
119
151
project.delete()
120
152
121
153
122
- Some `` GitlabObject ``-derived classes provide additional methods, allowing more
123
- actions on the GitLab resources. For example:
154
+ Some classes provide additional methods, allowing more actions on the GitLab
155
+ resources. For example:
124
156
125
157
.. code-block :: python
126
158
127
159
# star a git repository
128
160
project = gl.projects.get(1 )
129
161
project.star()
130
162
163
+ Lazy objects (v4 only)
164
+ ======================
165
+
166
+ To avoid useless calls to the server API, you can create lazy objects. These
167
+ objects are created locally using a known ID, and give access to other managers
168
+ and methods.
169
+
170
+ The following exemple will only make one API call to the GitLab server to star
171
+ a project:
172
+
173
+ .. code-block :: python
174
+
175
+ # star a git repository
176
+ project = gl.projects.get(1 , lazy = True ) # no API call
177
+ project.star() # API call
178
+
131
179
Pagination
132
180
==========
133
181
@@ -142,16 +190,15 @@ listing methods support the ``page`` and ``per_page`` parameters:
142
190
143
191
The first page is page 1, not page 0.
144
192
145
-
146
- By default GitLab does not return the complete list of items. Use the ``all ``
193
+ By default GitLab does not return the complete list of items. Use the ``all ``
147
194
parameter to get all the items when using listing methods:
148
195
149
196
.. code-block :: python
150
197
151
198
all_groups = gl.groups.list(all = True )
152
199
all_owned_projects = gl.projects.owned(all = True )
153
200
154
- .. note ::
201
+ .. warning ::
155
202
156
203
python-gitlab will iterate over the list by calling the corresponding API
157
204
multiple times. This might take some time if you have a lot of items to
@@ -160,6 +207,15 @@ parameter to get all the items when using listing methods:
160
207
use ``safe_all=True `` instead to stop pagination automatically if the
161
208
recursion limit is hit.
162
209
210
+ With v4, ``list() `` methods can also return a generator object which will
211
+ handle the next calls to the API when required:
212
+
213
+ .. code-block :: python
214
+
215
+ items = gl.groups.list(as_list = False )
216
+ for item in items:
217
+ print (item.attributes)
218
+
163
219
Sudo
164
220
====
165
221
0 commit comments