Skip to content

Commit 2bb682c

Browse files
authored
Allow timeout to be defined for requests (#1)
* Allow a timeout to be provided to Python requests
1 parent ea2a555 commit 2bb682c

File tree

2 files changed

+79
-52
lines changed

2 files changed

+79
-52
lines changed

akamai/netstorage/netstorage.py

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ class NetstorageError(Exception):
3939

4040

4141
class Netstorage:
42-
def __init__(self, hostname, keyname, key, ssl=False):
42+
def __init__(self, hostname, keyname, key, ssl=False, timeout=None):
4343
if not (hostname and keyname and key):
4444
raise NetstorageError('[NetstorageError] You should input netstorage hostname, keyname and key all')
4545

4646
self.hostname = hostname
4747
self.keyname = keyname
4848
self.key = key
4949
self.ssl = 's' if ssl else ''
50+
self.timeout = timeout
5051
self.http_client = requests.Session()
5152

5253

@@ -105,102 +106,117 @@ def _request(self, **kwargs):
105106
'Accept-Encoding': 'identity',
106107
'User-Agent': 'NetStorageKit-Python'
107108
}
109+
110+
timeout = kwargs['timeout'] if 'timeout' in kwargs else self.timeout
108111

109112
response = None
110113
if kwargs['method'] == 'GET':
111114
if kwargs['action'] == 'download':
112-
response = self.http_client.get(request_url, headers=headers, stream=True)
115+
response = self.http_client.get(request_url, headers=headers, timeout=timeout, stream=True)
113116
if 'stream' not in kwargs.keys():
114117
self._download_data_from_response(response, kwargs['path'], kwargs['destination'])
115118
else:
116-
response = self.http_client.get(request_url, headers=headers)
119+
response = self.http_client.get(request_url, headers=headers, timeout=timeout)
117120

118121
elif kwargs['method'] == 'POST':
119-
response = self.http_client.post(request_url, headers=headers)
122+
response = self.http_client.post(request_url, headers=headers, timeout=timeout)
120123

121124
elif kwargs['method'] == 'PUT': # Use only upload
122125
if 'stream' in kwargs.keys():
123-
response = self.http_client.put(request_url, headers=headers, data=kwargs['stream'])
126+
response = self.http_client.put(request_url, headers=headers, timeout=timeout, data=kwargs['stream'])
124127
elif kwargs['action'].startswith('upload'):
125128
mmapped_data = self._upload_data_to_request(kwargs['source'])
126-
response = self.http_client.put(request_url, headers=headers, data=mmapped_data)
129+
response = self.http_client.put(request_url, headers=headers, timeout=timeout, data=mmapped_data)
127130
if not isinstance(mmapped_data, str):
128131
mmapped_data.close()
129132

130133
return response.status_code == 200, response
131134

132-
def dir(self, ns_path, option={}):
135+
def dir(self, ns_path, option={}, timeout=None):
133136
option = "dir&format=xml&{0}".format(urlencode(option))
134137
return self._request(action=option,
135138
method='GET',
136-
path=ns_path)
139+
path=ns_path,
140+
timeout=timeout)
137141

138-
def list(self, ns_path, option={}):
142+
def list(self, ns_path, option={}, timeout=None):
139143
option = "list&format=xml&{0}".format(urlencode(option))
140144
return self._request(action=option,
141145
method='GET',
142-
path=ns_path)
146+
path=ns_path,
147+
timeout=timeout)
143148

144-
def download(self, ns_source, local_destination=''):
149+
def download(self, ns_source, local_destination='', timeout=None):
145150
if ns_source.endswith('/'):
146151
raise NetstorageError("[NetstorageError] Nestorage download path shouldn't be a directory: {0}".format(ns_source))
147152
return self._request(action='download',
148153
method='GET',
149154
path=ns_source,
150-
destination=local_destination)
155+
destination=local_destination,
156+
timeout=timeout)
151157

152-
def stream_download(self, ns_source):
158+
def stream_download(self, ns_source, timeout=None):
153159
return self._request(action='download',
154160
method='GET',
155161
path=ns_source,
156-
stream=True)
162+
stream=True,
163+
timeout=timeout)
157164

