I need help with what think is called function overriding? Regardless of what it's called here is my issue:
Please note I am not asking for help with the requests module but how to pass in a variable to override or call as specific a class' function.
I have a predefined class that I have to work with and can't modify:
class CustomAPI:
...
def get(some args here)
def post(some args here)
def delete(some args here)
...
the above is a wrapper that calls the requests module and executes based on what you call:
...
my_api = CustomAPI()
my_api.get(some args here)
...
what i need help here is how to pass in get|post|delete as variables into another function so that I can make calls as needed.
...
api_method = 'get'
do_something_fuction(api_method)
...
do_something_function(api_method):
response = my_api.api_method(some args here)
...
it is the api_method part that I cant figure out how to pass that value in so that I can call
response = my_api.get(some args here) or
response = my_api.post(some args here) or
response = my_api.delete(some args here) etc...
it would end up being (essentially the equivalent of bash my_api.$1(some args here)):
response = my_api.api_method(some args here)
I could do:
if api_method = 'get':
my_api.get(some args here)
elif api_method = 'post':
my_api.post(some args here)
elif api_method = 'delete':
my_api.delete(some args here)
but I am hoping there is more of a graceful way to do it with less typing as each if statement has like 6 additional lines that are not shown here that are basically repetitive.
I figured it out. I believe the name of what I am trying to do is:
dynamically instantiate the class method
...
api_method = 'get'
do_something_fuction(api_method)
...
do_something_function(api_method):
ClassMethod = getattr(my_api, api_method)
response = ClassMethod(some args here)
...
this was my missing piece: ClassMethod = getattr(my_api, method)