def get_new(cls, id=None, revision=None): client = PaperlessClient.get_instance() return client.get_new_resources( cls.construct_get_new_resources_url(), params=cls.construct_get_new_params(id, revision) if id else None, )
def create(self, table_name): """ Persist new version of self to Paperless Parts and updates instance with any new data from the creation. """ client = PaperlessClient.get_instance() data = self.to_json({'name': table_name}) resp_json = client.create_resource(self.construct_post_url(), data=data) return resp_json
def test_without_bearer_token_throws_an_error(self): """ Without a proper access token the client will raise a PaperlessAuthorizationException. """ client = PaperlessClient.get_instance() client.access_token = None self.assertEqual(client.access_token, None) self.assertRaises(PaperlessAuthorizationException, self.client.get_authenticated_headers)
def update(self, table_name): """ Persists local changes of an existing Paperless Parts resource to Paperless. """ client = PaperlessClient.get_instance() data = self.to_json({'config': self.config, 'data': self.data}) resp_json = client.update_resource(self.construct_patch_url(), table_name, data=data) return resp_json
def test_authorization_token(self): """ Test that we generate the properly formatted authenticated headers. """ client = PaperlessClient.get_instance() client.access_token = self.access_token self.assertEqual(client.access_token, self.access_token) headers = self.client.get_authenticated_headers() self.assertEqual(headers['Authorization'], 'API-Token {}'.format(self.access_token))
def test_client_is_singleton(self): # singleton by get_instance new_client1 = PaperlessClient.get_instance() self.assertTrue(new_client1 is self.client) new_client1.access_token = self.access_token self.assertEqual(self.access_token, self.client.access_token) # singleton by new instance new_client2 = PaperlessClient() self.assertTrue(new_client2 is self.client) self.assertEqual(self.access_token, new_client2.access_token)
def download_csv(cls, table_name, config=False, file_path=None): """ Download a CSV of the table data. If config == True, download the config data for the table instead. """ params = {'config': True} if config else None if file_path is None: file_path = f'table_{table_name}_{"config" if config else "data"}.csv' client = PaperlessClient.get_instance() client.download_file(cls.construct_download_csv_url(), table_name, file_path, params=params)
def get(cls, id, revision=None): """ Retrieves the resource specified by the id and revision. :raise PaperlessNotFoundException: Raised when the requested id 404s aka is not found. :param id: int :param revision: Optional[int] :return: resource """ client = PaperlessClient.get_instance() return cls.from_json( client.get_resource(cls.construct_get_url(), id, params=cls.construct_get_params(revision)))
def create(self, account_id): """ Persist new version of self to Paperless Parts and updates instance with any new data from the creation. """ client = PaperlessClient.get_instance() data = self.to_json() resp = client.create_resource(self.construct_post_url(account_id), data=data) resp_obj = self.from_json(resp) keys = filter( lambda x: not x.startswith('__') and not x.startswith('_'), dir(resp_obj)) for key in keys: setattr(self, key, getattr(resp_obj, key))
def set_status(self, status): client = PaperlessClient.get_instance() params = None if self.revision_number is not None: params = {'revision': self.revision_number} resp_json = client.request( url=f'quotes/public/{self.number}/status_change', method=PaperlessClient.METHODS.PATCH, data={"status": status}, params=params, ) resp_obj = self.from_json(resp_json) keys = filter( lambda x: not x.startswith('__') and not x.startswith('_'), dir(resp_obj)) for key in keys: setattr(self, key, getattr(resp_obj, key))
def list(cls, account_id=None, params=None): """ Returns a list of (1) either the minimal representation of this resource as defined by _list_object_representation or (2) a list of this resource. :param params: dict of params for your list request :return: [resource] """ client = PaperlessClient.get_instance() resource_list = cls.parse_list_response( client.get_resource_list(cls.construct_list_url(account_id), params=params)) if cls._list_object_representation: return [ cls._list_object_representation.from_json(resource) for resource in resource_list ] else: return [cls.from_json(resource) for resource in resource_list]
def construct_post_url(cls): client = PaperlessClient.get_instance() return 'suppliers/public/{}/custom_tables'.format(client.group_slug)
def delete(cls, table_name): client = PaperlessClient.get_instance() return client.delete_resource(cls.construct_delete_url(), table_name)
def get(cls, table_name): client = PaperlessClient.get_instance() return client.get_resource(cls.construct_get_url(), table_name)
def get_list(cls): client = PaperlessClient.get_instance() return client.get_new_resources(cls.construct_list_url(), params=None)
def construct_list_url(cls): client = PaperlessClient.get_instance() return 'orders/groups/{}'.format(client.group_slug)