atlassian-python-api 3.41.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. atlassian/VERSION +1 -0
  2. atlassian/__init__.py +32 -0
  3. atlassian/bamboo.py +1343 -0
  4. atlassian/bitbucket/__init__.py +3572 -0
  5. atlassian/bitbucket/base.py +198 -0
  6. atlassian/bitbucket/cloud/__init__.py +24 -0
  7. atlassian/bitbucket/cloud/base.py +123 -0
  8. atlassian/bitbucket/cloud/common/__init__.py +1 -0
  9. atlassian/bitbucket/cloud/common/builds.py +85 -0
  10. atlassian/bitbucket/cloud/common/comments.py +50 -0
  11. atlassian/bitbucket/cloud/common/users.py +79 -0
  12. atlassian/bitbucket/cloud/repositories/__init__.py +437 -0
  13. atlassian/bitbucket/cloud/repositories/branchRestrictions.py +175 -0
  14. atlassian/bitbucket/cloud/repositories/commits.py +188 -0
  15. atlassian/bitbucket/cloud/repositories/defaultReviewers.py +94 -0
  16. atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py +264 -0
  17. atlassian/bitbucket/cloud/repositories/diffstat.py +92 -0
  18. atlassian/bitbucket/cloud/repositories/groupPermissions.py +81 -0
  19. atlassian/bitbucket/cloud/repositories/hooks.py +117 -0
  20. atlassian/bitbucket/cloud/repositories/issues.py +164 -0
  21. atlassian/bitbucket/cloud/repositories/pipelines.py +274 -0
  22. atlassian/bitbucket/cloud/repositories/pullRequests.py +483 -0
  23. atlassian/bitbucket/cloud/repositories/refs.py +136 -0
  24. atlassian/bitbucket/cloud/repositories/repositoryVariables.py +146 -0
  25. atlassian/bitbucket/cloud/workspaces/__init__.py +146 -0
  26. atlassian/bitbucket/cloud/workspaces/members.py +62 -0
  27. atlassian/bitbucket/cloud/workspaces/permissions.py +125 -0
  28. atlassian/bitbucket/cloud/workspaces/projects.py +215 -0
  29. atlassian/bitbucket/server/__init__.py +46 -0
  30. atlassian/bitbucket/server/base.py +63 -0
  31. atlassian/bitbucket/server/common/__init__.py +1 -0
  32. atlassian/bitbucket/server/common/permissions.py +220 -0
  33. atlassian/bitbucket/server/globalPermissions.py +198 -0
  34. atlassian/bitbucket/server/projects/__init__.py +244 -0
  35. atlassian/bitbucket/server/projects/repos/__init__.py +327 -0
  36. atlassian/cloud_admin.py +32 -0
  37. atlassian/confluence.py +3336 -0
  38. atlassian/crowd.py +266 -0
  39. atlassian/errors.py +27 -0
  40. atlassian/insight.py +678 -0
  41. atlassian/jira.py +5324 -0
  42. atlassian/marketplace.py +84 -0
  43. atlassian/portfolio.py +108 -0
  44. atlassian/request_utils.py +39 -0
  45. atlassian/rest_client.py +558 -0
  46. atlassian/service_desk.py +911 -0
  47. atlassian/statuspage.py +3133 -0
  48. atlassian/utils.py +327 -0
  49. atlassian/xray.py +632 -0
  50. atlassian_python_api-3.41.13.dist-info/LICENSE +201 -0
  51. atlassian_python_api-3.41.13.dist-info/METADATA +261 -0
  52. atlassian_python_api-3.41.13.dist-info/RECORD +54 -0
  53. atlassian_python_api-3.41.13.dist-info/WHEEL +5 -0
  54. atlassian_python_api-3.41.13.dist-info/top_level.txt +1 -0
