Beispiel #1
0
 def delete(self, id_object, resource=None):
     resource = resource or self.resource
     url = "{}/{}.json".format(resource, id_object)
     resp = self.base._request(url, 'DELETE')
     if resp.status_code != 200:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
Beispiel #2
0
 def get_one(self, id_object, resource=None):
     resource = resource or self.resource
     url = "{}/{}.json".format(resource, id_object)
     resp = self.base._request(url)
     if resp.status_code != 200:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
     return self.class_object(**resp.json())
Beispiel #3
0
 def put(self, id_object, resource=None, **kwargs):
     resource = resource or self.resource
     url = "{}/{}.json".format(resource, id_object)
     data = {singularize(resource): kwargs}
     resp = self.base._request(url, 'PUT', **data)
     if resp.status_code != 200:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
     return self.class_object(**resp.json().get(singularize(resource)))
Beispiel #4
0
 def create(self, resource=None, **kwargs):
     resource = resource or self.resource
     url = "{}.json".format(resource)
     singular_resource = singularize(resource)
     data = {
         singular_resource: kwargs,
     }
     resp = self.base._request(url, 'POST', **data)
     if resp.status_code != 201:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
     return self.class_object(**resp.json().get(singularize(resource)))
Beispiel #5
0
 def get_one_query(self, query, resource=None):
     resource = resource or self.resource
     endpoint = "{}/search.json".format(resource)
     resp = self.base._request(endpoint, params=query)
     if resp.status_code != 200:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
     resp_json = resp.json()
     if resp_json.get('count') != 1:
         resp_json.update({'error': 'Return multiples objects for search!'})
         raise RequestException(resp.status_code, content=resp_json)
     item = resp_json.pop(self.resource)[0]
     return self.class_object(**item)
Beispiel #6
0
    def get(self, resource=None, page=1, per_page=10, **kwargs):
        resource = resource or self.resource
        endpoint = "{}.json?page={}&per_page={}".format(
            resource, page, per_page)
        resp = self.base._request(endpoint, **kwargs)
        if resp.status_code != 200:
            content = safe_get_json(resp)
            raise RequestException(resp.status_code, content=content)
        resp = resp.json()

        items = resp.pop(self.resource)
        resp.update(items=map(lambda x: self.class_object(**x), items))
        return resp
Beispiel #7
0
 def show_many(self, resource=None, name_field='ids', fields=[]):
     if len(fields) > 100:
         raise Exception('identificares the limit is 100!')
     resource = resource or self.resource
     url = "{}/show_many.json?{}={}".format(
         resource, name_field, fields.split(','))
     resp = self.base._request(url)
     if resp.status_code != 200:
         content = safe_get_json(resp)
         raise RequestException(resp.status_code, content=content)
     resp = resp.json()
     items = resp.pop(self.resource)
     resp.update(items=map(lambda x: self.class_object(**x), items))
     return resp
    def get_id(self, organization_id, user_id):
        endpoint = 'organizations/{organization_id}/\
organization_memberships.json'.format(organization_id=organization_id)
        resp = self.base._request(endpoint)
        if resp.status_code == 404:
            return None

        elif resp.status_code != 200:
            content = safe_get_json(resp)
            raise RequestException(resp.status_code, content=content)
        resp = resp.json()
        for organization in resp['organization_memberships']:
            if organization['user_id'] == user_id:
                return organization['id']
        return None