โ (๐ / ๐ฆ ๐ฝ) ๐ฝ¶
FastAPI ๐ช ๐ ๏ธ โฎ๏ธ ๐ โ.
๐ฅ ๐ฅ ๐ ๐ ๐ผ โ๏ธ ๐, ๐ ๐งข โ ๐ฝ.
๐ ๐ช ๐ ๏ธ โซ๏ธ ๐ ๐ โ ๐ฝ ๐:
- โณ
- ๐ธ
- โณ
- ๐ธ๐ฒ
- โณ, โ๏ธ.
Tip
๐ค ๐ ๐ ๐ โฎ๏ธ FastAPI & ๐, ๐ โ๏ธ ๐ โ, ๐ ๐ธ & ๐ ๐งฐ: https://github.com/tiangolo/full-stack-fastapi-couchbase
๐ ๐ ๐ฆฒ¶
๐, ๐ซ ๐ธ ๐ ๐, ๐ด ๐:
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
๐ฌ ๐ โ๏ธ "๐ ๐"¶
๐ฅ ๐ โ๏ธ โซ๏ธ โช ๐ง ๐ type
๐ ๐.
๐ ๐ซ โ ๐, โ๏ธ ๐ ๐ก ๐ ๐ โน ๐ โฎ๏ธ.
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
๐ฎ ๐ข ๐ค Bucket
¶
๐, ๐ฅก โ ๐, ๐ ๐ช ๐ ๐.
๐ซ ๐ ๐ ๐ ๐ ๐ธ.
๐ ๐ ๐ฝ ๐ ๐ "๐ฝ" (๐ฏ ๐ฝ, ๐ซ ๐ฝ ๐ฝ).
๐ โณ ๐ "๐".
๐, Bucket
๐จ ๐ ๐จ๐ป ๐ป โฎ๏ธ ๐ฝ.
๐ ๐ ๐ข ๐:
- ๐ ๐ ๐ (๐ ๐ช ๐ ๐ฐ).
- โ ๐ข โฒ.
- ๐ ๐.
- ๐ค
Bucket
๐.- โ ๐ข โฒ.
- ๐จ โซ๏ธ.
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
โ Pydantic ๐ท¶
๐ "๐" ๐ค "๐ป ๐", ๐ฅ ๐ช ๐ท ๐ซ โฎ๏ธ Pydantic.
User
๐ท¶
๐ฅ, โก๏ธ โ User
๐ท:
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
๐ฅ ๐ โ๏ธ ๐ ๐ท ๐ โก ๐ ๏ธ ๐ข,, ๐ฅ ๐ซ ๐ โซ๏ธ hashed_password
.
UserInDB
๐ท¶
๐, โก๏ธ โ UserInDB
๐ท.
๐ ๐ โ๏ธ ๐ฝ ๐ ๐ค ๐ช ๐ฝ.
๐ฅ ๐ซ โ โซ๏ธ ๐ฟ Pydantic BaseModel
โ๏ธ ๐ฟ ๐ ๐ User
, โฉ๏ธ โซ๏ธ ๐ โ๏ธ ๐ ๐ข User
โ ๐ฉโโคโ๐จ ๐
:
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
Note
๐ ๐ ๐ฅ โ๏ธ hashed_password
& type
๐ ๐ ๐ ๐ช ๐ฝ.
โ๏ธ โซ๏ธ ๐ซ ๐ ๐ข User
๐ท (1๏ธโฃ ๐ฅ ๐ ๐จ โก ๐ ๏ธ).
๐ค ๐ฉโ๐ป¶
๐ โ ๐ข ๐ ๐:
- โ ๐.
- ๐ ๐ ๐ โช๏ธโก๏ธ โซ๏ธ.
- ๐ค ๐ โฎ๏ธ ๐ ๐.
- ๐ฎ ๐ ๐
UserInDB
๐ท.
๐ ๐ข ๐ ๐ด ๐ก ๐ค ๐ ๐ฉโ๐ป โช๏ธโก๏ธ username
(โ๏ธ ๐ ๐ ๐ข) ๐ฌ ๐ โก ๐ ๏ธ ๐ข, ๐ ๐ช ๐ ๐ช ๐ค-โ๏ธ โซ๏ธ ๐ ๐ & ๐ฎ โ ๐ฏ โซ๏ธ:
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
โ-๐ป¶
๐ฅ ๐ ๐ซ ๐ฐ โฎ๏ธ f"userprofile::{username}"
, โซ๏ธ ๐ "โ-๐ป".
๐ ๐ข ๐ ๐ฎ ๐ {}
โ-๐ป ๐ โ / ๐ ๐ป.
dict
๐¶
๐ฅ ๐ ๐ซ ๐ฐ โฎ๏ธ UserInDB(**result.value)
, โซ๏ธ โ๏ธ dict
"๐".
โซ๏ธ ๐ โ dict
result.value
, & โ ๐ ๐ฎ ๐ & ๐ฒ & ๐ถโโ๏ธ ๐ซ ๐-๐ฒ UserInDB
๐จ๐ป โ.
, ๐ฅ dict
๐:
{
"username": "johndoe",
"hashed_password": "some_hash",
}
โซ๏ธ ๐ ๐ถโโ๏ธ UserInDB
:
UserInDB(username="johndoe", hashed_password="some_hash")
โ ๐ FastAPI ๐¶
โ FastAPI
๐ฑ¶
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
โ โก ๐ ๏ธ ๐ข¶
๐ ๐ ๐ค ๐ & ๐ฅ ๐ซ โ๏ธ ๐ฅผ ๐ await
๐โ๐ฆบ, ๐ฅ ๐ ๐ฃ ๐ ๐ข โฎ๏ธ ๐ def
โฉ๏ธ async def
.
, ๐ ๐ ๐ซ โ๏ธ ๐ Bucket
๐ ๐ "๐งตโ",, ๐ฅ ๐ช ๐ค ๐ฅก ๐ & ๐ถโโ๏ธ โซ๏ธ ๐ ๐ ๐ข:
from typing import Union
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
from fastapi import FastAPI
from pydantic import BaseModel
USERPROFILE_DOC_TYPE = "userprofile"
def get_bucket():
cluster = Cluster(
"couchbase://couchbasehost:8091?fetch_mutation_tokens=1&operation_timeout=30&n1ql_timeout=300"
)
authenticator = PasswordAuthenticator("username", "password")
cluster.authenticate(authenticator)
bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
bucket.timeout = 30
bucket.n1ql_timeout = 300
return bucket
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
type: str = USERPROFILE_DOC_TYPE
hashed_password: str
def get_user(bucket: Bucket, username: str):
doc_id = f"userprofile::{username}"
result = bucket.get(doc_id, quiet=True)
if not result.value:
return None
user = UserInDB(**result.value)
return user
# FastAPI specific code
app = FastAPI()
@app.get("/users/{username}", response_model=User)
def read_user(username: str):
bucket = get_bucket()
user = get_user(bucket=bucket, username=username)
return user
๐¶
๐ ๐ช ๐ ๏ธ ๐ ๐ฅ ๐ฅณ โ ๐ฝ, โ๏ธ ๐ซ ๐ฉ ๐ฆ.
๐ โ ๐ ๐ ๐ข ๐งฐ, โ๏ธ โ๏ธ ๐ ๏ธ.