def test_connector(self): import jobboss.models as jb jb.AutoNumber.objects.create(type='SalesOrder', system_generated=True, last_nbr=1) jb.AutoNumber.objects.create(type='Job', system_generated=True, last_nbr=1) from job import process_order with open('core-python/tests/unit/mock_data/order.json') as data_file: mock_order_json = json.load(data_file) client = PaperlessClient() client.get_resource = MagicMock(return_value=mock_order_json) order = Order.get(1) process_order(order) self.assertEqual(len(order.order_items), jb.Job.objects.filter(job=F('top_lvl_job')).count()) self.assertEqual( sum([ len([comp for comp in oi.components if not comp.is_hardware]) for oi in order.order_items ]), jb.Job.objects.count()) op_count = 0 for oi in order.order_items: for comp in oi.components: op_count += len(comp.shop_operations) addon_count = sum(len(oi.ordered_add_ons) for oi in order.order_items) self.assertEqual(op_count + addon_count, jb.JobOperation.objects.count())
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 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 setUp(self): # instantiate client singleton self.client = PaperlessClient() with open('tests/unit/mock_data/purchased_component_columns.json' ) as data_file: self.mock_purchased_component_columns_json = json.load(data_file) self.mock_c1 = self.mock_purchased_component_columns_json[0]
def setUp(self): # instantiate client singleton self.client = PaperlessClient() with open('tests/unit/mock_data/order.json') as data_file: self.mock_order_json = json.load(data_file) with open('tests/unit/mock_data/minimal_order.json') as data_file: self.mock_minimal_order_json = json.load(data_file)
def main(): PaperlessClient( access_token=common.PAPERLESS_CONFIG.token, group_slug=common.PAPERLESS_CONFIG.slug ) my_sdk = PaperlessSDK(loop=False) listener = MyOrderListener() my_sdk.add_listener(listener) my_sdk.run()
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 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]
class TestClient(unittest.TestCase): def setUp(self): # instantiate client singleton self.client = PaperlessClient() self.access_token = "test_accesstoken" 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 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 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 setUp(self): self.client = PaperlessClient() with open('tests/unit/mock_data/account.json') as data_file: self.mock_account_json = json.load(data_file)
def setUp(self): self.client = PaperlessClient() with open('tests/unit/mock_data/facility.json') as data_file: self.mock_facility_json = json.load(data_file)
def setUp(self): self.client = PaperlessClient() with open('tests/unit/mock_data/contact_list.json') as data_file: self.mock_contact_list_json = json.load(data_file)
def setUp(self): # instantiate client singleton self.client = PaperlessClient() self.access_token = "test_accesstoken"
def setUp(self): self.client = PaperlessClient() with open('tests/unit/mock_data/billing_address.json') as data_file: self.mock_billing_address_json = json.load(data_file)
def construct_post_url(cls): client = PaperlessClient.get_instance() return 'suppliers/public/{}/custom_tables'.format(client.group_slug)
my_sdk.run() if __name__ == '__main__': test_mode = False try: if sys.argv[1] == 'testmode': test_mode = True order_num = None else: order_num = int(sys.argv[1]) except (IndexError, ValueError): order_num = None if order_num is not None: PaperlessClient( access_token=common.PAPERLESS_CONFIG.token, group_slug=common.PAPERLESS_CONFIG.slug ) order = Order.get(order_num) process_order(order) elif test_mode: print('Testing JobBOSS Connection') print('Host:', common.JOBBOSS_CONFIG.host) print('Database:', common.JOBBOSS_CONFIG.name) print('Username:'******'Job count: {} OK!'.format(c)) else: if common.PAPERLESS_CONFIG.active: logger.info('Running connector!') main()
def construct_list_url(cls): client = PaperlessClient.get_instance() return 'orders/groups/{}'.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 setUp(self): # instantiate client singleton self.client = PaperlessClient() with open('tests/unit/mock_data/quote.json') as data_file: self.mock_quote_json = json.load(data_file)