Create python API from EDEKA?

Hey people,

I'm currently programming in Python, but I have the following problem.

I want to create a shopping list with current prices, but I need an API for that. I've never worked with an API before, but I definitely want to incorporate it. Can someone help me with how to connect an API to Python?

greeting

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
3 months ago

(Web)APIs basically work to send an HTTP request to a specific endpoint specified by the API. Depending on the requirement, it is necessary to provide further data. The HTTP repository contains the expected data in enquiries.

Basically, in the first step, you should deal with the documentation of the API you choose. It gives you the necessary information for the first connection setup, which endpoints are available, which format is used for data exchange (JSON/XML/…), etc.

If there are not already client libraries for the respective API (which you can often find out about the documentation), you can easily access the API’s requests requests– Use library.

As a result, I can show two applications using a sophisticated web API. These provide two endpoints to create new people and search for an existing person. For simplicity, no authentication is necessary. The Request methods specify what action should be done.

Base-URL: https://person-database.org

[POST] /api/persons/person
Request Data (JSON): { name: string, age: number, id: string }
Response Status: 200 / 401 / 403 / 500

[GET] /api/persons/person/{id}
Response Status: 200 / 401 / 403 / 404
Response Data (JSON): { name: string, age: number, id: string } / {}

Python script:

import requests

base_url = 'https://person-database.org'
person_id = '123'

# create a person
create_person_response = requests.post(base_url + '/api/persons/person', json={ "name": "John Doe", age: 37, id: person_id })
print(create_person_response.status_code) # 200 on success

# search for a person
get_person_response = requests.post(base_url + '/api/persons/person/' + person_id)

if get_person_response.status_code == requests.codes.ok:
  person = get_person_response.json()
  print('Name:', person['name'], 'Age:', person['age']) 
else:
  print('Person with id', person_id, 'could not be found.')

A POST request is required to create a person. The data is sent to the API in JSON format within the Request Body. If the action was successful, it delivers the status code 200 (Ok).

Via GET-Request is again looking for a person. This time, the data is sent to the request URL (in the request method GET, this is always the way to send data). If the request was successful, the Response Body contains the expected data in JSON format.