158-
def du(self, ns_path):
165+
def du(self, ns_path, timeout=None):
159166
return self._request(action='du&format=xml',
160167
method='GET',
161-
path=ns_path)
168+
path=ns_path,
169+
timeout=timeout)
162170

163-
def stat(self, ns_path):
171+
def stat(self, ns_path, timeout=None):
164172
return self._request(action='stat&format=xml',
165173
method='GET',
166-
path=ns_path)
174+
path=ns_path,
175+
timeout=timeout)
167176

168-
def mkdir(self, ns_path):
177+
def mkdir(self, ns_path, timeout=None):
169178
return self._request(action='mkdir',
170179
method='POST',
171-
path=ns_path)
180+
path=ns_path,
181+
timeout=timeout)
172182

173-
def rmdir(self, ns_path):
183+
def rmdir(self, ns_path, timeout=None):
174184
return self._request(action='rmdir',
175185
method='POST',
176-
path=ns_path)
186+
path=ns_path,
187+
timeout=timeout)
177188

178-
def mtime(self, ns_path, mtime):
189+
def mtime(self, ns_path, mtime, timeout=None):
179190
return self._request(action='mtime&format=xml&mtime={0}'.format(mtime),
180191
method='POST',
181-
path=ns_path)
192+
path=ns_path,
193+
timeout=timeout)
182194

183-
def delete(self, ns_path):
195+
def delete(self, ns_path, timeout=None):
184196
return self._request(action='delete',
185197
method='POST',
186-
path=ns_path)
198+
path=ns_path,
199+
timeout=timeout)
187200

188-
def quick_delete(self, ns_path):
201+
def quick_delete(self, ns_path, timeout=None):
189202
return self._request(action='quick-delete&quick-delete=imreallyreallysure',
190203
method='POST',
191-
path=ns_path)
204+
path=ns_path,
205+
timeout=timeout)
192206

193-
def rename(self, ns_target, ns_destination):
207+
def rename(self, ns_target, ns_destination, timeout=None):
194208
return self._request(action='rename&destination={0}'.format(quote_plus(ns_destination)),
195209
method='POST',
196-
path=ns_target)
210+
path=ns_target,
211+
timeout=timeout)
197212

198-
def symlink(self, ns_target, ns_destination):
213+
def symlink(self, ns_target, ns_destination, timeout=None):
199214
return self._request(action='symlink&target={0}'.format(quote_plus(ns_target)),
200215
method='POST',
201-
path=ns_destination)
216+
path=ns_destination,
217+
timeout=timeout)
202218

203-
def upload(self, local_source, ns_destination, index_zip=False):
219+
def upload(self, local_source, ns_destination, index_zip=False, timeout=None):
204220
if os.path.isfile(local_source):
205221
if ns_destination.endswith('/'):
206222
ns_destination = "{0}{1}".format(ns_destination, ntpath.basename(local_source))
@@ -214,10 +230,12 @@ def upload(self, local_source, ns_destination, index_zip=False):
214230
return self._request(action=action,
215231
method='PUT',
216232
source=local_source,
217-
path=ns_destination)
233+
path=ns_destination,
234+
timeout=timeout)
218235

219-
def stream_upload(self, data, ns_destination):
236+
def stream_upload(self, data, ns_destination, timeout=None):
220237
return self._request(action='upload',
221238
method='PUT',
222239
stream=data,
223-
path=ns_destination)
240+
path=ns_destination,
241+
timeout=timeout)

