def __init__(self, request_executor, base_url = None): assert_instance(request_executor, HttpClientRequestExecutor, "request_executor") self.base_url = self._get_base_url_(base_url) self.request_executor = request_executor self.resource_factory = ResourceFactory(self)
def __init__(self, client_builder = None): self.application_href = None if (client_builder): assert_instance(client_builder, ClientBuilder, 'client_builder') self.client_builder = client_builder else: self.client_builder = ClientBuilder()
def _save_(self, href, resource, return_type): assert_instance(resource, Resource, "resource") assert_subclass(return_type, Resource, "return_type") q_href = self._qualify_(href) if self._needs_to_be_fully_qualified_(href) else href response = self._execute_request_('post', q_href, json.dumps(self._to_dict_(resource))) return self.resource_factory.instantiate(return_type, response)
def execute_request(self, request): full_request_name = Request.__module__ + "." + Request.__name__ assert_instance(request, Request, "request argument must be an instance of {}.".format(full_request_name)) if self.api_key: self.signer.sign_request(api_key=self.api_key, request=request) self._add_query_string_to_href_(request) resp, content = self.http_client.request(request.href, request.http_method, request.body, request.http_headers) return Response(int(resp.status), resp.get('content-type'), content.decode())
def _get_href_with_user_info_(self, href, at_sign_index): assert_instance(href, str, 'href') assert_instance(at_sign_index, int, 'at_sign_index') double_slash_index = href.find('//') assert_true(double_slash_index > 0, 'Invalid application href URL') parts = {} parts[0] = href[0:double_slash_index + 2] #up to and including the double slash parts[1] = href[double_slash_index + 2:at_sign_index] #raw user info parts[2] = href[at_sign_index + 1:len(href)] #after the @ character return parts
def authenticate(self, parent_href, request): assert_not_none(parent_href, "parent_href must be specified.") assert_instance(request, UsernamePasswordRequest, 'request') username = request.principals if request.principals else '' password = request.credentials if request.credentials else '' value = base64.b64encode(bytes((username + ':' + password).encode())) attempt = self.data_store.instantiate(BasicLoginAttempt) attempt.type('basic') attempt.value(value.decode()) href = parent_href + '/loginAttempts' return self.data_store.create(href, attempt, AuthenticationResult)
def save(self, resource, clazz = None): assert_instance(resource, Resource, "resource") href = resource.href assert_true(href, "save may only be called on objects that have already been persisted (i.e. they have an existing href).") href = self._qualify_(href) if self._needs_to_be_fully_qualified_(href) else href clazz = clazz if clazz else resource.__class__ returned_resource = self._save_(href, resource, clazz) # ensure the caller's argument is updated with what is returned from the server: resource.set_properties(returned_resource.properties) return returned_resource
def __init__(self, api_key, base_url = None): assert_instance(api_key, ApiKey, 'api_key') request_executor = HttpClientRequestExecutor(api_key) self.data_store = DataStore(request_executor, base_url)
def create_application(self, application): assert_instance(application, stormpath.resource.Application, 'application') href = '/applications' #TODO enable auto discovery return self.data_store.create(href, application, stormpath.resource.Application)
def delete(self, resource): assert_instance(resource, Resource, 'resource') self._execute_request_('delete', resource.href)
def __init__(self, data_store): assert_instance(data_store, DataStore, "data_store") self.data_store = data_store
def __init__(self, data_store): assert_instance(data_store, stormpath.ds.DataStore, 'data_store') self.data_store = data_store