@@ -0,0 +1,558 @@
1
+ # coding=utf-8
2
+ import logging
3
+ from json import dumps
4
+
5
+ import requests
6
+ from requests.adapters import HTTPAdapter
7
+
8
+ try:
9
+ from oauthlib.oauth1.rfc5849 import SIGNATURE_RSA_SHA512 as SIGNATURE_RSA
10
+ except ImportError:
11
+ from oauthlib.oauth1 import SIGNATURE_RSA
12
+ from requests import HTTPError
13
+ from requests_oauthlib import OAuth1, OAuth2
14
+ from six.moves.urllib.parse import urlencode
15
+ from urllib3.util import Retry
16
+
17
+ from atlassian.request_utils import get_default_logger
18
+
19
+ log = get_default_logger(__name__)
20
+
21
+
22
+ class AtlassianRestAPI(object):
23
+ default_headers = {
24
+ "Content-Type": "application/json",
25
+ "Accept": "application/json",
26
+ }
27
+ experimental_headers = {
28
+ "Content-Type": "application/json",
29
+ "Accept": "application/json",
30
+ "X-ExperimentalApi": "opt-in",
31
+ }
32
+ form_token_headers = {
33
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
34
+ "X-Atlassian-Token": "no-check",
35
+ }
36
+ no_check_headers = {"X-Atlassian-Token": "no-check"}
37
+ safe_mode_headers = {
38
+ "X-Atlassian-Token": "nocheck",
39
+ "Content-Type": "application/vnd.atl.plugins.safe.mode.flag+json",
40
+ }
41
+ experimental_headers_general = {
42
+ "X-Atlassian-Token": "no-check",
43
+ "X-ExperimentalApi": "opt-in",
44
+ }
45
+ response = None
46
+
47
+ def __init__(
48
+ self,
49
+ url,
50
+ username=None,
51
+ password=None,
52
+ timeout=75,
53
+ api_root="rest/api",
54
+ api_version="latest",
55
+ verify_ssl=True,
56
+ session=None,
57
+ oauth=None,
58
+ oauth2=None,
59
+ cookies=None,
60
+ advanced_mode=None,
61
+ kerberos=None,
62
+ cloud=False,
63
+ proxies=None,
64
+ token=None,
65
+ cert=None,
66
+ backoff_and_retry=False,
67
+ retry_status_codes=[413, 429, 503],
68
+ max_backoff_seconds=1800,
69
+ max_backoff_retries=1000,
70
+ ):
71
+ """
72
+ init function for the AtlassianRestAPI object.
73
+
74
+ :param url: The url to be used in the request.
75
+ :param username: Username. Defaults to None.
76
+ :param password: Password. Defaults to None.
77
+ :param timeout: Request timeout. Defaults to 75.
78
+ :param api_root: Root for the api requests. Defaults to "rest/api".
79
+ :param api_version: Version of the API to use. Defaults to "latest".
80
+ :param verify_ssl: Turn on / off SSL verification. Defaults to True.
81
+ :param session: Pass an existing Python requests session object. Defaults to None.
82
+ :param oauth: oauth. Defaults to None.
83
+ :param oauth2: oauth2. Defaults to None.
84
+ :param cookies: Cookies to send with the request. Defaults to None.
85
+ :param advanced_mode: Return results in advanced mode. Defaults to None.
86
+ :param kerberos: Kerberos. Defaults to None.
87
+ :param cloud: Specify if using Atlassian Cloud. Defaults to False.
88
+ :param proxies: Specify proxies to use. Defaults to None.
89
+ :param token: Atlassian / Jira auth token. Defaults to None.
90
+ :param cert: Client-side certificate to use. Defaults to None.
91
+ :param backoff_and_retry: Enable exponential backoff and retry.
92
+ This will retry the request if there is a predefined failure. Primarily
93
+ designed for Atlassian Cloud where API limits are commonly hit if doing
94
+ operations on many issues, and the limits require a cooling off period.
95
+ The wait period before the next request increases exponentially with each
96
+ failed retry. Defaults to False.
97
+ :param retry_status_codes: Errors to match, passed as a list of HTTP
98
+ response codes. Defaults to [413, 429, 503].
99
+ :param max_backoff_seconds: Max backoff seconds. When backing off, requests won't
100
+ wait any longer than this. Defaults to 1800.
101
+ :param max_backoff_retries: Maximum number of retries to try before
102
+ continuing. Defaults to 1000.
103
+ """
104
+ self.url = url
105
+ self.username = username
106
+ self.password = password
107
+ self.timeout = int(timeout)
108
+ self.verify_ssl = verify_ssl
109
+ self.api_root = api_root
110
+ self.api_version = api_version
111
+ self.cookies = cookies
112
+ self.advanced_mode = advanced_mode
113
+ self.cloud = cloud
114
+ self.proxies = proxies
115
+ self.cert = cert
116
+ if session is None:
117
+ self._session = requests.Session()
118
+ else:
119
+ self._session = session
120
+ if backoff_and_retry:
121
+ # Note: we only retry on status and not on any of the
122
+ # other supported reasons
123
+ retries = Retry(
124
+ total=None,
125
+ status=max_backoff_retries,
126
+ allowed_methods=None,
127
+ status_forcelist=retry_status_codes,
128
+ backoff_factor=1,
129
+ backoff_jitter=1,
130
+ backoff_max=max_backoff_seconds,
131
+ )
132
+ self._session.mount(self.url, HTTPAdapter(max_retries=retries))
133
+ if username and password:
134
+ self._create_basic_session(username, password)
135
+ elif token is not None:
136
+ self._create_token_session(token)
137
+ elif oauth is not None:
138
+ self._create_oauth_session(oauth)
139
+ elif oauth2 is not None:
140
+ self._create_oauth2_session(oauth2)
141
+ elif kerberos is not None:
142
+ self._create_kerberos_session(kerberos)
143
+ elif cookies is not None:
144
+ self._session.cookies.update(cookies)
145
+
146
+ def __enter__(self):
147
+ return self
148
+
149
+ def __exit__(self, *_):
150
+ self.close()
151
+
152
+ def _create_basic_session(self, username, password):
153
+ self._session.auth = (username, password)
154
+
155
+ def _create_token_session(self, token):
156
+ self._update_header("Authorization", "Bearer {token}".format(token=token.strip()))
157
+
158
+ def _create_kerberos_session(self, _):
159
+ from requests_kerberos import OPTIONAL, HTTPKerberosAuth
160
+
161
+ self._session.auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
162
+
163
+ def _create_oauth_session(self, oauth_dict):
164
+ oauth = OAuth1(
165
+ oauth_dict["consumer_key"],
166
+ rsa_key=oauth_dict["key_cert"],
167
+ signature_method=oauth_dict.get("signature_method", SIGNATURE_RSA),
168
+ resource_owner_key=oauth_dict["access_token"],
169
+ resource_owner_secret=oauth_dict["access_token_secret"],
170
+ )
171
+ self._session.auth = oauth
172
+
173
+ def _create_oauth2_session(self, oauth_dict):
174
+ """
175
+ Use OAuth 2.0 Authentication
176
+ :param oauth_dict: Dictionary containing access information. Must at
177
+ least contain "client_id" and "token". "token" is a dictionary and
178
+ must at least contain "access_token" and "token_type".
179
+ :return:
180
+ """
181
+ if "client" not in oauth_dict:
182
+ oauth_dict["client"] = None
183
+ oauth = OAuth2(oauth_dict["client_id"], oauth_dict["client"], oauth_dict["token"])
184
+ self._session.auth = oauth
185
+
186
+ def _update_header(self, key, value):
187
+ """
188
+ Update header for exist session
189
+ :param key:
190
+ :param value:
191
+ :return:
192
+ """
193
+ self._session.headers.update({key: value})
194
+
195
+ @staticmethod
196
+ def _response_handler(response):
197
+ try:
198
+ return response.json()
199
+ except ValueError:
200
+ log.debug("Received response with no content")
201
+ return None
202
+ except Exception as e:
203
+ log.error(e)
204
+ return None
205
+
206
+ def log_curl_debug(self, method, url, data=None, headers=None, level=logging.DEBUG):
207
+ """
208
+
209
+ :param method:
210
+ :param url:
211
+ :param data:
212
+ :param headers:
213
+ :param level:
214
+ :return:
215
+ """
216
+ headers = headers or self.default_headers
217
+ message = "curl --silent -X {method} -H {headers} {data} '{url}'".format(
218
+ method=method,
219
+ headers=" -H ".join(["'{0}: {1}'".format(key, value) for key, value in headers.items()]),
220
+ data="" if not data else "--data '{0}'".format(dumps(data)),
221
+ url=url,
222
+ )
223
+ log.log(level=level, msg=message)
224
+
225
+ def resource_url(self, resource, api_root=None, api_version=None):
226
+ if api_root is None:
227
+ api_root = self.api_root
228
+ if api_version is None:
229
+ api_version = self.api_version
230
+ return "/".join(str(s).strip("/") for s in [api_root, api_version, resource] if s is not None)
231
+
232
+ @staticmethod
233
+ def url_joiner(url, path, trailing=None):
234
+ url_link = "/".join(str(s).strip("/") for s in [url, path] if s is not None)
235
+ if trailing:
236
+ url_link += "/"
237
+ return url_link
238
+
239
+ def close(self):
240
+ return self._session.close()
241
+
242
+ def request(
243
+ self,
244
+ method="GET",
245
+ path="/",
246
+ data=None,
247
+ json=None,
248
+ flags=None,
249
+ params=None,
250
+ headers=None,
251
+ files=None,
252
+ trailing=None,
253
+ absolute=False,
254
+ advanced_mode=False,
255
+ ):
256
+ """
257
+
258
+ :param method:
259
+ :param path:
260
+ :param data:
261
+ :param json:
262
+ :param flags:
263
+ :param params:
264
+ :param headers:
265
+ :param files:
266
+ :param trailing: bool - OPTIONAL: Add trailing slash to url
267
+ :param absolute: bool, OPTIONAL: Do not prefix url, url is absolute
268
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
269
+ :return:
270
+ """
271
+ url = self.url_joiner(None if absolute else self.url, path, trailing)
272
+ params_already_in_url = True if "?" in url else False
273
+ if params or flags:
274
+ if params_already_in_url:
275
+ url += "&"
276
+ else:
277
+ url += "?"
278
+ if params:
279
+ url += urlencode(params or {})
280
+ if flags:
281
+ url += ("&" if params or params_already_in_url else "") + "&".join(flags or [])
282
+ json_dump = None
283
+ if files is None:
284
+ data = None if not data else dumps(data)
285
+ json_dump = None if not json else dumps(json)
286
+ self.log_curl_debug(
287
+ method=method,
288
+ url=url,
289
+ headers=headers,
290
+ data=data if data else json_dump,
291
+ )
292
+ headers = headers or self.default_headers
293
+ response = self._session.request(
294
+ method=method,
295
+ url=url,
296
+ headers=headers,
297
+ data=data,
298
+ json=json,
299
+ timeout=self.timeout,
300
+ verify=self.verify_ssl,
301
+ files=files,
302
+ proxies=self.proxies,
303
+ cert=self.cert,
304
+ )
305
+ response.encoding = "utf-8"
306
+
307
+ log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason)
308
+ log.debug("HTTP: Response text -> %s", response.text)
309
+ if self.advanced_mode or advanced_mode:
310
+ return response
311
+
312
+ self.raise_for_status(response)
313
+ return response
314
+
315
+ def get(
316
+ self,
317
+ path,
318
+ data=None,
319
+ flags=None,
320
+ params=None,
321
+ headers=None,
322
+ not_json_response=None,
323
+ trailing=None,
324
+ absolute=False,
325
+ advanced_mode=False,
326
+ ):
327
+ """
328
+ Get request based on the python-requests module. You can override headers, and also, get not json response
329
+ :param path:
330
+ :param data:
331
+ :param flags:
332
+ :param params:
333
+ :param headers:
334
+ :param not_json_response: OPTIONAL: For get content from raw request's packet
335
+ :param trailing: OPTIONAL: for wrap slash symbol in the end of string
336
+ :param absolute: bool, OPTIONAL: Do not prefix url, url is absolute
337
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
338
+ :return:
339
+ """
340
+ response = self.request(
341
+ "GET",
342
+ path=path,
343
+ flags=flags,
344
+ params=params,
345
+ data=data,
346
+ headers=headers,
347
+ trailing=trailing,
348
+ absolute=absolute,
349
+ advanced_mode=advanced_mode,
350
+ )
351
+ if self.advanced_mode or advanced_mode:
352
+ return response
353
+ if not_json_response:
354
+ return response.content
355
+ else:
356
+ if not response.text:
357
+ return None
358
+ try:
359
+ return response.json()
360
+ except Exception as e:
361
+ log.error(e)
362
+ return response.text
363
+
364
+ def post(
365
+ self,
366
+ path,
367
+ data=None,
368
+ json=None,
369
+ headers=None,
370
+ files=None,
371
+ params=None,
372
+ trailing=None,
373
+ absolute=False,
374
+ advanced_mode=False,
375
+ ):
376
+ """
377
+ :param path:
378
+ :param data:
379
+ :param json:
380
+ :param headers:
381
+ :param files:
382
+ :param params:
383
+ :param trailing:
384
+ :param absolute:
385
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
386
+ :return: if advanced_mode is not set - returns dictionary. If it is set - returns raw response.
387
+ """
388
+ response = self.request(
389
+ "POST",
390
+ path=path,
391
+ data=data,
392
+ json=json,
393
+ headers=headers,
394
+ files=files,
395
+ params=params,
396
+ trailing=trailing,
397
+ absolute=absolute,
398
+ advanced_mode=advanced_mode,
399
+ )
400
+ if self.advanced_mode or advanced_mode:
401
+ return response
402
+ return self._response_handler(response)
403
+
404
+ def put(
405
+ self,
406
+ path,
407
+ data=None,
408
+ headers=None,
409
+ files=None,
410
+ trailing=None,
411
+ params=None,
412
+ absolute=False,
413
+ advanced_mode=False,
414
+ ):
415
+ """
416
+ :param path: Path of request
417
+ :param data:
418
+ :param headers: adjusted headers, usually it's default
419
+ :param files:
420
+ :param trailing:
421
+ :param params:
422
+ :param absolute:
423
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
424
+ :return: if advanced_mode is not set - returns dictionary. If it is set - returns raw response.
425
+ """
426
+ response = self.request(
427
+ "PUT",
428
+ path=path,
429
+ data=data,
430
+ headers=headers,
431
+ files=files,
432
+ params=params,
433
+ trailing=trailing,
434
+ absolute=absolute,
435
+ advanced_mode=advanced_mode,
436
+ )
437
+ if self.advanced_mode or advanced_mode:
438
+ return response
439
+ return self._response_handler(response)
440
+
441
+ """
442
+ Partial modification of resource by PATCH Method
443
+ LINK: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
444
+ """
445
+
446
+ def patch(
447
+ self,
448
+ path,
449
+ data=None,
450
+ headers=None,
451
+ files=None,
452
+ trailing=None,
453
+ params=None,
454
+ absolute=False,
455
+ advanced_mode=False,
456
+ ):
457
+ """
458
+ :param path: Path of request
459
+ :param data:
460
+ :param headers: adjusted headers, usually it's default
461
+ :param files:
462
+ :param trailing:
463
+ :param params:
464
+ :param absolute:
465
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
466
+ :return: if advanced_mode is not set - returns dictionary. If it is set - returns raw response.
467
+ """
468
+ response = self.request(
469
+ "PATCH",
470
+ path=path,
471
+ data=data,
472
+ headers=headers,
473
+ files=files,
474
+ params=params,
475
+ trailing=trailing,
476
+ absolute=absolute,
477
+ advanced_mode=advanced_mode,
478
+ )
479
+ if self.advanced_mode or advanced_mode:
480
+ return response
481
+ return self._response_handler(response)
482
+
483
+ def delete(
484
+ self,
485
+ path,
486
+ data=None,
487
+ headers=None,
488
+ params=None,
489
+ trailing=None,
490
+ absolute=False,
491
+ advanced_mode=False,
492
+ ):
493
+ """
494
+ Deletes resources at given paths.
495
+ :param path:
496
+ :param data:
497
+ :param headers:
498
+ :param params:
499
+ :param trailing:
500
+ :param absolute:
501
+ :param advanced_mode: bool, OPTIONAL: Return the raw response
502
+ :rtype: dict
503
+ :return: Empty dictionary to have consistent interface.
504
+ Some of Atlassian REST resources don't return any content.
505
+ If advanced_mode is set - returns raw response.
506
+ """
507
+ response = self.request(
508
+ "DELETE",
509
+ path=path,
510
+ data=data,
511
+ headers=headers,
512
+ params=params,
513
+ trailing=trailing,
514
+ absolute=absolute,
515
+ advanced_mode=advanced_mode,
516
+ )
517
+ if self.advanced_mode or advanced_mode:
518
+ return response
519
+ return self._response_handler(response)
520
+
521
+ def raise_for_status(self, response):
522
+ """
523
+ Checks the response for errors and throws an exception if return code >= 400
524
+ Since different tools (Atlassian, Jira, ...) have different formats of returned json,
525
+ this method is intended to be overwritten by a tool specific implementation.
526
+ :param response:
527
+ :return:
528
+ """
529
+ if response.status_code == 401 and response.headers.get("Content-Type") != "application/json;charset=UTF-8":
530
+ raise HTTPError("Unauthorized (401)", response=response)
531
+
532
+ if 400 <= response.status_code < 600:
533
+ try:
534
+ j = response.json()
535
+ if self.url == "https://api.atlassian.com":
536
+ error_msg = "\n".join(["{}: {}".format(k, v) for k, v in j.items()])
537
+ else:
538
+ error_msg_list = j.get("errorMessages", list())
539
+ errors = j.get("errors", dict())
540
+ if isinstance(errors, dict) and "message" not in errors:
541
+ error_msg_list.extend(errors.values())
542
+ elif isinstance(errors, dict) and "message" in errors:
543
+ error_msg_list.append(errors.get("message", ""))
544
+ elif isinstance(errors, list):
545
+ error_msg_list.extend([v.get("message", "") if isinstance(v, dict) else v for v in errors])
546
+ error_msg = "\n".join(error_msg_list)
547
+ except Exception as e:
548
+ log.error(e)
549
+ response.raise_for_status()
550
+ else:
551
+ raise HTTPError(error_msg, response=response)
552
+ else:
553
+ response.raise_for_status()
554
+
555
+ @property
556
+ def session(self):
557
+ """Providing access to the restricted field"""
558
+ return self._session