Skip to content

Commit 1362312

Browse files
authored
feat: allow union return types (#71)
* feat: support union types Signed-off-by: Timo Glastra <timo@animo.id> * chore: update version to 0.4.0 Signed-off-by: Timo Glastra <timo@animo.id> * style: black formatting Signed-off-by: Timo Glastra <timo@animo.id>
1 parent f697c88 commit 1362312

File tree

7 files changed

+81
-35
lines changed

7 files changed

+81
-35
lines changed

aries_cloudcontroller/api/credential_definition.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
from aries_cloudcontroller.model.credential_definitions_created_result import (
3030
CredentialDefinitionsCreatedResult,
3131
)
32+
from aries_cloudcontroller.model.txn_or_credential_definition_send_result import (
33+
TxnOrCredentialDefinitionSendResult,
34+
)
3235

3336

3437
class CredentialDefinitionApi(Consumer):
@@ -72,7 +75,7 @@ async def publish_cred_def(
7275
conn_id: Optional[str] = None,
7376
create_transaction_for_endorser: Optional[bool] = None,
7477
body: Optional[CredentialDefinitionSendRequest] = None
75-
) -> CredentialDefinitionSendResult:
78+
) -> Union[CredentialDefinitionSendResult, TxnOrCredentialDefinitionSendResult]:
7679
"""Sends a credential definition to the ledger"""
7780
return await self.__publish_cred_def(
7881
conn_id=conn_id,
@@ -115,5 +118,5 @@ def __publish_cred_def(
115118
conn_id: Query = None,
116119
create_transaction_for_endorser: Query = None,
117120
body: Body(type=CredentialDefinitionSendRequest) = {}
118-
) -> CredentialDefinitionSendResult:
121+
) -> Union[CredentialDefinitionSendResult, TxnOrCredentialDefinitionSendResult]:
119122
"""Internal uplink method for publish_cred_def"""

aries_cloudcontroller/api/revocation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
)
3131
from aries_cloudcontroller.model.rev_regs_created import RevRegsCreated
3232
from aries_cloudcontroller.model.revoke_request import RevokeRequest
33+
from aries_cloudcontroller.model.txn_or_publish_revocations_result import (
34+
TxnOrPublishRevocationsResult,
35+
)
36+
from aries_cloudcontroller.model.txn_or_rev_reg_result import TxnOrRevRegResult
3337

3438

3539
class RevocationApi(Consumer):
@@ -106,7 +110,7 @@ async def publish_rev_reg_def(
106110
rev_reg_id: str,
107111
conn_id: Optional[str] = None,
108112
create_transaction_for_endorser: Optional[bool] = None
109-
) -> RevRegResult:
113+
) -> Union[RevRegResult, TxnOrRevRegResult]:
110114
"""Send revocation registry definition to ledger"""
111115
return await self.__publish_rev_reg_def(
112116
rev_reg_id=rev_reg_id,
@@ -134,7 +138,7 @@ async def publish_revocations(
134138
conn_id: Optional[str] = None,
135139
create_transaction_for_endorser: Optional[bool] = None,
136140
body: Optional[PublishRevocations] = None
137-
) -> PublishRevocations:
141+
) -> Union[PublishRevocations, TxnOrPublishRevocationsResult]:
138142
"""Publish pending revocations to ledger"""
139143
return await self.__publish_revocations(
140144
conn_id=conn_id,
@@ -233,7 +237,7 @@ def __publish_rev_reg_def(
233237
rev_reg_id: str,
234238
conn_id: Query = None,
235239
create_transaction_for_endorser: Query = None
236-
) -> RevRegResult:
240+
) -> Union[RevRegResult, TxnOrRevRegResult]:
237241
"""Internal uplink method for publish_rev_reg_def"""
238242

239243
@returns.json
@@ -256,7 +260,7 @@ def __publish_revocations(
256260
conn_id: Query = None,
257261
create_transaction_for_endorser: Query = None,
258262
body: Body(type=PublishRevocations) = {}
259-
) -> PublishRevocations:
263+
) -> Union[PublishRevocations, TxnOrPublishRevocationsResult]:
260264
"""Internal uplink method for publish_revocations"""
261265

