22Getting started with the API
33############################
44
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.
617
718* ``gitlab.Gitlab `` is the primary class, handling the HTTP requests. It holds
819 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+
1437
1538``gitlab.Gitlab `` class
1639=======================
@@ -40,7 +63,9 @@ You can also use configuration files to create ``gitlab.Gitlab`` objects:
4063 See the :ref: `cli_configuration ` section for more information about
4164configuration files.
4265
43- **GitLab v4 support **
66+
67+ API version
68+ ===========
4469
4570``python-gitlab `` uses the v3 GitLab API by default. Use the ``api_version ``
4671parameter to switch to v4:
@@ -53,15 +78,17 @@ parameter to switch to v4:
5378
5479 .. warning ::
5580
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.
5785
5886Managers
5987========
6088
6189The ``gitlab.Gitlab `` class provides managers to access the GitLab resources.
6290Each 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.
6592
6693Examples:
6794
@@ -84,17 +111,22 @@ Examples:
84111
85112 The attributes of objects are defined upon object creation, and depend on the
86113GitLab 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:
88116
89117.. code-block :: python
90118
91119 project = gl.projects.get(1 )
120+
121+ # v3
92122 print (vars (project))
93123 # or
94124 print (project.__dict__ )
95125
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:
98130
99131.. code-block :: python
100132
@@ -105,7 +137,7 @@ GitLab resources:
105137 Gitlab Objects
106138==============
107139
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 :
109141
110142.. code-block :: python
111143
@@ -119,15 +151,31 @@ You can update or delete an object when it exists as a ``GitlabObject`` object:
119151 project.delete()
120152
121153
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:
124156
125157.. code-block :: python
126158
127159 # star a git repository
128160 project = gl.projects.get(1 )
129161 project.star()
130162
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+
131179 Pagination
132180==========
133181
@@ -142,16 +190,15 @@ listing methods support the ``page`` and ``per_page`` parameters:
142190
143191 The first page is page 1, not page 0.
144192
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 ``
147194parameter to get all the items when using listing methods:
148195
149196.. code-block :: python
150197
151198 all_groups = gl.groups.list(all = True )
152199 all_owned_projects = gl.projects.owned(all = True )
153200
154- .. note ::
201+ .. warning ::
155202
156203 python-gitlab will iterate over the list by calling the corresponding API
157204 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:
160207 use ``safe_all=True `` instead to stop pagination automatically if the
161208 recursion limit is hit.
162209
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+
163219 Sudo
164220====
165221
0 commit comments