Skip to content

Commit

Permalink
feat: allow union return types (#71)
Browse files Browse the repository at this point in the history
* feat: support union types

Signed-off-by: Timo Glastra <[email protected]>

* chore: update version to 0.4.0

Signed-off-by: Timo Glastra <[email protected]>

* style: black formatting

Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Nov 17, 2021
1 parent f697c88 commit 1362312
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
7 changes: 5 additions & 2 deletions aries_cloudcontroller/api/credential_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from aries_cloudcontroller.model.credential_definitions_created_result import (
CredentialDefinitionsCreatedResult,
)
from aries_cloudcontroller.model.txn_or_credential_definition_send_result import (
TxnOrCredentialDefinitionSendResult,
)


class CredentialDefinitionApi(Consumer):
Expand Down Expand Up @@ -72,7 +75,7 @@ async def publish_cred_def(
conn_id: Optional[str] = None,
create_transaction_for_endorser: Optional[bool] = None,
body: Optional[CredentialDefinitionSendRequest] = None
) -> CredentialDefinitionSendResult:
) -> Union[CredentialDefinitionSendResult, TxnOrCredentialDefinitionSendResult]:
"""Sends a credential definition to the ledger"""
return await self.__publish_cred_def(
conn_id=conn_id,
Expand Down Expand Up @@ -115,5 +118,5 @@ def __publish_cred_def(
conn_id: Query = None,
create_transaction_for_endorser: Query = None,
body: Body(type=CredentialDefinitionSendRequest) = {}
) -> CredentialDefinitionSendResult:
) -> Union[CredentialDefinitionSendResult, TxnOrCredentialDefinitionSendResult]:
"""Internal uplink method for publish_cred_def"""
12 changes: 8 additions & 4 deletions aries_cloudcontroller/api/revocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
)
from aries_cloudcontroller.model.rev_regs_created import RevRegsCreated
from aries_cloudcontroller.model.revoke_request import RevokeRequest
from aries_cloudcontroller.model.txn_or_publish_revocations_result import (
TxnOrPublishRevocationsResult,
)
from aries_cloudcontroller.model.txn_or_rev_reg_result import TxnOrRevRegResult


class RevocationApi(Consumer):
Expand Down Expand Up @@ -106,7 +110,7 @@ async def publish_rev_reg_def(
rev_reg_id: str,
conn_id: Optional[str] = None,
create_transaction_for_endorser: Optional[bool] = None
) -> RevRegResult:
) -> Union[RevRegResult, TxnOrRevRegResult]:
"""Send revocation registry definition to ledger"""
return await self.__publish_rev_reg_def(
rev_reg_id=rev_reg_id,
Expand Down Expand Up @@ -134,7 +138,7 @@ async def publish_revocations(
conn_id: Optional[str] = None,
create_transaction_for_endorser: Optional[bool] = None,
body: Optional[PublishRevocations] = None
) -> PublishRevocations:
) -> Union[PublishRevocations, TxnOrPublishRevocationsResult]:
"""Publish pending revocations to ledger"""
return await self.__publish_revocations(
conn_id=conn_id,
Expand Down Expand Up @@ -233,7 +237,7 @@ def __publish_rev_reg_def(
rev_reg_id: str,
conn_id: Query = None,
create_transaction_for_endorser: Query = None
) -> RevRegResult:
) -> Union[RevRegResult, TxnOrRevRegResult]:
"""Internal uplink method for publish_rev_reg_def"""

@returns.json
Expand All @@ -256,7 +260,7 @@ def __publish_revocations(
conn_id: Query = None,
create_transaction_for_endorser: Query = None,
body: Body(type=PublishRevocations) = {}
) -> PublishRevocations:
) -> Union[PublishRevocations, TxnOrPublishRevocationsResult]:
"""Internal uplink method for publish_revocations"""

@returns.json
Expand Down
5 changes: 3 additions & 2 deletions aries_cloudcontroller/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from aries_cloudcontroller.model.schema_send_request import SchemaSendRequest
from aries_cloudcontroller.model.schema_send_result import SchemaSendResult
from aries_cloudcontroller.model.schemas_created_result import SchemasCreatedResult
from aries_cloudcontroller.model.txn_or_schema_send_result import TxnOrSchemaSendResult


