def post_bindata(self, resource, entity, sfile, headers=None): conn = self.conn() if isinstance(resource, str): resource = [resource] url = self._apiEndpoint + '/'.join(resource) + '/' + entity hdrs = headers or self.headers vprint('POST', url, '@' + sfile.name, hdrs) curlprint(self.host, 'POST', url, headers=hdrs, file=sfile.name, fileMode='binary') conn.request('POST', url, sfile, hdrs) response = conn.getresponse() vprint('Status: ', response.status) if (response.status == 303): newurl = response.getheader('Location') conn = self.conn() conn.request('GET', newurl, '', self.headers) response = conn.getresponse() if (response.status != 200): print response.status return False else: return json.loads(response.read()) elif (response.status == 200): return json.loads(response.read()) else: print response.status return False
def post(self, resource, body): conn = self.conn() if isinstance(resource, str): resource = [resource] url = self._apiEndpoint + '/'.join(resource) jbody = json.dumps(body) vprint('POST', url, jbody, self.headers) curlprint(self.host, 'POST', url, headers=self.headers, data=jbody) conn.request('POST', url, jbody, self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status == 303): newurl = response.getheader('Location') conn = self.conn() conn.request('GET', newurl, '', self.headers) response = conn.getresponse() if (response.status != 200): print response.status return False else: return json.loads(response.read()) elif (response.status == 200): return json.loads(response.read()) else: print response.status return False
def list(self, resource): conn = self.conn() vprint('GET', self._apiEndpoint + resource, '', self.headers) curlprint(self.host, 'GET', self._apiEndpoint + resource, headers=self.headers) conn.request('GET', self._apiEndpoint + resource, '', self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status != 200): return False else: return json.loads(response.read())
def delete(self, resource, entity): conn = self.conn() vprint('DELETE', self._apiEndpoint + resource + '/' + entity, '', self.headers) curlprint(self.host, 'DELETE', self._apiEndpoint + resource + '/' + entity, self.headers) conn.request('DELETE', self._apiEndpoint + resource + '/' + entity, '', self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status >= 200 and response.status < 300): return True else: return False
def delete_body(self, resource, entity, body): conn = self.conn() jbody = json.dumps(body) vprint('DELETE', self._apiEndpoint + resource + '/' + entity, jbody, self.headers) curlprint(self.host, 'DELETE', self._apiEndpoint + resource + '/' + entity, headers=self.headers, data=jbody) conn.request('DELETE', self._apiEndpoint + resource + '/' + entity, jbody, self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status >= 200 and response.status < 300): return True else: return False
def get_attr(self, resource, entity, attr): conn = self.conn() if isinstance(attr, str): attr = [attr] url = self._apiEndpoint + resource + '/' + entity + '/' + '/'.join(attr) vprint('GET', url, '', self.headers) curlprint(self.host, 'GET', url, headers=self.headers) conn.request('GET', url, '', self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status != 200): return False else: return json.loads(response.read())
def delete_attr(self, resource, entity, attr): conn = self.conn() if isinstance(attr, str): attr = [attr] url = self._apiEndpoint + resource + '/' + entity + '/' + '/'.join(attr) vprint('DELETE', url, '', self.headers) curlprint(self.host, 'DELETE', url, headers=self.headers) conn.request('DELETE', url, '', self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status < 300 and response.status >= 300): return False else: return True
def get(self, resource, entity, suffix='', rawResponse=False, headers=None): conn = self.conn() hdrs = headers or self.headers vprint('GET', self._apiEndpoint + resource + '/' + entity + suffix, '', hdrs) curlprint(self.host, 'GET', self._apiEndpoint + resource + '/' + entity + suffix, hdrs) conn.request('GET', self._apiEndpoint + resource + '/' + entity + suffix, '', hdrs) response = conn.getresponse() vprint('Status: ', response.status) if (response.status != 200): return False else: if rawResponse: return response else: return json.loads(response.read())
def full_list(self, resource, fields): conn = self.conn() hdrs = self.headers params = '?full-list=true' if fields != []: params = params + '&full-list-fields=' + ','.join(fields) vprint('GET', self._apiEndpoint + resource, '', hdrs) url = self._apiEndpoint + resource + params curlprint(self.host, 'GET', url, headers=hdrs) conn.request('GET', url, '', hdrs) response = conn.getresponse() vprint('Status: ', response.status) if (response.status != 200): return False else: return json.loads(response.read())
def put_bindata(self, resource, entity, target, suffix='', headers=None): conn = self.conn() if isinstance(entity, str): entity = [entity] url = self._apiEndpoint + resource + '/' + '/'.join(entity) + suffix hdrs = headers or self.headers vprint('PUT', url, '@' + target.name, hdrs) curlprint(self.host, 'PUT', url, headers=hdrs, upload=target.name) conn.request('PUT', url, target, hdrs) response = conn.getresponse() vprint('Status: ', response.status) if (response.status > 200 and response.status > 200): return response.status if (response.status != 200): return False else: return json.loads(response.read())
def put(self, resource, entity, body): conn = self.conn() if isinstance(entity, str): entity = [entity] url = self._apiEndpoint + resource + '/' + '/'.join(entity) jbody = json.dumps(body) vprint('PUT', url, jbody, self.headers) curlprint(self.host, 'PUT', url, headers=self.headers, data=jbody) conn.request('PUT', url, jbody, self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status == 201): # This was added for dryrun. Add body? return True if (response.status != 200): return False else: return json.loads(response.read())
def put_attr(self, resource, entity, attr, body): conn = self.conn() if isinstance(attr, str): attr = [attr] jbody = json.dumps(body) url = self._apiEndpoint + resource + '/' + entity + '/' + '/'.join(attr) vprint('PUT', url, jbody, self.headers) curlprint(self.host, 'PUT', url, headers=self.headers, data=jbody) conn.request('PUT', url, jbody, self.headers) response = conn.getresponse() vprint('Status: ', response.status) if response.status == 200: return json.loads(response.read()) elif (response.status > 200 and response.status < 400): print response.status return True else: return False
def connect(self, user, pw): conn = self.conn() if self._apiVersion == '0.1.0': print "the 0.1.0 API is no longer supported!" exit(1) else: form = urllib.urlencode({'username' :user, 'password': pw, 'grant_type': 'password'}) url = self._apiEndpoint + 'oauth/token' headers = copy.copy(self.headers) headers['content-type'] = 'application/x-www-form-urlencoded' vprint('POST', url, form, self.headers) curlprint(self.host, 'POST', url, headers=self.headers, data=form) conn.request('POST', url, form, self.headers) response = conn.getresponse() vprint('Status:', response.status) if (response.status == 200): resp = json.loads(response.read()) self._token = resp['access_token'] self.headers['authorization'] = 'Bearer ' + self._token return self._token else: return False
def post_attr(self, resource, entity, attr, body): conn = self.conn() if isinstance(attr, str): attr = [attr] url = self._apiEndpoint + resource + '/' + entity + '/' + '/'.join(attr) jbody = json.dumps(body) vprint('POST', url, jbody, self.headers) curlprint(self.host, 'POST', url, headers=self.headers, data=jbody) conn.request('POST', url, jbody, self.headers) response = conn.getresponse() vprint('Status: ', response.status) if (response.status == 303): newurl = response.getheader('Location') conn = self.conn() conn.request('GET', newurl, '', self.headers) response = conn.getresponse() vprint('Status (after redirect): ', response.status) if (response.status == 404): time.sleep(5) conn = self.conn() conn.request('GET', newurl, '', self.headers) response = conn.getresponse() vprint('Status (after reload): ', response.status) if (response.status != 200): return False else: return json.loads(response.read()) elif (response.status != 200): return False else: return json.loads(response.read()) elif (response.status == 201): return 201 elif (response.status == 200): return json.loads(response.read()) else: return False
amt = 4096 try: data = response.read(amt) except httplib.IncompleteRead, e: data = e.partial f.write(data) f.flush() return while data: f.write(data) f.flush() try: data = response.read(amt) except httplib.IncompleteRead, e: vprint('Incomplete read captured') data = e.partial def dataset_get(args): response = args.endpoint.get_bindata(args.uuid, '/dataset.gz') if response: vprint('Headers:', response.getheaders()) _write_bindata(response, args.out or sys.stdout) def import_dataset(args): j = json.load(args.manifest) uuid = j['uuid'] if not args.dataset_only: response = args.endpoint._put_attr(uuid, [], j) if response: print 'Manifest imported'
amt = 4096 try: data = response.read(amt) except httplib.IncompleteRead, e: data = e.partial f.write(data) f.flush() return while data: f.write(data) f.flush() try: data = response.read(amt) except httplib.IncompleteRead, e: vprint('Incomplete read captured') data = e.partial def dataset_get(args): response = args.endpoint.get_bindata(args.uuid, '/dataset.gz') if response: vprint('Headers:', response.getheaders()) _write_bindata(response, args.out or sys.stdout) def import_dataset(args): j = json.load(args.manifest) uuid = j['uuid'] if not args.dataset_only: response = args.endpoint._put_attr(uuid, [], j)
def dataset_get(args): response = args.endpoint.get_bindata(args.uuid, '/dataset.gz') if response: vprint('Headers:', response.getheaders()) _write_bindata(response, args.out or sys.stdout)