You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here I have a suggestion to make. After thinking a while about Disgenet API, I've come to a conclusion. Using classes for authorization required APIs is the best practive since it is the perfect balance between data security and ease of use.
Details
If we store the api key in the storage, it's not a good practice for data security. And if we don't, we have to prompt to user for e-mail and password every time we want to make a query. Of course they can be stored in a variable but classes are more elegant way of doing it. Prompting one time for each session is enough in this approach.
How
This base class is an Abstract Class and has a decorator (wrapper if you prefer) to use queries without the hassle of checking authorization status every time in every query. It also checks if the necessary and unique attributes of each child class is implemented.
Code
fromabcimportABC, abstractmethodclassBaseAuthorizedApi(ABC):
_name=NotImplemented_api_url=NotImplemented_authorized=False_api_key=None@abstractmethoddefauthorize(self) ->bool:
passdef__if_authorized(f):
defwrapper(self, *args, **kwargs):
ifself.authorize():
returnf(self, *args, **kwargs)
else:
print('Failure in authorization, check your credentials.')
returnwrapperdef__init_subclass__(cls, **kwargs):
forattrindir(cls):
try:
if (getattr(cls, attr) ==NotImplemented):
raiseNotImplementedError(f'\'{attr}\' attribute is not defined in subclass.')
exceptAttributeError:
pass
The text was updated successfully, but these errors were encountered:
If I got it right, yes, of course. That way there will be no unnecessary login prompts and this base class will prove useful for child classes. I made a correction to the wrapper by the way. You might want to look at it again.
My initial goal was to create a base class for the inputs module. This way, if a contributor wants to write an api for a database, one will know how to do it with consistence with the code base and ease. They just have to use this as a parent class and the parent class will guide them.
Summary
Here I have a suggestion to make. After thinking a while about Disgenet API, I've come to a conclusion. Using classes for authorization required APIs is the best practive since it is the perfect balance between data security and ease of use.
Details
If we store the api key in the storage, it's not a good practice for data security. And if we don't, we have to prompt to user for e-mail and password every time we want to make a query. Of course they can be stored in a variable but classes are more elegant way of doing it. Prompting one time for each session is enough in this approach.
How
This base class is an Abstract Class and has a decorator (wrapper if you prefer) to use queries without the hassle of checking authorization status every time in every query. It also checks if the necessary and unique attributes of each child class is implemented.
Code
The text was updated successfully, but these errors were encountered: