From 78574da0f6712ca84f3a5bcdbde7e312daf48a19 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:11:03 -0400 Subject: [PATCH] Make `ObjectStorageACL` a `StrEnum` (#420) --- linode_api4/objects/object_storage.py | 8 +++++--- linode_api4/objects/serializable.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/linode_api4/objects/object_storage.py b/linode_api4/objects/object_storage.py index 022fd2a6..11df847d 100644 --- a/linode_api4/objects/object_storage.py +++ b/linode_api4/objects/object_storage.py @@ -1,3 +1,4 @@ +from typing import Optional from urllib import parse from linode_api4.errors import UnexpectedResponseError @@ -8,10 +9,11 @@ Property, Region, ) +from linode_api4.objects.serializable import StrEnum from linode_api4.util import drop_null_keys -class ObjectStorageACL: +class ObjectStorageACL(StrEnum): PRIVATE = "private" PUBLIC_READ = "public-read" AUTHENTICATED_READ = "authenticated-read" @@ -67,7 +69,7 @@ def make_instance(cls, id, client, parent_id=None, json=None): def access_modify( self, - acl: ObjectStorageACL = None, + acl: Optional[ObjectStorageACL] = None, cors_enabled=None, ): """ @@ -109,7 +111,7 @@ def access_modify( def access_update( self, - acl: ObjectStorageACL = None, + acl: Optional[ObjectStorageACL] = None, cors_enabled=None, ): """ diff --git a/linode_api4/objects/serializable.py b/linode_api4/objects/serializable.py index 15494cdc..b0e7a250 100644 --- a/linode_api4/objects/serializable.py +++ b/linode_api4/objects/serializable.py @@ -1,5 +1,6 @@ import inspect from dataclasses import dataclass +from enum import Enum from types import SimpleNamespace from typing import ( Any, @@ -223,3 +224,22 @@ def __delitem__(self, key): def __len__(self): return len(vars(self)) + + +class StrEnum(str, Enum): + """ + Used for enums that are of type string, which is necessary + for implicit JSON serialization. + + NOTE: Replace this with StrEnum once Python 3.10 has been EOL'd. + See: https://docs.python.org/3/library/enum.html#enum.StrEnum + """ + + def __new__(cls, *values): + value = str(*values) + member = str.__new__(cls, value) + member._value_ = value + return member + + def __str__(self): + return self._value_