262266
@returns.json

aries_cloudcontroller/api/schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from aries_cloudcontroller.model.schema_send_request import SchemaSendRequest
2222
from aries_cloudcontroller.model.schema_send_result import SchemaSendResult
2323
from aries_cloudcontroller.model.schemas_created_result import SchemasCreatedResult
24+
from aries_cloudcontroller.model.txn_or_schema_send_result import TxnOrSchemaSendResult
2425

2526

2627
class SchemaApi(Consumer):
@@ -52,7 +53,7 @@ async def publish_schema(
5253
conn_id: Optional[str] = None,
5354
create_transaction_for_endorser: Optional[bool] = None,
5455
body: Optional[SchemaSendRequest] = None
55-
) -> SchemaSendResult:
56+
) -> Union[SchemaSendResult, TxnOrSchemaSendResult]:
5657
"""Sends a schema to the ledger"""
5758
return await self.__publish_schema(
5859
conn_id=conn_id,
@@ -92,7 +93,7 @@ def __publish_schema(
9293
conn_id: Query = None,
9394
create_transaction_for_endorser: Query = None,
9495
body: Body(type=SchemaSendRequest) = {}
95-
) -> SchemaSendResult:
96+
) -> Union[SchemaSendResult, TxnOrSchemaSendResult]:
9697
"""Internal uplink method for publish_schema"""
9798

9899
@returns.json

