1
1
import json
2
+ import logging
2
3
import requests
3
4
4
5
5
- class Atlassian :
6
+ logging .basicConfig (level = logging .INFO , format = "[%(asctime).19s] [%(levelname)s] %(message)s" )
7
+ logging .getLogger ("requests" ).setLevel (logging .WARNING )
8
+ log = logging .getLogger ("atlassian" )
9
+
10
+
11
+ class AtlassianRestAPI :
6
12
7
13
def __init__ (self , url , username , password ):
8
14
self .url = url
9
15
self .username = username
10
16
self .password = password
11
17
12
- def get (self , path , headers = {"Content-Type" : "application/json" , "Accept" : "application/json" }):
13
- url = "{0}{1}" .format (self .url , path )
14
- return requests .get (url , headers = headers , auth = (self .username , self .password ), timeout = 60 )
15
-
16
- def post (self , path , data = None , headers = {"Content-Type" : "application/json" , "Accept" : "application/json" }):
17
- url = "{0}{1}" .format (self .url , path )
18
- return requests .post (url , json .dumps (data ), headers = headers , auth = (self .username , self .password ))
19
-
20
- def put (self , path , data = None , headers = {"Content-Type" : "application/json" , "Accept" : "application/json" }):
21
- url = "{0}{1}" .format (self .url , path )
22
- return requests .put (url , json .dumps (data ), headers = headers , auth = (self .username , self .password ))
23
-
24
- def delete (self , path , headers = {"Content-Type" : "application/json" , "Accept" : "application/json" }):
25
- url = "{0}{1}" .format (self .url , path )
26
- return requests .delete (url , headers = headers , auth = (self .username , self .password ))
18
+ def log_curl_debug (self , method , path , headers = {}, data = None ):
19
+ command = "curl --silent -X {method} -u '{username}':'{password}' -H {headers} {data} '{url}'" .format (
20
+ method = method ,
21
+ username = self .username ,
22
+ password = self .password ,
23
+ headers = ' -H ' .join (["'{0}: {1}'" .format (key , value ) for key , value in headers .items ()]),
24
+ data = '' if not data else "--data '{0}'" .format (json .dumps (data )),
25
+ url = '{0}{1}' .format (self .url , path ))
26
+ log .debug (command )
27
+
28
+ def request (self , method = 'GET' , path = '/' , headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }, data = None ):
29
+ self .log_curl_debug (method , path , headers , data )
30
+ response = requests .request (
31
+ method = method ,
32
+ url = '{0}{1}' .format (self .url , path ),
33
+ headers = headers ,
34
+ data = json .dumps (data ),
35
+ auth = (self .username , self .password ),
36
+ timeout = 60 )
37
+ response .raise_for_status ()
38
+ log .debug ('Received: {0}' .format (response .json ()))
39
+ return response
40
+
41
+ def get (self , path , headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }):
42
+ return self .request ('GET' , path , headers ).json ()
43
+
44
+ def post (self , path , headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }, data = None ):
45
+ return self .request ('POST' , path , headers , data ).json ()
46
+
47
+ def put (self , path , headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }, data = None ):
48
+ return self .request ('PUT' , path , headers , data ).json ()
49
+
50
+ def delete (self , path , headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }):
51
+ return self .request ('DELETE' , path , headers ).json ()
27
52
28
53
29
54
from .confluence import Confluence
30
- from .jira import Jira
55
+ from .jira import Jira
56
+ from .stash import Stash
0 commit comments