-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_api.py
More file actions
25 lines (20 loc) · 1.08 KB
/
call_api.py
File metadata and controls
25 lines (20 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import requests
import json
from faker import Faker
from main import Customer
fake = Faker(locale='NL')
customer_columns = Customer.__table__.columns.keys()
# Testing GET Request
requests.get('http://127.0.0.1:5000/customers').json() # Getting list of all customers
requests.get('http://127.0.0.1:5000/customers/1').json() # Getting customer with id: 1
# Testing DELETE Request
requests.delete('http://127.0.0.1:5000/customers/20') # Deleting customer with id: 20
# Testing POST Request
fake_data = {**fake.profile(), 'phone_number': fake.phone_number()}
fake_data['birthdate'] = str(fake_data['birthdate']) # Convert datetime to str to serialize birthdate
post_data = {col: fake_data[col] for col in customer_columns if col != 'id'}
requests.post('http://127.0.0.1:5000/customers', data=json.dumps(post_data)) # Insert new customer record
# Testing PUT/PATCH Request
put_data = {'job': 'Software Engineer'}
requests.put('http://127.0.0.1:5000/customers/1', data=json.dumps(put_data)) # Update customer record with id 1
requests.patch('http://127.0.0.1:5000/customers/1', data=json.dumps(put_data))