From 20bd98af69f9f4b7e06c6e3d6bc15e0c5e669402 Mon Sep 17 00:00:00 2001 From: Kai Welke Date: Sun, 31 Jul 2022 18:23:00 +0200 Subject: [PATCH 1/3] feat: update Python get started --- .../content-api/integrations/python.md | 291 ++++++++++-------- 1 file changed, 166 insertions(+), 125 deletions(-) diff --git a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md index 16d00b31b7..6c00f0beb8 100644 --- a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md +++ b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md @@ -6,8 +6,6 @@ canonicalUrl: https://docs.strapi.io/developer-docs/latest/developer-resources/c # Getting Started with Python -!!!include(developer-docs/latest/developer-resources/content-api/snippets/integration-guide-not-updated.md)!!! - This integration guide is following the [Quick Start Guide](/developer-docs/latest/getting-started/quick-start.md). We assume that you have fully completed its "Hands-on" path, and therefore can consume the API by browsing this [url](http://localhost:1337/api/restaurants). If you haven't gone through the Quick Start Guide, the way you request a Strapi API with [Python](https://www.python.org/) remains the same except that you will not fetch the same content. @@ -38,66 +36,56 @@ Be sure that you activated the `find` permission for the `restaurant` collection ::: request Example GET request ```python -requests.get("http://localhost:1337/api/restaurants", - headers={ - 'Content-Type': 'application/json' - }) +import requests + +response = requests.get("http://localhost:1337/api/restaurants") +print(response.json()) ``` + ::: ::: response Example response ```json -[ - { - "id": 1, - "name": "Biscotte Restaurant", - "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.", - "created_by": { - "id": 1, - "firstname": "Paul", - "lastname": "Bocuse", - "username": null - }, - "updated_by": { +{ + "data": [ + { "id": 1, - "firstname": "Paul", - "lastname": "Bocuse", - "username": null - }, - "created_at": "2020-07-31T11:37:16.964Z", - "updated_at": "2020-07-31T11:37:16.975Z", - "categories": [ - { - "id": 1, - "name": "French Food", - "created_by": 1, - "updated_by": 1, - "created_at": "2020-07-31T11:36:23.164Z", - "updated_at": "2020-07-31T11:36:23.172Z" + "attributes": { + "name": "Biscotte Restaurant", + "description": "Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.", + "createdAt": "2022-07-31T11:57:01.330Z", + "updatedAt": "2022-07-31T11:57:44.945Z", + "publishedAt": "2022-07-31T11:57:44.943Z" } - ] + } + ], + "meta": { + "pagination": { + "page": 1, + "pageSize": 25, + "pageCount": 1, + "total": 1 + } } -] +} ``` + ::: :::: ### Example ```python -import requests import json +import requests class Restaurant: def __init__(self): - self.api_url = "http://localhost:1337" + self.api_url = "http://localhost:1337/api" def all(self): - r = requests.get(self.api_url + "/restaurants", - headers={ - 'Content-Type': 'application/json' - }) + r = requests.get(self.api_url + "/restaurants") return r.json() restaurant = Restaurant() @@ -109,6 +97,7 @@ print(restaurant.all()) Execute a `POST` request on the `restaurant` collection type in order to create a restaurant. Be sure that you activated the `create` permission for the `restaurant` collection type and the `find` permission for the `category` Collection type. +Add the `?populate=categories` query parameter to return categories with the response. In this example a `japanese` category has been created which has the id: 3. @@ -116,75 +105,104 @@ In this example a `japanese` category has been created which has the id: 3. ::: request Example POST request ```python -requests.post("http://localhost:1337/api/restaurants", -data=json_dumps({ - 'name': 'Dolemon Sushi', - 'description': 'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious', - 'categories': [3], - }), - headers={ - 'Content-Type': 'application/json' - }) +import json +import requests + +response = requests.post( + "http://localhost:1337/api/restaurants", + headers={"Content-Type": "application/json"}, + params={"populate": "categories"}, + data=json.dumps( + { + "data": { + "name": "Dolemon Sushi", + "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious", + "categories": [3], + } + } + ), +) + +print(response.json()) ``` + ::: ::: response Example response ```json { - "id": 2, - "name": "Dolemon Sushi", - "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious", - "created_by": null, - "updated_by": null, - "created_at": "2020-08-04T09:57:11.669Z", - "updated_at": "2020-08-04T09:57:11.669Z", - "categories": [ - { - "id": 3, - "name": "Japanese", - "created_by": 1, - "updated_by": 1, - "created_at": "2020-07-31T11:36:23.164Z", - "updated_at": "2020-07-31T11:36:23.172Z" + "data": { + "id": 2, + "attributes": { + "name": "Dolemon Sushi", + "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious", + "createdAt": "2022-07-31T15:36:56.336Z", + "updatedAt": "2022-07-31T15:36:56.336Z", + "publishedAt": "2022-07-31T15:36:56.336Z", + "categories": { + "data": [ + { + "id": 3, + "attributes": { + "name": "Japanese Food", + "createdAt": "2022-07-31T11:57:35.040Z", + "updatedAt": "2022-07-31T11:57:35.631Z", + "publishedAt": "2022-07-31T11:57:35.629Z" + } + } + ] + } } - ] + }, + "meta": {} } ``` + ::: :::: ### Example ```python -import requests import json +import requests class Restaurant: def __init__(self): - self.api_url = "http://localhost:1337" + self.api_url = "http://localhost:1337/api" def all(self): r = requests.get(self.api_url + "/restaurants") return r.json() def create(self, params): - r = requests.post(self.api_url + "/restaurants", - data=json.dumps({ - 'name': params["name"], - 'description': params["description"], - 'categories': params["categories"] - }), - headers={ - 'Content-Type': 'application/json' - }) + r = requests.post( + self.api_url + "/restaurants", + headers={"Content-Type": "application/json"}, + data=json.dumps( + { + "data": { + "name": params["name"], + "description": params["description"], + "categories": params["categories"], + } + } + ), + ) + return r.json() + restaurant = Restaurant() -print(restaurant.create({ - 'name': 'Dolemon Sushi', - 'description': 'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious', - 'categories': [3], -})) +print( + restaurant.create( + { + "name": "Dolemon Sushi", + "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious", + "categories": [3], + } + ) +) ``` ## PUT Request your collection type @@ -197,80 +215,103 @@ Be sure that you activated the `put` permission for the `restaurant` collection ::: request Example PUT request ```python -requests.put("http://localhost:1337/api/restaurants/2", -data=json_dumps({ - 'categories': [2] -}), -headers={ -'Content-Type': 'application/json' -}) +import json +import requests + +response = requests.put( + "http://localhost:1337/api/restaurants/2", + params={"populate": "categories"}, + headers={"Content-Type": "application/json"}, + data=json.dumps( + { + "data": { + "categories": [2], + } + } + ), +) + +print(response.json()) ``` + ::: ::: response Example response ```json { - "id": 2, - "name": "Dolemon Sushi", - "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious", - "created_by": null, - "updated_by": null, - "created_at": "2020-08-04T10:21:30.219Z", - "updated_at": "2020-08-04T10:21:30.219Z", - "categories": [ - { - "id": 2, - "name": "Brunch", - "created_by": 1, - "updated_by": 1, - "created_at": "2020-08-04T10:24:26.901Z", - "updated_at": "2020-08-04T10:24:26.911Z" + "data": { + "id": 2, + "attributes": { + "name": "Dolemon Sushi", + "description": null, + "createdAt": "2022-07-31T15:43:44.039Z", + "updatedAt": "2022-07-31T15:46:29.903Z", + "publishedAt": "2022-07-31T15:43:44.038Z", + "categories": { + "data": [ + { + "id": 2, + "attributes": { + "name": "Brunch", + "createdAt": "2022-07-31T11:57:23.472Z", + "updatedAt": "2022-07-31T11:57:25.180Z", + "publishedAt": "2022-07-31T11:57:25.179Z" + } + } + ] + } } - ] + }, + "meta": {} } ``` + ::: :::: ### Example ```python -import requests import json +import requests class Restaurant: def __init__(self): - self.api_url = "http://localhost:1337" + self.api_url = "http://localhost:1337/api" def all(self): r = requests.get(self.api_url + "/restaurants") return r.json() def create(self, params): - r = requests.post(self.api_url + "/restaurants", - data=json.dumps({ - 'name': params["name"], - 'description': params["description"], - 'categories': params["categories"] - }), - headers={ - 'Content-Type': 'application/json' - }) + r = requests.post( + self.api_url + "/restaurants", + headers={"Content-Type": "application/json"}, + data=json.dumps( + { + "data": { + "name": params["name"], + "description": params["description"], + "categories": params["categories"], + } + } + ), + ) + return r.json() def update(self, id, params): - r = requests.put(self.api_url + "/restaurants/" + str(id), - data=json.dumps({ - 'categories': params["categories"] - }), - headers={ - 'Content-Type': 'application/json' - }) + r = requests.put( + self.api_url + "/restaurants/" + str(id), + headers={"Content-Type": "application/json"}, + params={"populate": "categories"}, + data=json.dumps({"data": {"categories": params["categories"]}}), + ) + return r.json() + restaurant = Restaurant() -print(restaurant.update(2, { - 'categories': [2] -})) +print(restaurant.update(2, {"categories": [2]})) ``` ## Conclusion From ae602d905a4a44d90b234d2697771aa2f4805fab Mon Sep 17 00:00:00 2001 From: Kai Welke Date: Sun, 31 Jul 2022 20:32:15 +0200 Subject: [PATCH 2/3] fix: category name in response --- .../developer-resources/content-api/integrations/python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md index 6c00f0beb8..2f882b9899 100644 --- a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md +++ b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md @@ -145,7 +145,7 @@ print(response.json()) { "id": 3, "attributes": { - "name": "Japanese Food", + "name": "japanese", "createdAt": "2022-07-31T11:57:35.040Z", "updatedAt": "2022-07-31T11:57:35.631Z", "publishedAt": "2022-07-31T11:57:35.629Z" From 05128cdac9593380f673872093f64114e78b2ff7 Mon Sep 17 00:00:00 2001 From: Kai Welke Date: Sun, 31 Jul 2022 20:35:01 +0200 Subject: [PATCH 3/3] fix: misunderstood api-call --- .../content-api/integrations/python.md | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md index 2f882b9899..f5482a800c 100644 --- a/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md +++ b/docs/developer-docs/latest/developer-resources/content-api/integrations/python.md @@ -36,10 +36,7 @@ Be sure that you activated the `find` permission for the `restaurant` collection ::: request Example GET request ```python -import requests - -response = requests.get("http://localhost:1337/api/restaurants") -print(response.json()) +requests.get("http://localhost:1337/api/restaurants") ``` ::: @@ -105,10 +102,7 @@ In this example a `japanese` category has been created which has the id: 3. ::: request Example POST request ```python -import json -import requests - -response = requests.post( +requests.post( "http://localhost:1337/api/restaurants", headers={"Content-Type": "application/json"}, params={"populate": "categories"}, @@ -122,8 +116,6 @@ response = requests.post( } ), ) - -print(response.json()) ``` ::: @@ -215,10 +207,7 @@ Be sure that you activated the `put` permission for the `restaurant` collection ::: request Example PUT request ```python -import json -import requests - -response = requests.put( +requests.put( "http://localhost:1337/api/restaurants/2", params={"populate": "categories"}, headers={"Content-Type": "application/json"}, @@ -230,8 +219,6 @@ response = requests.put( } ), ) - -print(response.json()) ``` :::