1+ '''
2+ * * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3+ * * See LICENSE in the project root for license information.
4+ '''
5+
6+ import json
7+ import requests
8+
9+ class HttpRequestFailed (Exception ):
10+ def __init__ (self , request , response ):
11+ self ._request = request
12+ self ._response = response
13+
14+ @property
15+ def response (self ):
16+ return self ._request
17+
18+ @property
19+ def response (self ):
20+ return self ._response
21+
22+ class RestApiService (object ):
23+
24+ def get_raw (self , url , token , headers = None ):
25+ method = 'GET'
26+ s_headers = {}
27+ self ._set_header_token (s_headers , token )
28+ if headers :
29+ s_headers .update (headers )
30+ response = self ._send (method , url , s_headers )
31+ return response .text
32+
33+ def get_json (self , url , token , headers = None ):
34+ method = 'GET'
35+ s_headers = {'Accept' : 'application/json' ,
36+ 'Content-Type' : 'application/json' }
37+ self ._set_header_token (s_headers , token )
38+ if headers :
39+ s_headers .update (headers )
40+ response = self ._send (method , url , s_headers )
41+ return json .loads (response .text )
42+
43+ def get_img (self , url , token , headers = None ):
44+ method = 'GET'
45+ s_headers = {'content-type' : 'image/jpeg' }
46+ self ._set_header_token (s_headers , token )
47+ if headers :
48+ s_headers .update (headers )
49+ response = self ._send (method , url , s_headers )
50+ return response .content
51+
52+ def get_object_list (self , url , token , key = 'value' , headers = None , model = None , next_key = '' ):
53+ content = self .get_json (url , token , headers )
54+ entity_list = []
55+ next_link = ''
56+ if content and model :
57+ value_list = content [key ]
58+ for value in value_list :
59+ entity = model (value )
60+ entity_list .append (entity )
61+ if next_key :
62+ next_link = content .get (next_key , '' )
63+ if next_key :
64+ return entity_list , next_link
65+ else :
66+ return entity_list
67+
68+ def get_object (self , url , token , headers = None , model = None ):
69+ content = self .get_raw (url , token , headers )
70+ if content and model :
71+ value = json .loads (content )
72+ return model (value )
73+ return None
74+
75+ def delete (self , url , token , headers = None ):
76+ method = 'DELETE'
77+ s_headers = {'Accept' : 'application/json' ,
78+ 'Content-Type' : 'application/json' }
79+ self ._set_header_token (s_headers , token )
80+ if headers :
81+ s_headers .update (headers )
82+ self ._send (method , url , s_headers )
83+
84+ def post_json (self , url , token , headers = None , data = None ):
85+ method = 'POST'
86+ s_headers = {'Accept' : 'application/json' ,
87+ 'Content-Type' : 'application/json' }
88+ self ._set_header_token (s_headers , token )
89+ if headers :
90+ s_headers .update (headers )
91+ return self ._send (method , url , s_headers , json .dumps (data ))
92+
93+ def put_file (self , url , token , file = None ):
94+ s_headers = {'Content-Type' : 'application/octet-stream' }
95+ self ._set_header_token (s_headers , token )
96+ method = 'PUT'
97+ return json .loads (self ._send (method , url , s_headers ,file .chunks ()).text )
98+
99+ def _set_header_token (self , headers , token ):
100+ key = 'Authorization'
101+ value = 'Bearer {0}' .format (token )
102+ headers [key ] = value
103+
104+ def _send (self , method , url , headers , data = None ):
105+ session = requests .Session ()
106+ request = requests .Request (method , url , headers , data = data )
107+ prepped = request .prepare ()
108+ response = session .send (prepped )
109+ if response .status_code < 200 or response .status_code > 299 :
110+ raise HttpRequestFailed (request , response )
111+ return response
112+
0 commit comments