aries_cloudcontroller/util/pydantic_converter.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
to deserialize and serialize values.
44
"""
55

6-
from typing import Any
6+
from typing import Any, Union
7+
import typing
78
from pydantic.json import ENCODERS_BY_TYPE
89
from uplink.converters.interfaces import Factory, Converter
910
from uplink.utils import is_subclass
@@ -66,6 +67,16 @@ def convert(self, response):
6667
except AttributeError:
6768
data = response
6869

70+
# workaround because uplink doesn't support Union types
71+
# see https://github.com/prkumar/uplink/issues/233
72+
if typing.get_origin(self._model) is Union:
73+
74+
class UnionContainer(BaseModel):
75+
v: self._model
76+
77+
data = {"v": data}
78+
return UnionContainer.parse_obj(data).v
79+
6980
return self._model.parse_obj(data)
7081

7182

@@ -97,6 +108,17 @@ def get_users(self, username) -> List[UserModel]:
97108
def _get_model(self, type_):
98109
if is_subclass(type_, BaseModel):
99110
return type_
111+
# workaround because uplink doesn't support Union types
112+
# see https://github.com/prkumar/uplink/issues/233
113+
elif typing.get_origin(type_) is Union:
114+
typing_args = typing.get_args(type_)
115+
all_are_models = all(
116+
[is_subclass(inner_type, BaseModel) for inner_type in typing_args]
117+
)
118+
119+
if all_are_models:
120+
return type_
121+
100122
raise ValueError("Expected pydantic.BaseModel subclass or instance")
101123

102124
def _make_converter(self, converter, type_):

generator/data/openapi.patch

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
diff --git a/generator/data/openapi.yml b/generator/data/openapi.yml
2-
index 81a975a..647ae7e 100644
2+
index 81a975a..ab4c13c 100644
33
--- a/generator/data/openapi.yml
44
+++ b/generator/data/openapi.yml
5-
@@ -705,7 +705,7 @@ paths:
5+
@@ -705,7 +705,9 @@ paths:
66
content:
77
application/json:
88
schema:
99
- $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
10-
+ $ref: '#/components/schemas/CredentialDefinitionSendResult'
10+
+ oneOf:
11+
+ - $ref: '#/components/schemas/CredentialDefinitionSendResult'
12+
+ - $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
1113
x-codegen-request-body-name: body
1214
operationId: publish_cred_def
1315
/credential-definitions/created:
14-
@@ -3322,7 +3322,7 @@ paths:
16+
@@ -3322,7 +3324,9 @@ paths:
1517
content:
1618
application/json:
1719
schema:
1820
- $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
19-
+ $ref: '#/components/schemas/PublishRevocations'
21+
+ oneOf:
22+
+ - $ref: '#/components/schemas/PublishRevocations'
23+
+ - $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
2024
x-codegen-request-body-name: body
2125
operationId: publish_revocations
2226
/revocation/registries/created:
23-
@@ -3433,7 +3433,7 @@ paths:
27+
@@ -3433,7 +3437,9 @@ paths:
2428
content:
2529
application/json:
2630
schema:
2731
- $ref: '#/components/schemas/TxnOrRevRegResult'
28-
+ $ref: '#/components/schemas/RevRegResult'
32+
+ oneOf:
33+
+ - $ref: '#/components/schemas/RevRegResult'
34+
+ - $ref: '#/components/schemas/TxnOrRevRegResult'
2935
operationId: publish_rev_reg_def
3036
/revocation/registry/{rev_reg_id}/entry:
3137
post:
32-
@@ -3610,7 +3610,7 @@ paths:
38+
@@ -3610,7 +3616,9 @@ paths:
3339
content:
3440
application/json:
3541
schema:
3642
- $ref: '#/components/schemas/TxnOrSchemaSendResult'
37-
+ $ref: '#/components/schemas/SchemaSendResult'
43+
+ oneOf:
44+
+ - $ref: '#/components/schemas/SchemaSendResult'
45+
+ - $ref: '#/components/schemas/TxnOrSchemaSendResult'
3846
x-codegen-request-body-name: body
3947
operationId: publish_schema
4048
/schemas/created:
41-
@@ -5045,7 +5045,8 @@ components:
49+
@@ -5045,7 +5053,8 @@ components:
4250
description: Tag within credential definition identifier
4351
example: tag
4452
type:
@@ -48,7 +56,7 @@ index 81a975a..647ae7e 100644
4856
description: 'Signature type: CL for Camenisch-Lysyanskaya'
4957
example: CL
5058
value:
51-
@@ -5087,6 +5088,8 @@ components:
59+
@@ -5087,6 +5096,8 @@ components:
5260
example: default
5361
CredentialDefinitionSendResult:
5462
type: object
@@ -57,7 +65,7 @@ index 81a975a..647ae7e 100644
5765
properties:
5866
credential_definition_id:
5967
pattern: ^([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{21,22}):3:CL:(([1-9][0-9]*)|([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{21,22}:2:.+:[0-9.]+)):(.+)?$
60-
@@ -6111,18 +6114,14 @@ components:
68+
@@ -6111,18 +6122,14 @@ components:
6169
type: object
6270
properties:
6371
from:
@@ -76,7 +84,7 @@ index 81a975a..647ae7e 100644
7684
example: 1640995199
7785
IndyProofReqPredSpec:
7886
required:
79-
@@ -6168,18 +6167,14 @@ components:
87+
@@ -6168,18 +6175,14 @@ components:
8088
type: object
8189
properties:
8290
from:
@@ -95,7 +103,7 @@ index 81a975a..647ae7e 100644
95103
example: 1640995199
96104
IndyProofRequest:
97105
type: object
98-
@@ -6217,18 +6212,14 @@ components:
106+
@@ -6217,18 +6220,14 @@ components:
99107
type: object
100108
properties:
101109
from:
@@ -114,7 +122,7 @@ index 81a975a..647ae7e 100644
114122
example: 1640995199
115123
IndyProofRequestedProof:
116124
type: object
117-
@@ -7394,6 +7385,8 @@ components:
125+
@@ -7394,6 +7393,8 @@ components:
118126
example: 0
119127
RevRegResult:
120128
type: object
@@ -123,15 +131,15 @@ index 81a975a..647ae7e 100644
123131
properties:
124132
result:
125133
$ref: '#/components/schemas/IssuerRevRegRecord'
126-
@@ -7545,6 +7538,7 @@ components:
134+
@@ -7545,6 +7546,7 @@ components:
127135
SchemaSendResult:
128136
required:
129137
- schema_id
130138
+ - schema
131139
type: object
132140
properties:
133141
schema:
134-
@@ -7683,10 +7677,7 @@ components:
142+
@@ -7683,10 +7685,7 @@ components:
135143
mechanism:
136144
type: string
137145
time:
@@ -142,31 +150,31 @@ index 81a975a..647ae7e 100644
142150
example: 1640995199
143151
TAAInfo:
144152
type: object
145-
@@ -7831,6 +7822,7 @@ components:
153+
@@ -7831,6 +7830,7 @@ components:
146154
example: 2021-12-31 23:59:59+00:00
147155
TxnOrCredentialDefinitionSendResult:
148156
type: object
149157
+ additionalProperties: false
150158
properties:
151159
sent:
152160
$ref: '#/components/schemas/CredentialDefinitionSendResult'
153-
@@ -7841,6 +7833,7 @@ components:
161+
@@ -7841,6 +7841,7 @@ components:
154162
- $ref: '#/components/schemas/TransactionRecord'
155163
TxnOrPublishRevocationsResult:
156164
type: object
157165
+ additionalProperties: false
158166
properties:
159167
sent:
160168
$ref: '#/components/schemas/PublishRevocations'
161-
@@ -7851,6 +7844,7 @@ components:
169+
@@ -7851,6 +7852,7 @@ components:
162170
- $ref: '#/components/schemas/TransactionRecord'
163171
TxnOrRevRegResult:
164172
type: object
165173
+ additionalProperties: false
166174
properties:
167175
sent:
168176
$ref: '#/components/schemas/RevRegResult'
169-
@@ -7861,6 +7855,7 @@ components:
177+
@@ -7861,6 +7863,7 @@ components:
170178
- $ref: '#/components/schemas/TransactionRecord'
171179
TxnOrSchemaSendResult:
172180
type: object

generator/data/openapi.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,9 @@ paths:
705705
content:
706706
application/json:
707707
schema:
708-
$ref: '#/components/schemas/CredentialDefinitionSendResult'
708+
oneOf:
709+
- $ref: '#/components/schemas/CredentialDefinitionSendResult'
710+
- $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
709711
x-codegen-request-body-name: body
710712
operationId: publish_cred_def
711713
/credential-definitions/created:
@@ -3322,7 +3324,9 @@ paths:
33223324
content:
33233325
application/json:
33243326
schema:
3325-
$ref: '#/components/schemas/PublishRevocations'
3327+
oneOf:
3328+
- $ref: '#/components/schemas/PublishRevocations'
3329+
- $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
33263330
x-codegen-request-body-name: body
33273331
operationId: publish_revocations
33283332
/revocation/registries/created:
@@ -3433,7 +3437,9 @@ paths:
34333437
content:
34343438
application/json:
34353439
schema:
3436-
$ref: '#/components/schemas/RevRegResult'
3440+
oneOf:
3441+
- $ref: '#/components/schemas/RevRegResult'
3442+
- $ref: '#/components/schemas/TxnOrRevRegResult'
34373443
operationId: publish_rev_reg_def
34383444
/revocation/registry/{rev_reg_id}/entry:
34393445
post:
@@ -3610,7 +3616,9 @@ paths:
36103616
content:
36113617
application/json:
36123618
schema:
3613-
$ref: '#/components/schemas/SchemaSendResult'
3619+
oneOf:
3620+
- $ref: '#/components/schemas/SchemaSendResult'
3621+
- $ref: '#/components/schemas/TxnOrSchemaSendResult'
36143622
x-codegen-request-body-name: body
36153623
operationId: publish_schema
36163624
/schemas/created:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def parse_requirements(filename: str):
1818
if __name__ == "__main__":
1919
setup(
2020
name=PACKAGE_NAME,
21-
version="0.3.2",
21+
version="0.4.0",
2222
description="A simple python package for controlling an aries agent through the admin-api interface",
2323
long_description=long_description,
2424
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)