liveBook · Manning

Requests

As explained in Snowflake Data Engineering

Requests is a Python library for sending HTTP requests and receiving HTTP responses.

Requests is a Python library used inside a Snowflake Python UDF to call an external HTTP API and return its response for pipeline enrichment. section 7.2.1

A UDF can use requests to retrieve customer reviews through this sequence:

  • Register requests in the UDF packages parameter and import it in the Python handler. section 7.2.1
  • Retrieve the API key from the configured Snowflake secret. section 7.2.1
  • Construct the Yelp reviews endpoint from the supplied business alias. section 7.2.1
  • Call requests.get with the URL and bearer-token authorization, then return response.json(). section 7.2.1
Listing 7.1 The definition of the UDF
create or replace function GET_CUSTOMER_REVIEWS(business_alias varchar)
returns variant
language python
runtime_version = 3.10
handler = 'get_reviews'
external_access_integrations = (YELP_API_INTEGRATION)
secrets = ('yelp_api_token' = YELP_API_TOKEN)
packages = ('requests')
AS     #1

The UDF definition registers Python 3.10, the handler, external access integration, secret mapping, and requests package. section 7.2.1

Listing 7.2 The body of the UDF
$$     #1
import _snowflake
import requests

def get_reviews(business_alias):
  api_key = _snowflake.get_generic_secret_string('yelp_api_token')
  url = f'''https://api.yelp.com/v3/businesses/{business_alias}/reviews'''
  response = requests.get(
    url=url, 
    headers = {'Authorization': 'Bearer ' + api_key}
  )
  return response.json()
$$;

The handler imports requests, retrieves the secret, builds the Yelp URL, sends an authenticated GET request, and returns JSON. section 7.2.1

Querying the UDF

After creation, select the function with a business alias such as boulangerie-julien-paris-3; the result is returned as a VARIANT containing the API response. section 7.2.1

How Snowflake Data Engineering covers it

How deep it goes
Defines package registration, imports, secret retrieval, URL construction, authentication, request execution, and JSON return, but defers response flattening.
Examples
Uses Yelp's reviews endpoint, business aliases, bearer-token authorization, and a GET_CUSTOMER_REVIEWS SQL call.
What it assumes
Assumes an external access integration, API endpoint, API secret, and USAGE and READ privileges for external access.
What you can do after
Apply it via requests by registering the package in a Python UDF and returning an authenticated API response as JSON.
How it connects
Connects Python UDFs with external access integrations, secrets, API endpoints, JSON, and Snowpark.

Also explained in

Compare how 6 books explain requests →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Snowflake Data Engineering. The section links point to the parts of the book it draws on.