Hi all, hoping to get some input on part of my pro...
# ask-community
e
Hi all, hoping to get some input on part of my project. I'm looking to create a job that will pull data from a rest api and eventually store into a database. My initial thought was I would like to create a
Resource
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.
Copy code
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"]
    )
p
If I’m following what you want correctly, I think you just use the resource you created:
Copy code
@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()
probably you would also need to pass either config in for the resource, or use the
configured
api:
Copy code
@job(resource_defs={"book_api": my_books_rest_api_resource.configured({"host": HOST, "snow_user_email": EMAIL, "snow_user_password": PASSWORD})})
def my_job():
    ...
e
Ahh, I didn't realize I could simply call them within an op. I'll give that a try. Appreciate the response prha . cheers