def retrieve(cls: Type[T], entity: Entity, account: Account, order: LedgerOrder = LedgerOrder.YOUNGEST_FIRST, page: int = 1, start_time: Optional[datetime] = None, end_time: Optional[datetime] = None, denomination: Optional[Denomination] = None) -> T: """ Retrieve a Ledger for the supplied account. Optionally specify order, page, denomination, start time, and end time. """ if not isinstance(entity, Entity): raise TypeError('entity must be of type `Entity`') arguments = Ledger.RetrieveArguments(account, order, page, start_time, end_time, denomination) data = DataPackage(object_data=arguments, override_listing=True) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest(path=cls._PATH, method=HTTPMethod.GET, credentials=entity.session, data=data, url_parameters=parameters) return cls._decode(entity, request.response_data)
def update(self: T, time: Optional[datetime] = None, entries: Optional[List[Entry]] = None, denomination: Optional[Denomination] = None, description: Optional[str] = None) -> T: """Replace existing transaction data with supplied data.""" arguments = Transaction.UpdateArguments(self, time, entries, denomination, description) data = DataPackage.from_object(arguments) parameters = UrlParameters(entity_id=self.entity.id_) request = ApiRequest(path=Transaction._PATH, method=HTTPMethod.PUT, credentials=self.entity.session, data=data, url_parameters=parameters) transaction = Transaction._decode(self.entity, request.response_data) if transaction.id_ != self.id_: raise ApiError('Mismatched response Trasaction ID - Fatal') return transaction
def create_many( cls: Type[T], session: Session, arguments: List[K] ) -> List[T]: """Return many newly created Users""" if not isinstance(session, Session): raise TypeError('session must be of type Session') if not isinstance(arguments, list): raise TypeError( 'arguments must be of type List[User.CreateArguments]' ) if False in [isinstance(a, User.CreateArguments) for a in arguments]: raise TypeError( 'arguments must be of type List[User.CreateArguments]' ) request = ApiRequest( path=cls._PATH, method=HTTPMethod.POST, credentials=session, data=DataPackage(list_data=arguments), url_parameters=None ) return cls.decode_many(session, request.response_data)
def retrieve(cls: Type[T], session: Session, state: State = State.ALL, page: int = 1) -> T: """ Retrieve a UserList from the perspective of the User tied to the supplied Session. Optionally specify a State (active, all, deleted) and integer page number. """ if not isinstance(session, Session): raise TypeError('session must be of type Session') if not isinstance(state, State): raise TypeError('state must be of type State') if not isinstance(page, int): raise TypeError('page must be of type int') state_target = UrlTarget('state', state.value) page_target = UrlTarget('page', str(page)) parameters = UrlParameters(targets=[state_target, page_target]) request = ApiRequest(path=cls._PATH, method=HTTPMethod.GET, credentials=session, data=None, url_parameters=parameters) return cls.decode(session, request.response_data)
def create( cls: Type[T], entity: Entity, name: str, code: str, exponent: int, description: Optional[str] = None, priority: Optional[int] = None ) -> T: arguments = CustomUnit.CreationArguments( code, name, exponent, priority, description ) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest( path=CustomUnit._PATH, credentials=entity.session, method=HTTPMethod.POST, data=DataPackage.from_object(arguments), url_parameters=parameters ) return CustomUnit._decode(entity, request.response_data)
def update( self, name: Optional[str] = None, description: Optional[str] = None, owner: Optional[User] = None, permissions_graph: Optional[PermissionsGraph] = None) -> 'Entity': """ Modify data describing this Entity. Returns this Entity, the Entity instance is not modified in place. """ owner_id = None if owner is not None: owner_id = owner.id_ update_arguments = Entity.UpdateArguments( self, name=name, description=description, owner_id=owner_id, permissions_graph=permissions_graph) data_package = DataPackage.from_object(data=update_arguments) request = ApiRequest(path=Entity.PATH, method=HTTPMethod.PUT, credentials=self._session, data=data_package, url_parameters=None) updated_entity = Entity.decode(request.response_data, self.session) return updated_entity
def update(self: T, name: Optional[str] = None, am_type: Optional[AMType] = None, parent: Optional[T] = None, description: Optional[str] = None, denomination: Optional[Denomination] = None, counterparty: Optional[Entity] = None, color: Optional[Color] = None) -> 'Account': """ Update this Account with new metadata. """ arguments = Account.UpdateArguments(self, name, am_type, parent, description, denomination, counterparty, color) data = DataPackage.from_object(arguments) parameters = UrlParameters(entity_id=self.entity.id_) request = ApiRequest(path=Account._PATH, method=HTTPMethod.PUT, credentials=self.entity.session, data=data, url_parameters=parameters) account = Account._decode(self.entity, request.response_data) if account.id_ != self.id_: raise ApiError('Returned Account ID does not match request ID') return account
def create(cls: Type[T], entity: Entity, name: str, am_type: AMType, denomination: Denomination, description: Optional[str] = None, parent: Optional[T] = None, counter_party: Optional[Entity] = None, color: Optional[Color] = None) -> T: arguments = Account.CreateArguments(name, description, am_type, parent, denomination, counter_party, color) data = DataPackage.from_object(arguments) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest(path=Account._PATH, method=HTTPMethod.POST, credentials=entity.session, data=data, url_parameters=parameters) account = cls._decode(entity, request.response_data) return account
def _retrieve( cls: Type[T], entity: Entity, custom_unit_ids: List[int] ) -> T: if not isinstance(entity, Entity): raise TypeError('entity must be of type `Entity`') key = CustomUnit._URL_KEY targets = [UrlTarget(key, str(i)) for i in custom_unit_ids] url_parameters = UrlParameters( entity_id=entity.id_, targets=targets ) request = ApiRequest( path=CustomUnit._PATH, method=HTTPMethod.GET, credentials=entity.session, data=None, url_parameters=url_parameters ) unit = cls._decode( entity, request.response_data ) return unit
def retrieve_many(cls: Type[T], entity: Entity, ids: List[int], denomination: Denomination) -> List[T]: """Return many retrieved Transactions""" if not isinstance(entity, Entity): raise TypeError('entity must be of type `Entity`') if not isinstance(ids, list): raise TypeError('ids must be of type `list`') if False in [isinstance(i, int) for i in ids]: raise TypeError('ids must be of type `int`') parameters = UrlParameters(entity_id=entity.id_) data = DataPackage(list_data=[ cls.RetrieveArguments(i, denomination, None) for i in ids ]) request = ApiRequest(path=Transaction._PATH, method=HTTPMethod.GET, credentials=entity.session, data=data, url_parameters=parameters) transactions = cls.decode_many(entity, request.response_data) return transactions
def delete(self) -> None: """Return None after deleting this User""" target = UrlTarget.from_integer('user_id', self._id) parameters = UrlParameters(targets=[target]) ApiRequest( path=self._PATH, data=None, credentials=self._session, method=HTTPMethod.DELETE, url_parameters=parameters ) return
def update( self, name: Optional[str] = None, code: Optional[str] = None, priority: Optional[int] = None, description: Optional[str] = None, exponent: Optional[str] = None ) -> None: """ Replace existing Custom Unit data with supplied data. Parameters not supplied will default to existing values. """ if name is None: name = self.name if code is None: code = self.code if priority is None: priority = self.priority if description is None: description = self.description if exponent is None: exponent = self.exponent assert isinstance(code, str) assert isinstance(priority, int) assert isinstance(description, str) assert isinstance(exponent, int) arguments = CustomUnit.UpdateArguments( self.id_, code, name, exponent, priority, description ) parameters = UrlParameters(entity_id=self.entity.id_) request = ApiRequest( path=self._PATH, method=HTTPMethod.PUT, data=DataPackage.from_object(arguments), url_parameters=parameters, credentials=self.entity.session ) return CustomUnit._decode( self.entity, request.response_data )
def retrieve_list(cls: Type[T], session: Session, state: State = State.ALL, offset: int = 0, limit: int = 10, name_fragment: Optional[str] = None) -> List[T]: if not isinstance(session, Session): raise TypeError('session must be of type `amatino.Session`') if not isinstance(offset, int): raise TypeError('offset must be of type `int`') if not isinstance(limit, int): raise TypeError('limit must be of type `int`') if not isinstance(state, State): raise TypeError('state must be of type `amatino.State`') if name_fragment is not None: if not isinstance(name_fragment, str): raise TypeError('name_fragment must be of type `str`') if len(name_fragment) < cls.MIN_NAME_SEARCH_LENGTH: raise ValueError( 'name_fragment minimum length is {c} char'.format( c=str(cls.MIN_NAME_SEARCH_LENGTH))) if len(name_fragment) > cls.MAX_NAME_SEARCH_LENGTH: raise ValueError( 'name_fragment maximum length is {c} char'.format( c=str(cls.MAX_NAME_SEARCH_LENGTH))) url_targets = [ UrlTarget('limit', limit), UrlTarget('offset', offset), UrlTarget('state', state.value) ] if name_fragment is not None: url_targets.append(UrlTarget('name', name_fragment)) url_parameters = UrlParameters(targets=url_targets) request = ApiRequest(path=Entity.LIST_PATH, method=HTTPMethod.GET, credentials=session, data=None, url_parameters=url_parameters) return cls.optionally_decode_many(data=request.response_data, session=session, default_to_empty_list=True)
def _create(cls: Type[T], user_id: Optional[int], email: Optional[str], secret: str) -> T: new_arguments = NewSessionArguments(secret, email, user_id) request_data = DataPackage(object_data=new_arguments, override_listing=True) request = ApiRequest(path=cls._PATH, method=HTTPMethod.POST, data=request_data) entity = cls._decode(request.response_data) return entity
def delete(self) -> None: """ Destroy this Transaction, such that it will no longer be included in any view of this Entity's accounting information. """ target = UrlTarget(self._URL_KEY, str(self.id_)) parameters = UrlParameters(entity_id=self.entity.id_, targets=[target]) ApiRequest(path=self._PATH, method=HTTPMethod.DELETE, credentials=self.entity.session, data=None, url_parameters=parameters) return
def retrieve(cls: Type[T], entity: Entity, account_id: int) -> T: """ Return an existing Account """ target = UrlTarget.from_integer(key=Account._URL_KEY, value=account_id) url_parameters = UrlParameters(entity_id=entity.id_, targets=[target]) request = ApiRequest(path=Account._PATH, method=HTTPMethod.GET, credentials=entity.session, data=None, url_parameters=url_parameters) account = cls._decode(entity, request.response_data) return account
def create(cls: Type[T], session: Session, name: str, description: Optional[str], region: Optional[Region] = None) -> T: new_entity_arguments = cls.CreateArguments(name, description, region) request_data = DataPackage.from_object(new_entity_arguments) request = ApiRequest(Entity.PATH, HTTPMethod.POST, session, request_data, None, False) created_entity = cls.decode(request.response_data, session) return created_entity
def retrieve(cls: Type[T], session: Session, entity_id: str) -> T: if not isinstance(session, Session): raise TypeError('session must be of type `Session`') if not isinstance(entity_id, str): raise TypeError('entity_id must be of type `str`') url_parameters = UrlParameters(entity_id=entity_id) request = ApiRequest(path=Entity.PATH, method=HTTPMethod.GET, credentials=session, data=None, url_parameters=url_parameters, debug=False) return cls.decode(request.response_data, session)
def retrieve( cls: Type[T], entity: Entity, custom_unit_id: int ) -> T: target = UrlTarget.from_integer(cls._URL_KEY, custom_unit_id) parameters = UrlParameters(entity_id=entity.id_, targets=[target]) request = ApiRequest( path=cls._PATH, method=HTTPMethod.GET, data=None, url_parameters=parameters, credentials=entity.session ) return cls._decode(entity, request.response_data)
def retrieve_many(cls: Type[T], entity: Entity, arguments: List[K]) -> List[T]: """Retrieve several Balances.""" if not isinstance(entity, Entity): raise TypeError('entity must be of type `Entity`') if False in [isinstance(a, cls.RetrieveArguments) for a in arguments]: raise TypeError( 'arguments must be of type List[Balance.RetrieveArguments]') data = DataPackage(list_data=arguments) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest(path=cls.PATH, method=HTTPMethod.GET, credentials=entity.session, data=data, url_parameters=parameters) return cls._decode_many(entity, request.response_data)
def _retrieve(cls: Type[T], entity: Entity, arguments: K) -> T: """Retrieve a Position""" if not isinstance(entity, Entity): raise TypeError('entity must be of type `Entity`') if not isinstance(arguments, cls.RetrieveArguments): raise TypeError( 'arguments must be of type Position.RetrieveArguments' ) data = DataPackage(object_data=arguments, override_listing=True) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest( path=cls._PATH, method=HTTPMethod.GET, credentials=entity.session, data=data, url_parameters=parameters ) return cls.decode(entity, request.response_data)
def _retrieve_many( cls: Type[T], session: Session, ids: Optional[List[int]] = None ) -> List[T]: """Return a list of users""" parameters = None if ids is not None: targets = UrlTarget.from_many_integers(cls._URL_KEY, ids) parameters = UrlParameters.from_targets(targets) request = ApiRequest( path=cls._PATH, data=None, credentials=session, method=HTTPMethod.GET, url_parameters=parameters ) users = cls.decode_many(session, request.response_data) return users
def retrieve( cls: Type[T], entity: Entity, transaction: Transaction ) -> T: """Return a TransactionVersionList for the supplied Transaction""" if not isinstance(transaction, Transaction): raise TypeError('transaction must be of type Transaction') if not isinstance(entity, Entity): raise TypeError('entity must be of type Entity') targets = [UrlTarget.from_integer('transaction_id', transaction.id_)] parameters = UrlParameters(entity_id=entity.id_, targets=targets) request = ApiRequest( path=cls._PATH, method=HTTPMethod.GET, credentials=entity.session, data=None, url_parameters=parameters ) return cls._decode(entity, request.response_data)
def create( cls: Type[T], entity: Entity, time: datetime, entries: List[Entry], denomination: Denomination, description: Optional[str] = None, ) -> T: arguments = Transaction.CreateArguments(time, entries, denomination, description) data = DataPackage.from_object(arguments) parameters = UrlParameters(entity_id=entity.id_) request = ApiRequest(path=Transaction._PATH, method=HTTPMethod.POST, credentials=entity.session, data=data, url_parameters=parameters) transaction = cls._decode(entity, request.response_data) return transaction
def retrieve_many(cls: Type[T], session: Session, ids: List[int]) -> List[T]: """Retrieve a set of Global Units""" if not isinstance(session, Session): raise TypeError('session must be of type `Session`') if not isinstance(ids, list): raise TypeError('ids must be of type `List[int]`') if False in [isinstance(i, int) for i in ids]: raise TypeError('ids must be of type `List[int]`') targets = UrlTarget.from_many_integers(GlobalUnit._URL_KEY, ids) parameters = UrlParameters.from_targets(targets) request = ApiRequest(path=GlobalUnit._PATH, url_parameters=parameters, credentials=session, method=HTTPMethod.GET) units = GlobalUnit._decode_many(request.response_data) return units