ch14
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
$ http http://localhost:8000/stations
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 702
|
||||
content-type: application/json
|
||||
date: Thu, 19 Aug 2021 22:11:10 GMT
|
||||
server: uvicorn
|
||||
|
||||
[
|
||||
{
|
||||
"city": "Rome",
|
||||
"code": "ROM",
|
||||
"country": "Italy",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"city": "Paris",
|
||||
"code": "PAR",
|
||||
"country": "France",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"city": "London",
|
||||
"code": "LDN",
|
||||
"country": "UK",
|
||||
"id": 2
|
||||
},
|
||||
{
|
||||
"city": "Kyiv",
|
||||
"code": "KYV",
|
||||
"country": "Ukraine",
|
||||
"id": 3
|
||||
},
|
||||
{
|
||||
"city": "Stockholm",
|
||||
"code": "STK",
|
||||
"country": "Sweden",
|
||||
"id": 4
|
||||
},
|
||||
{
|
||||
"city": "Warsaw",
|
||||
"code": "WSW",
|
||||
"country": "Poland",
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"city": "Moskow",
|
||||
"code": "MSK",
|
||||
"country": "Russia",
|
||||
"id": 6
|
||||
},
|
||||
{
|
||||
"city": "Amsterdam",
|
||||
"code": "AMD",
|
||||
"country": "Netherlands",
|
||||
"id": 7
|
||||
},
|
||||
{
|
||||
"city": "Edinburgh",
|
||||
"code": "EDB",
|
||||
"country": "Scotland",
|
||||
"id": 8
|
||||
},
|
||||
{
|
||||
"city": "Budapest",
|
||||
"code": "BDP",
|
||||
"country": "Hungary",
|
||||
"id": 9
|
||||
},
|
||||
{
|
||||
"city": "Bucharest",
|
||||
"code": "BCR",
|
||||
"country": "Romania",
|
||||
"id": 10
|
||||
},
|
||||
{
|
||||
"city": "Sofia",
|
||||
"code": "SFA",
|
||||
"country": "Bulgaria",
|
||||
"id": 11
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations?code=LDN
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 54
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 19:31:08 GMT
|
||||
server: uvicorn
|
||||
|
||||
[
|
||||
{
|
||||
"city": "London",
|
||||
"code": "LDN",
|
||||
"country": "UK",
|
||||
"id": 2
|
||||
}
|
||||
]
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations/3
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 55
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 19:38:36 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"city": "Kyiv",
|
||||
"code": "KYV",
|
||||
"country": "Ukraine",
|
||||
"id": 3
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations/kyiv
|
||||
HTTP/1.1 422 Unprocessable Entity
|
||||
content-length: 107
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 19:42:34 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": [
|
||||
"path",
|
||||
"station_id"
|
||||
],
|
||||
"msg": "value is not a valid integer",
|
||||
"type": "type_error.integer"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations/100
|
||||
HTTP/1.1 404 Not Found
|
||||
content-length: 35
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 19:48:09 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"detail": "Station 100 not found."
|
||||
}
|
||||
|
||||
|
||||
---
|
||||
|
||||
$ http POST http://localhost:8000/stations \
|
||||
code=TMP country=Temporary-Country city=tmp-city
|
||||
HTTP/1.1 201 Created
|
||||
content-length: 70
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 20:20:34 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"city": "tmp-city",
|
||||
"code": "TMP",
|
||||
"country": "Temporary-Country",
|
||||
"id": 12
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
$ http POST http://localhost:8000/stations \
|
||||
country=Another-Country city=another-city
|
||||
HTTP/1.1 422 Unprocessable Entity
|
||||
content-length: 88
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 20:23:21 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": [
|
||||
"body",
|
||||
"code"
|
||||
],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
$ http PUT http://localhost:8000/stations/12 \
|
||||
code=SMC country=Some-Country city=Some-city
|
||||
HTTP/1.1 204 No Content
|
||||
date: Fri, 20 Aug 2021 21:19:22 GMT
|
||||
server: uvicorn
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations/12
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 66
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 21:20:39 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"city": "Some-city",
|
||||
"code": "SMC",
|
||||
"country": "Some-Country",
|
||||
"id": 12
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
$ http PUT http://localhost:8000/stations/12 code=xxx
|
||||
HTTP/1.1 204 No Content
|
||||
date: Fri, 20 Aug 2021 21:23:09 GMT
|
||||
server: uvicorn
|
||||
|
||||
---
|
||||
|
||||
$ http http://localhost:8000/stations/12
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 66
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 21:23:47 GMT
|
||||
server: uvicorn
|
||||
|
||||
{
|
||||
"city": "Some-city",
|
||||
"code": "xxx",
|
||||
"country": "Some-Country",
|
||||
"id": 12
|
||||
}
|
||||
|
||||
|
||||
---
|
||||
|
||||
$ http DELETE http://localhost:8000/stations/12
|
||||
HTTP/1.1 204 No Content
|
||||
date: Fri, 20 Aug 2021 21:33:17 GMT
|
||||
server: uvicorn
|
||||
|
||||
---
|
||||
|
||||
$ http DELETE http://localhost:8000/stations/12
|
||||
HTTP/1.1 404 Not Found
|
||||
Transfer-Encoding: chunked
|
||||
date: Fri, 20 Aug 2021 21:33:43 GMT
|
||||
server: uvicorn
|
||||
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
$ http POST http://localhost:8000/users/authenticate \
|
||||
email="fabrizio.romano@example.com" password="f4bPassword"
|
||||
HTTP/1.1 200 OK
|
||||
content-length: 201
|
||||
content-type: application/json
|
||||
date: Fri, 20 Aug 2021 21:51:13 GMT
|
||||
server: uvicorn
|
||||
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2Mjk0OTYyNzQsImV4cCI6MTYyOTU4MjY3NCwiZW1haWwiOiJmYWJyaXppby5yb21hbm9AZXhhbXBsZS5jb20iLCJyb2xlIjoiYWRtaW4ifQ.q-8m_xh2LacyxtNGHzMg2cQhgyDpmyvCr75Qb_7snYI"
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
# samples/typing.examples.py
|
||||
|
||||
|
||||
###############################################
|
||||
# This module is not supposed to be executed! #
|
||||
###############################################
|
||||
|
||||
|
||||
# not strongly typed language
|
||||
a = 7
|
||||
b = "7"
|
||||
|
||||
a + b == 14
|
||||
concatenate(a, b) == "77"
|
||||
|
||||
|
||||
# dynamic typing
|
||||
a = 7
|
||||
a * 2 == 14
|
||||
a = "Hello"
|
||||
a * 2 == "HelloHello"
|
||||
|
||||
|
||||
# other languages
|
||||
String greeting = "Hello";
|
||||
int m = 7;
|
||||
float pi = 3.141592;
|
||||
|
||||
|
||||
# function annotations
|
||||
def greet(first_name, last_name, age):
|
||||
return f"Greeting {first_name} {last_name} of age {age}"
|
||||
|
||||
def greet(
|
||||
first_name: "First name of the person we are greeting",
|
||||
last_name: "Last name of the person we are greeting",
|
||||
age: "The person's age"
|
||||
) -> "Returns the greeting sentence":
|
||||
return f"Greeting {first_name} {last_name} of age {age}"
|
||||
|
||||
def greet(first_name: str, last_name: str, age: int = 18) -> str:
|
||||
return f"Greeting {first_name} {last_name} of age {age}"
|
||||
|
||||
|
||||
# list
|
||||
from typing import List
|
||||
|
||||
def process_words(words: List[str]):
|
||||
for word in words:
|
||||
# do something with word
|
||||
|
||||
|
||||
# dict
|
||||
from typing import Dict
|
||||
|
||||
def process_users(users: Dict[str, int]):
|
||||
for name, age in users.items():
|
||||
# do something with name and age
|
||||
|
||||
|
||||
# optional
|
||||
from typing import Optional
|
||||
|
||||
def greet_again(name: Optional[str] = None):
|
||||
if name is not None:
|
||||
print(f"Hello {name}!")
|
||||
else:
|
||||
print("Hey dude")
|
||||
|
||||
|
||||
# custom
|
||||
class Cat:
|
||||
def __init__(self, name: str):
|
||||
self.name = name
|
||||
|
||||
def call_cat(cat: Cat):
|
||||
return f"{cat.name}! Come here!"
|
||||
Reference in New Issue
Block a user