Esempio n. 1
0
    def run_query(self, endpoint=None):
        endpoint = endpoint or self.endpoint
        if not endpoint in self.data_query:
            raise Semantics3Error(
                "No query built", "You need to first create\
                                            a query using the add() method.")
        query = self.data_query[endpoint]
        self.query_result = self.query(endpoint, **query)

        if self.query_result['code'] != 'OK':
            raise Semantics3Error(self.query_result['code'],
                                  self.query_result['message'])
    def add(self, endpoint, *fields):
        ancestors = fields[:-2]
        parent = self.data_query.setdefault(endpoint, {})
        for i in ancestors:
            if isinstance(parent, dict):
                parent = parent.setdefault(i, {})
            else:
                raise Semantics3Error(
                    'Invalid constraint', 'Cannot add this constraint, \'' +
                    parent + '\' is already a value.')

        if isinstance(parent, dict):
            parent[fields[-2]] = fields[-1]
        else:
            raise Semantics3Error(
                'Invalid constraint', 'Cannot add this constraint, \'' +
                parent + '\' is already a value.')
Esempio n. 3
0
 def __init__(self, api_key=None, api_secret=None, endpoint=None):
     if api_key is None:
         raise Semantics3Error(
             'API Credentials Missing',
             'You did not supply an api_key. Please sign up at\
             https://semantics3.com/ to obtain your api_key.')
     if api_secret is None:
         raise Semantics3Error(
             'API Credentials Missing',
             'You did not supply an api_secret. Please sign up at\
             https://semantics3.com/ to obtain your api_key.')
     self.api_key = api_key
     self.api_secret = api_secret
     self.endpoint = endpoint
     self.oauth = OAuth1Session(api_key, client_secret=api_secret)
     self.data_query = {}
     self.query_result = None
     self.cache_size = 10
 def _remove(path, hash):
     if path[0] in hash:
         if len(path) == 1:
             del hash[path[0]]
         else:
             _remove(path[1:], hash[path[0]])
             if not hash[path[0]]:
                 del hash[path[0]]
     else:
         raise Semantics3Error(
             'Constraint does not exist',
             "Constraint '" + path[0] + "' does not exist.")
    def query(self, method, endpoint, kwargs):
        if method == "GET":
            params = {'q': json.dumps(kwargs)}
        else:
            params = kwargs
        response = self.fetch(method, endpoint, params)
        try:
            response_json = response.json()
        except:
            raise Exception("Malformed JSON")

        if response.status_code < 400:
            return response.json()
        else:
            if response.json().get('code') != 'OK':
                response_body = response.json()
                raise Semantics3Error(response_body.get('code'),
                                      response_body.get('message'))