def __init__(self, app_id, server_params, consumer_token, resource_token=None): """Constructor for SmartClient. app_id: ID by which the app is known to the SMArt Container server_params: Hash containing an "api_base" key pointing to the SMArt Container's base URL. e.g. {"api_base" : "http://sandbox-api.smartplatforms.org"} consumer_token: Hash containing a "consumer_key" and "consumer_secret" to be used for two- and three-legged OAuth requests. resource_token (optional): Hash containing session "oauth_token" and "oauth_token_secret" to be used for three-legged OAuth requests. """ consumer = OAuthConsumer(consumer_key = consumer_token['consumer_key'], secret = consumer_token['consumer_secret']) super(SmartClient, self).__init__(consumer = consumer); self.server_params = server_params if (resource_token): token = OAuthToken(token=resource_token['oauth_token'], secret=resource_token['oauth_token_secret']) self.set_token(token) self.baseURL = self.server_params['api_base'] self.saved_ids = {} self.app_id = app_id self.stylesheet = None if (not common.rdf_ontology.parsed): print "unparsed." self.__class__.ontology_file = self.get("/ontology") common.rdf_ontology.parse_ontology(SmartClient.ontology_file) print "parsed onto", common.rdf_ontology.parsed generate_api.augment(self.__class__) print "Done init sc"
def __init__(self, app_id, server_params, consumer_token, resource_token=None): """Constructor for SmartClient. app_id: ID by which the app is known to the SMArt Container server_params: Hash containing an "api_base" key pointing to the SMArt Container's base URL. e.g. {"api_base" : "http://sandbox-api.smartplatforms.org"} consumer_token: Hash containing a "consumer_key" and "consumer_secret" to be used for two- and three-legged OAuth requests. resource_token (optional): Hash containing session "oauth_token" and "oauth_token_secret" to be used for three-legged OAuth requests. """ consumer = OAuthConsumer(consumer_key=consumer_token['consumer_key'], secret=consumer_token['consumer_secret']) super(SmartClient, self).__init__(consumer=consumer) self.server_params = server_params if (resource_token): token = OAuthToken(token=resource_token['oauth_token'], secret=resource_token['oauth_token_secret']) self.set_token(token) self.baseURL = self.server_params['api_base'] self.saved_ids = {} self.app_id = app_id self.stylesheet = None if (not common.rdf_ontology.parsed): self.__class__.ontology_file = self.get("/ontology") common.rdf_ontology.parse_ontology(SmartClient.ontology_file) generate_api.augment(self.__class__)
if not self.token: raise SMARTClientError("Client must have a token to generate SURL credentials.") secret = base64.b64encode(hmac.new(self.token.secret, "SURL-SECRET", hashlib.sha1).digest()) return {'token': self.token.key, 'secret': secret} def _fill_url_template(self, url, **kwargs): for param_name in re.findall("{(.*?)}", str(url)): v = None arg_name = param_name.lower() try: v = kwargs[arg_name] except KeyError as e: # Is it a direct attribute of the client? i.e. client.record_id try: v = getattr(self, arg_name) except AttributeError: raise KeyError("Expected argument %s" % arg_name) if v is not None: url = url.replace("{%s}" % param_name, unicode(v)) return url def request(self, uri, uri_params, *args, **kwargs): uri = self._fill_url_template(uri, **uri_params) return super(SMARTClient, self).request(uri, *args, **kwargs) if (not common.rdf_tools.rdf_ontology.parsed): assert False, "No ontology found" augment(SMARTClient)