OSM package API handling
"""
-import tarfile
-import re
-import yaml
#from os import stat
#from os.path import basename
from osmclient.common.exceptions import ClientException
-from osmclient.common.exceptions import NotFound
from osmclient.common import utils
+import json
class Package(object):
#headers['Content-Range'] = 'bytes 0-{}/{}'.format(file_size - 1, file_size)
headers["Content-File-MD5"] = utils.md5(filename)
http_header = ['{}: {}'.format(key,val)
- for (key,val) in headers.items()]
+ for (key,val) in list(headers.items())]
self._http.set_http_header(http_header)
- resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
- #print 'RESP: {}'.format(yaml.safe_dump(resp))
- if not resp or 'id' not in resp:
- raise ClientException("failed to upload package")
+ http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
+ #print 'HTTP CODE: {}'.format(http_code)
+ #print 'RESP: {}'.format(resp)
+ if http_code in (200, 201, 202, 204):
+ if resp:
+ resp = json.loads(resp)
+ if not resp or 'id' not in resp:
+ raise ClientException('unexpected response from server - {}'.format(
+ resp))
+ print(resp['id'])
else:
- print resp['id']
-
+ msg = ""
+ if resp:
+ try:
+ msg = json.loads(resp)
+ except ValueError:
+ msg = resp
+ raise ClientException("failed to upload package - {}".format(msg))