def __init__(self, org_id, app_id, base_url='http://api.usergrid.com', client_id=None, client_secret=None, token_ttl_seconds=86400, auth_fallback="none"): self.base_url = base_url self.org_id = org_id self.app_id = app_id self.auth_fallback = auth_fallback self.logger = logging.getLogger('usergrid.UsergridClient') self.session = requests.Session() self.url_data = { 'base_url': base_url, 'org_id': org_id, 'app_id': app_id } if client_id and not client_secret: value_error('Client ID Specified but not Secret') elif client_secret and not client_id: value_error('Client ID Specified but not Secret') elif client_secret and client_id: self.auth = UsergridAppAuth(client_id=client_id, client_secret=client_secret, token_ttl_seconds=token_ttl_seconds) self.auth.authenticate(self) self.session.headers.update({'Authorization': 'Bearer %s' % self.auth.access_token})
class UsergridClient(object): def __init__(self, org_id, app_id, base_url='http://api.usergrid.com', client_id=None, client_secret=None, token_ttl_seconds=86400, auth_fallback="none"): self.base_url = base_url self.org_id = org_id self.app_id = app_id self.auth_fallback = auth_fallback self.logger = logging.getLogger('usergrid.UsergridClient') self.session = requests.Session() self.url_data = { 'base_url': base_url, 'org_id': org_id, 'app_id': app_id } if client_id and not client_secret: value_error('Client ID Specified but not Secret') elif client_secret and not client_id: value_error('Client ID Specified but not Secret') elif client_secret and client_id: self.auth = UsergridAppAuth(client_id=client_id, client_secret=client_secret, token_ttl_seconds=token_ttl_seconds) self.auth.authenticate(self) self.session.headers.update({'Authorization': 'Bearer %s' % self.auth.access_token}) def __str__(self): return json.dumps({ 'base_url': self.base_url, 'org_id': self.org_id, 'app_id': self.app_id, 'access_token': self.auth.access_token }) def GET(self, collection, uuid_name, connections='none', auth=None, **kwargs): url = get_entity_url_template.format(collection=collection, uuid_name=uuid_name, connections=connections, **self.url_data) if auth: r = requests.get(url, headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.get(url) return UsergridResponse(r, self) def PUT(self, collection, uuid_name, data, auth=None, **kwargs): url = put_entity_url_template.format(collection=collection, uuid_name=uuid_name, **self.url_data) if auth: r = requests.put(url, data=json.dumps(data), headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.put(url, data=json.dumps(data)) return UsergridResponse(r, self) def POST(self, collection, data, auth=None, **kwargs): url = post_collection_url_template.format(collection=collection, **self.url_data) if auth: r = requests.post(url, data=json.dumps(data), headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.post(url, data=json.dumps(data)) return UsergridResponse(r, self) def DELETE(self, collection, uuid_name, auth=None, **kwargs): url = delete_entity_url_template.format(collection=collection, uuid_name=uuid_name, **self.url_data) if auth: r = requests.delete(url, headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.delete(url) return UsergridResponse(r, self) def connect_entities(self, from_entity, relationship, to_entity, auth=None, **kwargs): url = connect_entities_by_type_template.format(from_collection=from_entity.get('type'), from_uuid_name=from_entity.entity_id(), relationship=relationship, to_collection=to_entity.get('type'), to_uuid_name=to_entity.entity_id(), **self.url_data) if auth: r = requests.post(url, headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.post(url) return UsergridResponse(r, self) def assign_role(self, role_uuid_name, entity, auth=None, **kwargs): url = assign_role_url_template.format(role_uuid_name=role_uuid_name, entity_type=entity.get('type'), entity_uuid_name=entity.entity_id(), **self.url_data) if auth: r = requests.delete(url, headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.delete(url) return UsergridResponse(r, self) def disconnect_entities(self, from_entity, relationship, to_entity, auth=None, **kwargs): url = connect_entities_by_type_template.format(from_collection=from_entity.get('type'), from_uuid_name=from_entity.entity_id(), relationship=relationship, to_collection=to_entity.get('type'), to_uuid_name=to_entity.entity_id(), **self.url_data) if auth: r = requests.delete(url, headers={'Authorization': 'Bearer %s' % auth.access_token}) else: r = self.session.delete(url) return UsergridResponse(r, self)