class SchemaApi(Consumer):
Expand Down Expand Up @@ -52,7 +53,7 @@ async def publish_schema(
conn_id: Optional[str] = None,
create_transaction_for_endorser: Optional[bool] = None,
body: Optional[SchemaSendRequest] = None
) -> SchemaSendResult:
) -> Union[SchemaSendResult, TxnOrSchemaSendResult]:
"""Sends a schema to the ledger"""
return await self.__publish_schema(
conn_id=conn_id,
Expand Down Expand Up @@ -92,7 +93,7 @@ def __publish_schema(
conn_id: Query = None,
create_transaction_for_endorser: Query = None,
body: Body(type=SchemaSendRequest) = {}
) -> SchemaSendResult:
) -> Union[SchemaSendResult, TxnOrSchemaSendResult]:
"""Internal uplink method for publish_schema"""

@returns.json
Expand Down
24 changes: 23 additions & 1 deletion aries_cloudcontroller/util/pydantic_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
to deserialize and serialize values.
"""

from typing import Any
from typing import Any, Union
import typing
from pydantic.json import ENCODERS_BY_TYPE
from uplink.converters.interfaces import Factory, Converter
from uplink.utils import is_subclass
Expand Down Expand Up @@ -66,6 +67,16 @@ def convert(self, response):
except AttributeError:
data = response

# workaround because uplink doesn't support Union types
# see https://github.com/prkumar/uplink/issues/233
if typing.get_origin(self._model) is Union:

class UnionContainer(BaseModel):
v: self._model

data = {"v": data}
return UnionContainer.parse_obj(data).v

return self._model.parse_obj(data)


Expand Down Expand Up @@ -97,6 +108,17 @@ def get_users(self, username) -> List[UserModel]:
def _get_model(self, type_):
if is_subclass(type_, BaseModel):
return type_
# workaround because uplink doesn't support Union types
# see https://github.com/prkumar/uplink/issues/233
elif typing.get_origin(type_) is Union:
typing_args = typing.get_args(type_)
all_are_models = all(
[is_subclass(inner_type, BaseModel) for inner_type in typing_args]
)

if all_are_models:
return type_

raise ValueError("Expected pydantic.BaseModel subclass or instance")

def _make_converter(self, converter, type_):
Expand Down
50 changes: 29 additions & 21 deletions generator/data/openapi.patch
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
diff --git a/generator/data/openapi.yml b/generator/data/openapi.yml
index 81a975a..647ae7e 100644
index 81a975a..ab4c13c 100644
--- a/generator/data/openapi.yml
+++ b/generator/data/openapi.yml
@@ -705,7 +705,7 @@ paths:
@@ -705,7 +705,9 @@ paths:
content:
application/json:
schema:
- $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
+ $ref: '#/components/schemas/CredentialDefinitionSendResult'
+ oneOf:
+ - $ref: '#/components/schemas/CredentialDefinitionSendResult'
+ - $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
x-codegen-request-body-name: body
operationId: publish_cred_def
/credential-definitions/created:
@@ -3322,7 +3322,7 @@ paths:
@@ -3322,7 +3324,9 @@ paths:
content:
application/json:
schema:
- $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
+ $ref: '#/components/schemas/PublishRevocations'
+ oneOf:
+ - $ref: '#/components/schemas/PublishRevocations'
+ - $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
x-codegen-request-body-name: body
operationId: publish_revocations
/revocation/registries/created:
@@ -3433,7 +3433,7 @@ paths:
@@ -3433,7 +3437,9 @@ paths:
content:
application/json:
schema:
- $ref: '#/components/schemas/TxnOrRevRegResult'
+ $ref: '#/components/schemas/RevRegResult'
+ oneOf:
+ - $ref: '#/components/schemas/RevRegResult'
+ - $ref: '#/components/schemas/TxnOrRevRegResult'
operationId: publish_rev_reg_def
/revocation/registry/{rev_reg_id}/entry:
post:
@@ -3610,7 +3610,7 @@ paths:
@@ -3610,7 +3616,9 @@ paths:
content:
application/json:
schema:
- $ref: '#/components/schemas/TxnOrSchemaSendResult'
+ $ref: '#/components/schemas/SchemaSendResult'
+ oneOf:
+ - $ref: '#/components/schemas/SchemaSendResult'
+ - $ref: '#/components/schemas/TxnOrSchemaSendResult'
x-codegen-request-body-name: body
operationId: publish_schema
/schemas/created:
@@ -5045,7 +5045,8 @@ components:
@@ -5045,7 +5053,8 @@ components:
description: Tag within credential definition identifier
example: tag
type:
Expand All @@ -48,7 +56,7 @@ index 81a975a..647ae7e 100644
description: 'Signature type: CL for Camenisch-Lysyanskaya'
example: CL
value:
@@ -5087,6 +5088,8 @@ components:
@@ -5087,6 +5096,8 @@ components:
example: default
CredentialDefinitionSendResult:
type: object
Expand All @@ -57,7 +65,7 @@ index 81a975a..647ae7e 100644
properties:
credential_definition_id:
pattern: ^([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{21,22}):3:CL:(([1-9][0-9]*)|([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{21,22}:2:.+:[0-9.]+)):(.+)?$
@@ -6111,18 +6114,14 @@ components:
@@ -6111,18 +6122,14 @@ components:
type: object
properties:
from:
Expand All @@ -76,7 +84,7 @@ index 81a975a..647ae7e 100644
example: 1640995199
IndyProofReqPredSpec:
required:
@@ -6168,18 +6167,14 @@ components:
@@ -6168,18 +6175,14 @@ components:
type: object
properties:
from:
Expand All @@ -95,7 +103,7 @@ index 81a975a..647ae7e 100644
example: 1640995199
IndyProofRequest:
type: object
@@ -6217,18 +6212,14 @@ components:
@@ -6217,18 +6220,14 @@ components:
type: object
properties:
from:
Expand All @@ -114,7 +122,7 @@ index 81a975a..647ae7e 100644
example: 1640995199
IndyProofRequestedProof:
type: object
@@ -7394,6 +7385,8 @@ components:
@@ -7394,6 +7393,8 @@ components:
example: 0
RevRegResult:
type: object
Expand All @@ -123,15 +131,15 @@ index 81a975a..647ae7e 100644
properties:
result:
$ref: '#/components/schemas/IssuerRevRegRecord'
@@ -7545,6 +7538,7 @@ components:
@@ -7545,6 +7546,7 @@ components:
SchemaSendResult:
required:
- schema_id
+ - schema
type: object
properties:
schema:
@@ -7683,10 +7677,7 @@ components:
@@ -7683,10 +7685,7 @@ components:
mechanism:
type: string
time:
Expand All @@ -142,31 +150,31 @@ index 81a975a..647ae7e 100644
example: 1640995199
TAAInfo:
type: object
@@ -7831,6 +7822,7 @@ components:
@@ -7831,6 +7830,7 @@ components:
example: 2021-12-31 23:59:59+00:00
TxnOrCredentialDefinitionSendResult:
type: object
+ additionalProperties: false
properties:
sent:
$ref: '#/components/schemas/CredentialDefinitionSendResult'
@@ -7841,6 +7833,7 @@ components:
@@ -7841,6 +7841,7 @@ components:
- $ref: '#/components/schemas/TransactionRecord'
TxnOrPublishRevocationsResult:
type: object
+ additionalProperties: false
properties:
sent:
$ref: '#/components/schemas/PublishRevocations'
@@ -7851,6 +7844,7 @@ components:
@@ -7851,6 +7852,7 @@ components:
- $ref: '#/components/schemas/TransactionRecord'
TxnOrRevRegResult:
type: object
+ additionalProperties: false
properties:
sent:
$ref: '#/components/schemas/RevRegResult'
@@ -7861,6 +7855,7 @@ components:
@@ -7861,6 +7863,7 @@ components:
- $ref: '#/components/schemas/TransactionRecord'
TxnOrSchemaSendResult:
type: object
Expand Down
16 changes: 12 additions & 4 deletions generator/data/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/CredentialDefinitionSendResult'
oneOf:
- $ref: '#/components/schemas/CredentialDefinitionSendResult'
- $ref: '#/components/schemas/TxnOrCredentialDefinitionSendResult'
x-codegen-request-body-name: body
operationId: publish_cred_def
/credential-definitions/created:
Expand Down Expand Up @@ -3322,7 +3324,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/PublishRevocations'
oneOf:
- $ref: '#/components/schemas/PublishRevocations'
- $ref: '#/components/schemas/TxnOrPublishRevocationsResult'
x-codegen-request-body-name: body
operationId: publish_revocations
/revocation/registries/created:
Expand Down Expand Up @@ -3433,7 +3437,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/RevRegResult'
oneOf:
- $ref: '#/components/schemas/RevRegResult'
- $ref: '#/components/schemas/TxnOrRevRegResult'
operationId: publish_rev_reg_def
/revocation/registry/{rev_reg_id}/entry:
post:
Expand Down Expand Up @@ -3610,7 +3616,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaSendResult'
oneOf:
- $ref: '#/components/schemas/SchemaSendResult'
- $ref: '#/components/schemas/TxnOrSchemaSendResult'
x-codegen-request-body-name: body
operationId: publish_schema
/schemas/created:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parse_requirements(filename: str):
if __name__ == "__main__":
setup(
name=PACKAGE_NAME,
version="0.3.2",
version="0.4.0",
description="A simple python package for controlling an aries agent through the admin-api interface",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 1362312

Please sign in to comment.