-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2330 from jupyter-naas/2329-stripe-list-all-products
feat: Stripe - List all products
- Loading branch information
Showing
2 changed files
with
283 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6dd505fc-4ef5-4095-9b25-d4611a2dc836", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8a8ceffa-9042-4a41-ab52-658704c0b5fd", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# Stripe - List all products" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "47ff49ac-4f44-42f1-b5d1-58af6a0bb6c5", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #stripe #api #products #list #python #reference" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8e9a0d86-9354-4f44-8b1b-02055e54d8e0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3616e9da-3b7c-4381-8dc6-3f32337e615c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-10-23 (Created: 2023-10-23)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0426fb42-3925-41e4-b435-cdf2cb21d923", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook returns a list of products sorted by creation date, with the most recently created products appearing first. It is usefull for organizations to quickly access their products." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8446b1d5-0573-46b9-8c0b-a66cf8e6ea75", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [Stripe API Documentation](https://stripe.com/docs/api/products/list)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "59ea2a4a-0b1d-40ea-bf52-add7bea46970", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6c0aa6f5-b456-474a-97c2-f83eb5c756d0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "ae165316-8ef0-409e-a9d3-a5332fe2e800", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"import naas\n", | ||
"from pprint import pprint" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "772e77c5-eb07-4332-bcb5-42d1bb07bbe5", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `api_key`: [API key](https://stripe.com/docs/keys) used to authenticate with Stripe" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f5fc764a-1077-4660-89ca-2ed1933d8f09", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"api_key = naas.secret.get(\"STRIPE_API_KEY_TEST\") or \"sk_test_4eC39HqLyjWDarjtT1zdp7dc\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6a2e8d48-d361-41cb-a20e-a21282c21c47", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cf14ea12-fe22-44b8-9a33-64b66292b115", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### List all products" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e3c30204-948b-40fe-a0df-2c64499f3b1b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def list_all_products(api_key):\n", | ||
" # Init\n", | ||
" stripe_endpoint = \"https://api.stripe.com/v1/products\"\n", | ||
" data = []\n", | ||
"\n", | ||
" # Define headers with authorization using Stripe API key\n", | ||
" headers = {\n", | ||
" \"Authorization\": f\"Bearer {api_key}\"\n", | ||
" }\n", | ||
"\n", | ||
" # Send GET request to Stripe API to list customers\n", | ||
" response = requests.get(stripe_endpoint, headers=headers)\n", | ||
"\n", | ||
" # Check for successful response (status code 200)\n", | ||
" if response.status_code == 200:\n", | ||
" # Get the customers from the response\n", | ||
" data = response.json()[\"data\"]\n", | ||
"\n", | ||
" # Check if there are more customers to fetch\n", | ||
" while response.json()[\"has_more\"]:\n", | ||
" # Set the starting_after parameter to the last customer ID from the previous response\n", | ||
" starting_after = data[-1][\"id\"]\n", | ||
"\n", | ||
" # Send GET request with the starting_after parameter to fetch the next set of customers\n", | ||
" response = requests.get(\n", | ||
" stripe_endpoint,\n", | ||
" headers=headers,\n", | ||
" params={\"starting_after\": starting_after}\n", | ||
" )\n", | ||
"\n", | ||
" # Check for successful response (status code 200)\n", | ||
" if response.status_code == 200:\n", | ||
" # Get the customers from the response\n", | ||
" data.append(response.json()[\"data\"])\n", | ||
" return data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fdba1769-5243-4794-b671-962d00b436ef", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4ced1927-8616-4e28-a97c-476bb3873c31", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "afeeaa59-d6aa-4eae-95d9-70c6ae6dcba7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"results = list_all_products(api_key)\n", | ||
"print(\"✅ Products:\", len(results))\n", | ||
"if len(results) > 0:\n", | ||
" pprint(results[0])" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |