3
3
"""
4
4
5
5
import logging
6
+ import os
6
7
import sys
8
+ import tempfile
7
9
8
10
import requests
9
11
@@ -88,13 +90,14 @@ def _handle_error(operation, exc):
88
90
sys .exit (1 )
89
91
90
92
91
- def get (url , params = None ): # type: (str, dict) -> requests.Response
93
+ def get (url , params = None , stream = False ):
94
+ # type: (str, dict, bool) -> requests.Response
92
95
"""Make GET request and handle errors."""
93
96
LOG .debug ('GET %s' , url )
94
97
95
98
try :
96
99
rsp = requests .get (url , auth = _get_auth (), headers = _get_headers (),
97
- params = params )
100
+ params = params , stream = stream )
98
101
rsp .raise_for_status ()
99
102
except requests .exceptions .RequestException as exc :
100
103
_handle_error ('fetch' , exc )
@@ -120,6 +123,31 @@ def put(url, data): # type: (str, dict) -> requests.Response
120
123
return rsp
121
124
122
125
126
+ def download (url , params = None ): # type: (str, dict) -> None
127
+ """Retrieve a specific API resource and save it to a file.
128
+
129
+ GET /{resource}/{resourceID}/
130
+
131
+ Arguments:
132
+ resource_type (str): The resource endpoint name.
133
+ resource_id (int/str): The ID for the specific resource.
134
+ params (dict/list): Additional parameters.
135
+
136
+ Returns:
137
+ A path to an output file containing the content.
138
+ """
139
+ output_fd , output_path = tempfile .mkstemp (suffix = '.patch' )
140
+
141
+ rsp = get (url , params , stream = True )
142
+ with os .fdopen (output_fd , 'w' ) as output_file :
143
+ LOG .debug ('Saving to %s' , output_path )
144
+ # we use iter_content because patches can be binary
145
+ for block in rsp .iter_content (1024 ):
146
+ output_file .write (block )
147
+
148
+ return output_path
149
+
150
+
123
151
def index (resource_type , params = None ): # type: (str, dict) -> dict
124
152
"""List API resources.
125
153
@@ -164,7 +192,7 @@ def detail(resource_type, resource_id, params=None):
164
192
url = '/' .join ([_get_server (), 'api' , resource_type ,
165
193
str (resource_id ), '' ])
166
194
167
- return get (url , params ).json ()
195
+ return get (url , params , stream = False ).json ()
168
196
169
197
170
198
def update (resource_type , resource_id , data ):
0 commit comments