|
12 | 12 | if TYPE_CHECKING:
|
13 | 13 | from stapi_fastapi.backends.product_backend import (
|
14 | 14 | CreateOrder,
|
| 15 | + GetOpportunityCollection, |
15 | 16 | SearchOpportunities,
|
| 17 | + SearchOpportunitiesAsync, |
16 | 18 | )
|
17 | 19 |
|
18 | 20 |
|
@@ -54,46 +56,88 @@ class Product(BaseModel):
|
54 | 56 | _opportunity_properties: type[OpportunityProperties]
|
55 | 57 | _order_parameters: type[OrderParameters]
|
56 | 58 | _create_order: CreateOrder
|
57 |
| - _search_opportunities: SearchOpportunities |
| 59 | + _search_opportunities: SearchOpportunities | None |
| 60 | + _search_opportunities_async: SearchOpportunitiesAsync | None |
| 61 | + _get_opportunity_collection: GetOpportunityCollection | None |
58 | 62 |
|
59 | 63 | def __init__(
|
60 | 64 | self,
|
61 | 65 | *args,
|
62 |
| - create_order: CreateOrder, |
63 |
| - search_opportunities: SearchOpportunities, |
64 | 66 | constraints: type[Constraints],
|
65 | 67 | opportunity_properties: type[OpportunityProperties],
|
66 | 68 | order_parameters: type[OrderParameters],
|
| 69 | + create_order: CreateOrder, |
| 70 | + search_opportunities: SearchOpportunities | None = None, |
| 71 | + search_opportunities_async: SearchOpportunitiesAsync | None = None, |
| 72 | + get_opportunity_collection: GetOpportunityCollection | None = None, |
67 | 73 | **kwargs,
|
68 | 74 | ) -> None:
|
69 | 75 | super().__init__(*args, **kwargs)
|
70 |
| - self._create_order = create_order |
71 |
| - self._search_opportunities = search_opportunities |
| 76 | + |
| 77 | + if bool(search_opportunities_async) != bool(get_opportunity_collection): |
| 78 | + raise ValueError( |
| 79 | + "Both the `search_opportunities_async` and `get_opportunity_collection` " |
| 80 | + "arguments must be provided if either is provided" |
| 81 | + ) |
| 82 | + |
72 | 83 | self._constraints = constraints
|
73 | 84 | self._opportunity_properties = opportunity_properties
|
74 | 85 | self._order_parameters = order_parameters
|
| 86 | + self._create_order = create_order |
| 87 | + self._search_opportunities = search_opportunities |
| 88 | + self._search_opportunities_async = search_opportunities_async |
| 89 | + self._get_opportunity_collection = get_opportunity_collection |
75 | 90 |
|
76 | 91 | @property
|
77 |
| - def create_order(self: Self) -> CreateOrder: |
| 92 | + def create_order(self) -> CreateOrder: |
78 | 93 | return self._create_order
|
79 | 94 |
|
80 | 95 | @property
|
81 |
| - def search_opportunities(self: Self) -> SearchOpportunities: |
| 96 | + def search_opportunities(self) -> SearchOpportunities: |
| 97 | + if not self._search_opportunities: |
| 98 | + raise AttributeError("This product does not support opportunity search") |
82 | 99 | return self._search_opportunities
|
83 | 100 |
|
84 | 101 | @property
|
85 |
| - def constraints(self: Self) -> type[Constraints]: |
| 102 | + def search_opportunities_async(self) -> SearchOpportunitiesAsync: |
| 103 | + if not self._search_opportunities_async: |
| 104 | + raise AttributeError( |
| 105 | + "This product does not support async opportunity search" |
| 106 | + ) |
| 107 | + return self._search_opportunities_async |
| 108 | + |
| 109 | + @property |
| 110 | + def get_opportunity_collection(self) -> GetOpportunityCollection: |
| 111 | + if not self._get_opportunity_collection: |
| 112 | + raise AttributeError( |
| 113 | + "This product does not support async opportunity search" |
| 114 | + ) |
| 115 | + return self._get_opportunity_collection |
| 116 | + |
| 117 | + @property |
| 118 | + def constraints(self) -> type[Constraints]: |
86 | 119 | return self._constraints
|
87 | 120 |
|
88 | 121 | @property
|
89 |
| - def opportunity_properties(self: Self) -> type[OpportunityProperties]: |
| 122 | + def opportunity_properties(self) -> type[OpportunityProperties]: |
90 | 123 | return self._opportunity_properties
|
91 | 124 |
|
92 | 125 | @property
|
93 |
| - def order_parameters(self: Self) -> type[OrderParameters]: |
| 126 | + def order_parameters(self) -> type[OrderParameters]: |
94 | 127 | return self._order_parameters
|
95 | 128 |
|
96 |
| - def with_links(self: Self, links: list[Link] | None = None) -> Self: |
| 129 | + @property |
| 130 | + def supports_opportunity_search(self) -> bool: |
| 131 | + return self._search_opportunities is not None |
| 132 | + |
| 133 | + @property |
| 134 | + def supports_async_opportunity_search(self) -> bool: |
| 135 | + return ( |
| 136 | + self._search_opportunities_async is not None |
| 137 | + and self._get_opportunity_collection is not None |
| 138 | + ) |
| 139 | + |
| 140 | + def with_links(self, links: list[Link] | None = None) -> Self: |
97 | 141 | if not links:
|
98 | 142 | return self
|
99 | 143 |
|
|
0 commit comments