-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
89 lines (66 loc) · 2.16 KB
/
API.py
File metadata and controls
89 lines (66 loc) · 2.16 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import requests
import json
import pprint
#GET method
def get_data_from_api():
URL = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(URL)
if response.status_code == 200:
response.json()
pprint.pprint(response.content)
else:
print("error")
#POST
def post_data():
new_data = {
"name": "andres",
"age": 23,
"position": "DevOps"
}
url = "https://jsonplaceholder.typicode.com/posts/1"
headers = {'Content-Type':'application/json; charset=UTF-8'}
response = requests.post(url, data=json.dumps(new_data), headers=headers)
response.json()
data = json.loads(response.text)
print(data)
#PUT
def put_data():
new_user = {
"userId": 342,
"id": 1048224831,
"title": "hacking DRM",
"body": "once upon in a time that I started with django"
}
url = "https://jsonplaceholder.typicode.com/posts/1"
headers = {'Content-Type':'application/json; charset=UTF-8'}
response = requests.put(url, data=json.dumps(new_user), headers=headers)
print(response.text)
#DELETE
def delete_item_from_server():
url = "https://jsonplaceholder.typicode.com/posts"
headers = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.delete(url, headers=headers)
print(response.text)
def api_weather() -> str:
API_KEY = "8d93ed2b737a6f91c8dfa80d73fa2c3f"
city = f"Bogota, CO"
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}'
city_weather = requests.get(url.format(city))
city_weather.json()
clima = json.loads(city_weather.text)
data = {
"city": clima["name"],
"country": clima["sys"]["country"],
"desc": clima["weather"][0]["description"],
"temp": clima["main"]["temp"],
"icon": clima["weather"][0]["icon"],
"feels": clima["main"]["feels_like"]
}
print("Ciudad: ", data["city"])
print("pais: ",data["country"] )
print("descripcion: ", data["desc"] )
print("temperatura: ",data["temp"] )
print("imagen: ", data["icon"] )
print("se siente: ", data["feels"] )
print()
api_weather()