Eric
12/07/2021, 12:30 AMResource
that encapsulated all the methods I would need for communicating with my rest api. However, the problem I'm running into is I don't know how to pass arguments to a resources method. My current workaround is to define the book_name
as Noneable and would only be defined in jobs that use this method within the resource but something seems strange with this setup.
Perhaps I'm thinking / going about this the wrong way ?
Example code below shows where I'm stuck trying to pass in a book_name
into the method fetch_book_by_name
of the resource.
class MyBooksRESTApi:
def __init__(self, host, username, password):
self.host = host
self.username = username
self.password = password
self.json_headers = {
"Content-Type": "application/json"
}
# how to pass book_name argument ?
def fetch_book_by_name(self, book_name):
response = requests.get(
f"<https://www.mycoolsite.com/api/books/{book_name}>",
auth=HTTPBasicAuth(self.username, self.password),
headers=self.json_headers
)
return response
# example method that does NOT need book_name defined
def fetch_all_books(self):
response = requests.get(
f"<https://www.mycoolsite.com/api/books>",
auth=HTTPBasicAuth(self.username, self.password),
headers=self.json_headers
)
return response
@resource(config_schema={
"host": String,
"username": String,
"password": String,
"book_name": Noneable
})
def my_books_rest_api_resource(init_context):
return MyBooksRESTApi(
init_context.resource_config["host"],
init_context.resource_config["snow_user_email"],
init_context.resource_config["snow_user_password"]
)
prha
12/07/2021, 12:33 AM@op(required_resource_keys={"book_api"})
def my_op(context):
book = context.resources.book_api.fetch_book_by_name('the_best_book_ever')
# do something here with book
@job(
resource_defs={"book_api": my_books_rest_api_resource},
):
def my_job():
my_op()
configured
api:
@job(resource_defs={"book_api": my_books_rest_api_resource.configured({"host": HOST, "snow_user_email": EMAIL, "snow_user_password": PASSWORD})})
def my_job():
...
Eric
12/07/2021, 4:23 PM