cms_netstorage.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def print_result(response, action):
6969
rmdir: to delete /123456/dir1 (directory needs to be empty)
7070
rmdir /123456/dir1
7171
'''
72-
usage = 'Usage: python cms_netstorage.py -H [hostname] -k [keyname] -K [key] -a [action_options] ..'
72+
usage = 'Usage: python cms_netstorage.py -H [hostname] -k [keyname] -K [key] -t [timeout] -s [use ssl] -a [action_options] ..'
7373
parser = NetstorageParser(usage=usage, epilog=action_list)
7474

7575
parser.add_option(
@@ -83,49 +83,58 @@ def print_result(response, action):
8383
help='Netstorage API key ex) xxxxxxxxxxxxx')
8484
parser.add_option(
8585
'-a', '--action', dest='action')
86+
parser.add_option(
87+
'-s', '--ssl', dest='ssl')
88+
parser.add_option(
89+
'-t', '--timeout', dest='timeout')
8690

8791
(options, args) = parser.parse_args()
8892

8993
if options.hostname and options.keyname and options.key and options.action:
90-
ns = Netstorage(options.hostname, options.keyname, options.key)
94+
ssl = options.ssl if hasattr(options, 'ssl') else False
95+
timeout = options.timeout if hasattr(options, 'timeout') else None
96+
ns = Netstorage(options.hostname, options.keyname, options.key, ssl, timeout)
97+
98+
def _arg(key, default=None):
99+
return args[key] if key in args else default
91100

92101
try:
93102
res = None
94103
if options.action == 'delete':
95-
ok, res = ns.delete(args[0])
104+
ok, res = ns.delete(args[0], _arg(1))
96105
elif options.action == 'dir':
97106
if len(args) >= 2:
98-
ok, res = ns.dir(args[0], ast.literal_eval(args[1]))
107+
ok, res = ns.dir(args[0], ast.literal_eval(args[1]), _arg(2))
99108
else:
100109
ok, res = ns.dir(args[0])
101110
elif options.action == 'list':
102111
if len(args) >= 2:
103-
ok, res = ns.list(args[0], ast.literal_eval(args[1]))
112+
ok, res = ns.list(args[0], ast.literal_eval(args[1]), _arg(2))
104113
else:
105114
ok, res = ns.list(args[0])
106115
elif options.action == 'download':
107-
ok, res = ns.download(args[0], args[1])
116+
ok, res = ns.download(args[0], args[1], _arg(2))
108117
elif options.action == 'du':
109-
ok, res = ns.du(args[0])
118+
ok, res = ns.du(args[0], _arg(1))
110119
elif options.action == 'mkdir':
111-
ok, res = ns.mkdir(args[0])
120+
ok, res = ns.mkdir(args[0], _arg(1))
112121
elif options.action == 'mtime':
113-
ok, res = ns.mtime(args[0], args[1])
122+
ok, res = ns.mtime(args[0], args[1], _arg(2))
114123
elif options.action == 'quick-delete':
115-
ok, res = ns.quick_delete(args[0])
124+
ok, res = ns.quick_delete(args[0], _arg(1))
116125
elif options.action == 'rmdir':
117-
ok, res = ns.rmdir(args[0])
126+
ok, res = ns.rmdir(args[0], _arg(1))
118127
elif options.action == 'stat':
119-
ok, res = ns.stat(args[0])
128+
ok, res = ns.stat(args[0], _arg(1))
120129
elif options.action == 'symlink':
121-
ok, res = ns.symlink(args[0], args[1])
130+
ok, res = ns.symlink(args[0], args[1], _arg(2))
122131
elif options.action == 'upload':
123132
if len(args) >= 3:
124-
ok, res = ns.upload(args[0], args[1], args[2])
133+
ok, res = ns.upload(args[0], args[1], args[2], _arg(3))
125134
else:
126135
ok, res = ns.upload(args[0], args[1])
127136
elif options.action == 'rename':
128-
ok, res = ns.rename(args[0], args[1])
137+
ok, res = ns.rename(args[0], args[1], _arg(1))
129138
else:
130139
print("Invalid action.\nUse option -h or --help")
131140
exit()
@@ -134,7 +143,7 @@ def print_result(response, action):
134143

135144
except IndexError as e:
136145
if options.action == 'download' and args[0]:
137-
ok, res = ns.download(args[0])
146+
ok, res = ns.download(args[0], '', _arg(1))
138147
print_result(res, options.action)
139148
else:
140149
print("Invalid argument.\n")

0 commit comments

Comments
 (0)