unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 6 forks source link

Add `MaybeModel[T]` type for POST/PUT/PATCH endpoint methods #17

Open pederhan opened 1 year ago

pederhan commented 1 year ago

Rationale

It is currently possible to pass dicts to methods that create or update resources. This is not explained anywhere, and is basically an implementation detail.

Being able to pass in dicts to these methods is beneficial if the API spec changes in a backwards-incompatible way in the future, as users will be able to pass in custom dicts conforming to the new spec until an official harborapi update is available, after which they can resume using the models.

Implementation

In order to document this (currently implicit) behavior, we should add a new type called MaybeModel[T] that is an alias for Union[T, dict[str, Any]], where T is bound to BaseModel, and replace current types on POST/PUT/PATCH methods with this type.

from typing import TypeVar, Union, Any
from pydantic import BaseModel

T = TypeVar('T', bound=BaseModel)

MaybeModel = Union[T, dict[str, Any]]