2
\$\begingroup\$
from enum import Enum
from fastapi import FastAPI
app = FastAPI()

Hi Defined a route and a Enum Class for the same but the Enum Class looks ugly.

class Subjects(str, Enum):
    path1="AcceptedEventRelation"
    path2="Account"
    path3="AccountChangeEvent"
    path4="AccountCleanInfo"
    path5="AccountContactRole"
    ...
    ...
    path100


@app.get("/subjects/{sobjectname:str}")
async def process_subjects(sobjectname:Subjects):
    endpoint_subjects = "/services/data/v49.0/sobjects"
    url_to_request = endpoint_subjects + "/" + sobjectname
    return {
        "subject" : sobjectname,
        "url_to_request": url_to_request
        }

Is there a better way to implement the Subjects Class. Thanks

\$\endgroup\$
2
  • \$\begingroup\$ The fact is that you need the LHS and RHS somewhere in your code, in some form. It can be a list, or dict, or in some other forms. \$\endgroup\$ Commented Aug 5, 2020 at 6:45
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$ Commented Aug 5, 2020 at 9:13

1 Answer 1

1
\$\begingroup\$

The fact is that you need the LHS and RHS somewhere in your code, in some form. It can be a list, or dict, or in some other forms.

For example:

from enum import Enum
from fastapi import FastAPI

app = FastAPI()
some_dict = dict(
    path1="AcceptedEventRelation",
    path2="Account",
    path3="AccountChangeEvent",
    path4="AccountCleanInfo",
    path5="AccountContactRole"
)

Subjects = Enum('Subjects', some_dict)


@app.get("/subjects/{sobjectname}")
async def process_subjects(sobjectname: Subjects):
    endpoint_subjects = "/services/data/v49.0/sobjects"
    url_to_request = endpoint_subjects + "/" + sobjectname.value
    return {
        "subject": sobjectname,
        "url_to_request": url_to_request
    }

Here, the some_dict is the variable you need to generate, which also looks ugly if there is a 100 items in it as the way you feel for the Enum class.

\$\endgroup\$
7
  • \$\begingroup\$ what is MyDynamicEnum referes to ? .. when I call the endpoint I get, TypeError: can only concatenate str (not "MyDynamicEnum") to str \$\endgroup\$ Commented Aug 5, 2020 at 7:39
  • \$\begingroup\$ MyDynamicEnum is the class name \$\endgroup\$ Commented Aug 5, 2020 at 7:47
  • \$\begingroup\$ ok but how canI fix the above error \$\endgroup\$ Commented Aug 5, 2020 at 8:15
  • \$\begingroup\$ probably you should show the error traceback \$\endgroup\$ Commented Aug 5, 2020 at 8:20
  • \$\begingroup\$ error traceback added in the question, pls check \$\endgroup\$ Commented Aug 5, 2020 at 8:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.