44author: hugh@blinkybeach.com
55"""
66from procuret .ancillary .communication_option import CommunicationOption
7- from typing import TypeVar , Type , Union
7+ from typing import TypeVar , Type , Union , List
88from procuret .data .codable import Codable , CodingDefinition as CD
99from procuret .ancillary .entity_headline import EntityHeadline
1010from procuret .errors .type_error import ProcuretTypeError
1111from procuret .http .api_request import ApiRequest , HTTPMethod
1212from procuret .errors .inconsistent import InconsistentState
1313from procuret .session import Session
1414from decimal import Decimal
15+ from enum import Enum
16+ from procuret .data .order import Order
17+ from procuret .http .query_parameters import QueryParameter , QueryParameters
18+ from typing import Optional
19+ from procuret .instalment_link .open import InstalmentLinkOpen
1520
16- T = TypeVar ('T' , bound = 'InstalmentLink' )
21+
22+ Self = TypeVar ('Self' , bound = 'InstalmentLink' )
23+
24+
25+ class OrderBy (Enum ):
26+ CREATED = 'created'
1727
1828
1929class InstalmentLink (Codable ):
2030
21- PATH = '/instalment-link'
31+ path = '/instalment-link'
32+ list_path = path + '/list'
2233
2334 _LINK_TEMPLATE = 'https://procuret.com/business/signup?supplier_id={entity\
2435 _id}&presented_invoice_id={invoice_id}&presented_invoice_amount={invoice_amoun\
2536 t}'
2637
2738 coding_map = {
39+ 'public_id' : CD (str ),
2840 'supplier' : CD (EntityHeadline ),
2941 'invoice_amount' : CD (Decimal ),
3042 'invitee_email' : CD (str ),
3143 'invoice_identifier' : CD (str ),
44+ 'opens' : CD (InstalmentLinkOpen , array = True )
3245 }
3346
3447 def __init__ (
3548 self ,
49+ public_id : str ,
3650 supplier : EntityHeadline ,
3751 invitee_email : str ,
3852 invoice_amount : Decimal ,
39- invoice_identifier : str
53+ invoice_identifier : str ,
54+ opens : List [InstalmentLinkOpen ]
4055 ) -> None :
4156
4257 self ._supplier = supplier
58+ self ._public_id = public_id
4359 self ._invitee_email = invitee_email
4460 self ._invoice_amount = invoice_amount
4561 self ._invoice_identifier = invoice_identifier
62+ self ._opens = opens
4663
4764 return
4865
66+ public_id = property (lambda s : s ._public_id )
4967 invitee_email = property (lambda s : s ._invitee_email )
5068 invoice_amount = property (lambda s : s ._invoice_amount )
5169 invoice_identifier = property (lambda s : s ._invoice_identifier )
70+ opens = property (lambda s : s ._opens )
5271
5372 url = property (lambda s : s ._LINK_TEMPLATE .format (
5473 entity_id = str (s ._supplier .entity_id ),
@@ -58,14 +77,14 @@ def __init__(
5877
5978 @classmethod
6079 def create (
61- cls : Type [T ],
80+ cls : Type [Self ],
6281 supplier : Union [int , EntityHeadline ],
6382 invoice_amount : Decimal ,
6483 invitee_email : str ,
6584 invoice_identifier : str ,
6685 communication : CommunicationOption ,
6786 session : Session
68- ) -> T :
87+ ) -> Self :
6988
7089 def infer_supplier_id (x : Union [int , EntityHeadline ]) -> int :
7190 if isinstance (x , int ):
@@ -113,7 +132,7 @@ def infer_communication(y: CommunicationOption) -> bool:
113132 }
114133
115134 result = ApiRequest .make (
116- path = cls .PATH ,
135+ path = cls .path ,
117136 method = HTTPMethod .POST ,
118137 data = data ,
119138 session = session ,
@@ -124,3 +143,77 @@ def infer_communication(y: CommunicationOption) -> bool:
124143 raise InconsistentState
125144
126145 return cls .decode (result )
146+
147+ @classmethod
148+ def retrieve (
149+ cls : Type [Self ],
150+ public_id : str ,
151+ session : Session
152+ ) -> Optional [Self ]:
153+
154+ if not isinstance (public_id , str ):
155+ raise TypeError ('public_id must be a string' )
156+
157+ result = cls .retrieve_many (
158+ public_id = public_id ,
159+ session = session
160+ )
161+
162+ if len (result ) < 1 :
163+ return None
164+
165+ return result [0 ]
166+
167+ @classmethod
168+ def retrieve_many (
169+ cls : Type [Self ],
170+ session : Session ,
171+ public_id : Optional [str ] = None ,
172+ limit : int = 20 ,
173+ offset : int = 0 ,
174+ order : Order = Order .ASCENDING ,
175+ order_by : OrderBy = OrderBy .CREATED ,
176+ opened : Optional [bool ] = None
177+ ) -> List [Self ]:
178+
179+ if not isinstance (limit , int ):
180+ raise TypeError ('limit must be an integer' )
181+
182+ if not isinstance (offset , int ):
183+ raise TypeError ('offset must be an integer' )
184+
185+ if not isinstance (order , Order ):
186+ raise TypeError ('order must be of type Order' )
187+
188+ if not isinstance (order_by , OrderBy ):
189+ raise TypeError ('order must be of type InstalmentLink.OrderBy' )
190+
191+ if not isinstance (session , Session ):
192+ raise TypeError ('session must be of type Session' )
193+
194+ parameters = [
195+ QueryParameter ('limit' , limit ),
196+ QueryParameter ('offset' , offset ),
197+ QueryParameter ('order' , order .value ),
198+ QueryParameter ('order_by' , order_by .value ),
199+ ]
200+
201+ if opened is not None :
202+ if not isinstance (opened , bool ):
203+ raise TypeError ('If supplied, opened must be a bool' )
204+ parameters .append (QueryParameter ('opened' , opened ))
205+
206+ if public_id is not None :
207+ if not isinstance (public_id , str ):
208+ raise TypeError ('If supplied, public_id must be str' )
209+ parameters .append (QueryParameter ('public_id' , public_id ))
210+
211+ result = ApiRequest .make (
212+ path = cls .list_path ,
213+ method = HTTPMethod .GET ,
214+ data = None ,
215+ session = session ,
216+ query_parameters = QueryParameters (parameters )
217+ )
218+
219+ return cls .optionally_decode_many (result , default_to_empty_list = True )
0 commit comments