Skip to content

Commit

Permalink
Merge pull request #13 from liblaber/python_v2
Browse files Browse the repository at this point in the history
Python v2
  • Loading branch information
Jim Bennett authored Apr 11, 2024
2 parents bc3fa8c + 15a7bc8 commit f0ca810
Show file tree
Hide file tree
Showing 28 changed files with 349 additions and 317 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ node_modules
sdk-examples/python/pics
sdk-examples/rest/pics
llama-store.sln
.mono/
11 changes: 6 additions & 5 deletions liblab.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@
"bundle": true,
"exportClassDefault": true,
"githubRepoName": "llama-store-sdk-typescript",
"sdkVersion": "0.0.3"
"sdkVersion": "0.0.4"
},
"python": {
"pypiPackageName": "LlamaStore",
"githubRepoName": "llama-store-sdk-python",
"sdkVersion": "0.0.3"
"sdkVersion": "0.0.4",
"liblabVersion": "2"
},
"java": {
"groupId": "com.liblab",
"githubRepoName": "llama-store-sdk-java",
"sdkVersion": "0.0.3"
"sdkVersion": "0.0.4"
},
"go": {
"goModuleName": "github.com/liblaber/llama-store-sdk-go",
"githubRepoName": "llama-store-sdk-go",
"sdkVersion": "0.0.3"
"sdkVersion": "0.0.4"
},
"csharp": {
"githubRepoName": "llama-store-sdk-csharp",
"sdkVersion": "0.0.3"
"sdkVersion": "0.0.4"
}
},
"publishing": {
Expand Down
8 changes: 4 additions & 4 deletions llama_store/data/llama_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_db_llama_by_id(db: Session, llama_id: int) -> DBLlama:
:return: The llama with the given ID.
:rtype: DBLlama
"""
return db.query(DBLlama).filter(DBLlama.id == llama_id).first()
return db.query(DBLlama).filter(DBLlama.llama_id == llama_id).first()


def get_llama_by_id(db: Session, llama_id: int) -> Llama:
Expand All @@ -30,20 +30,20 @@ def get_llama_by_id(db: Session, llama_id: int) -> Llama:
:param Session db: The database session.
:param int llama_id: The ID of the llama to get.
:return: The llama with the given ID.
:rtype: LLama
:rtype: Llama
"""
db_llama = get_db_llama_by_id(db, llama_id)
return None if db_llama is None else Llama.model_validate(db_llama)


def get_llama_by_name(db: Session, llama_name: str) -> Llama:
"""
Get a llama by name. Llama names muct be unique
Get a llama by name. Llama names must be unique
:param Session db: The database session.
:param int llama_name: The name of the llama to get.
:return: The llama with the given name.
:rtype: LLama
:rtype: Llama
"""
db_llama = db.query(DBLlama).filter(DBLlama.name == llama_name).first()
return None if db_llama is None else Llama.model_validate(db_llama)
Expand Down
4 changes: 2 additions & 2 deletions llama_store/data/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DBLlama(Base):

__tablename__ = "llamas"

id = Column(Integer, primary_key=True, index=True)
llama_id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True, index=True)
age = Column(Integer, index=False, nullable=False)
color = Column(String, index=False, nullable=False)
Expand All @@ -53,6 +53,6 @@ class DBLlamaPicture(Base):

__tablename__ = "llama_picture_locations"

id = Column(Integer, primary_key=True, index=True)
llama_picture_id = Column(Integer, primary_key=True, index=True)
llama_id = Column(Integer, index=True)
image_file_location = Column(String, index=False, nullable=False)
18 changes: 9 additions & 9 deletions llama_store/db_migrations/versions/first.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@

ALL_LLAMAS = [
{
"id": 1,
"llama_id": 1,
"name": "Libby the Llama",
"age": 3,
"color": "white",
"rating": 5,
},
{
"id": 2,
"llama_id": 2,
"name": "Labby the Llama",
"age": 5,
"color": "gray",
"rating": 4,
},
{
"id": 3,
"llama_id": 3,
"name": "Sean the fake Llama",
"age": 18,
"color": "white",
"rating": 1,
},
{
"id": 4,
"llama_id": 4,
"name": "Logo the Llama logo",
"age": 2,
"color": "black",
"rating": 5,
},
{
"id": 5,
"llama_id": 5,
"name": "Barack O'Llama",
"age": 4,
"color": "black",
"rating": 5,
},
{
"id": 6,
"llama_id": 6,
"name": "Llama Del Rey",
"age": 9,
"color": "white",
Expand Down Expand Up @@ -89,7 +89,7 @@ def upgrade() -> None:

llamas_table = op.create_table(
"llamas",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("llama_id", sa.Integer, primary_key=True),
sa.Column("name", sa.String, nullable=False, unique=True, index=True),
sa.Column("age", sa.Integer, nullable=False),
sa.Column("color", sa.String, nullable=False),
Expand All @@ -98,7 +98,7 @@ def upgrade() -> None:

llama_pictures_table = op.create_table(
"llama_picture_locations",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("llama_picture_id", sa.Integer, primary_key=True),
sa.Column("llama_id", sa.Integer, nullable=False, unique=True, index=True),
sa.Column("image_file_location", sa.String, nullable=False),
)
Expand All @@ -110,7 +110,7 @@ def upgrade() -> None:

def get_llama_insert(llama_id: int) -> dict:
return {
"id": llama_id,
"llama_picture_id": llama_id,
"llama_id": llama_id,
"image_file_location": f"{root}/{llama_id}.png",
}
Expand Down
8 changes: 4 additions & 4 deletions llama_store/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@

tags_metadata = [
{
"name": "llama",
"name": "Llama",
"description": "Get the llamas",
},
{
"name": "llama-picture",
"name": "LlamaPicture",
"description": "Get the llama pictures",
},
{
"name": "user",
"name": "User",
"description": "Register users",
},
{
"name": "token",
"name": "Token",
"description": "Manage API Tokens",
},
]
Expand Down
36 changes: 20 additions & 16 deletions llama_store/models/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from enum import Enum

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field


class LlamaColor(str, Enum):
Expand All @@ -23,18 +23,20 @@ class LlamaId(BaseModel):
A llama's ID. This is used as a response model when creating llama pictures.
"""

id: int = Field(description="The ID of the llama.", examples=[1])
llama_id: int = Field(description="The ID of the llama.", examples=[1], alias="llamaId", title="Llama Id")

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"id": "1",
"llama_id": "1",
}
],
"description": "A llama id.",
},
}
from_attributes=True,
populate_by_name=True,
)


class LlamaBase(BaseModel):
Expand Down Expand Up @@ -62,8 +64,8 @@ class LlamaBase(BaseModel):
le=5,
)

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"name": "libby the llama",
Expand All @@ -74,8 +76,9 @@ class LlamaBase(BaseModel):
],
"description": "A new llama for the llama store.",
},
"from_attributes": True,
}
from_attributes=True,
populate_by_name=True,
)


class LlamaCreate(LlamaBase):
Expand All @@ -89,13 +92,13 @@ class Llama(LlamaBase):
A llama, with details of it's name, age, color, and rating from 1 to 5.
"""

id: int = Field(description="The ID of the llama.", examples=[1])
llama_id: int = Field(description="The ID of the llama.", examples=[1], alias="llamaId", title="Llama Id")

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"id": "1",
"llama_id": "1",
"name": "libby the llama",
"age": 5,
"color": "brown",
Expand All @@ -104,5 +107,6 @@ class Llama(LlamaBase):
],
"description": "A llama, with details of its name, age, color, and rating from 1 to 5.",
},
"from_attributes": True,
}
from_attributes=True,
populate_by_name=True,
)
2 changes: 1 addition & 1 deletion llama_store/models/llama_picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LlamaPicture(BaseModel):
A link to a file containing a picture of a llama.
"""

id: int
llama_picture_id: int
llama_id: int
image_file_location: str

Expand Down
28 changes: 17 additions & 11 deletions llama_store/models/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# pylint: disable=duplicate-code

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from models.user import UserRegistration


Expand All @@ -13,8 +13,8 @@ class APITokenRequest(UserRegistration):
A model to represent an API token request. The email and password must match an existing user.
"""

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"email": "[email protected]",
Expand All @@ -23,8 +23,8 @@ class APITokenRequest(UserRegistration):
],
"description": "A request to get an API token for a given user.",
},
"from_attributes": True,
}
from_attributes=True,
)


class APIToken(BaseModel):
Expand All @@ -36,22 +36,28 @@ class APIToken(BaseModel):
access_token: str = Field(
description="The bearer token to use with the API. Pass this in the Authorization header as a bearer token.",
examples=["Authorization: Bearer 1234567890abcdef"],
alias="accessToken",
title="Access Token",
)

token_type: str = Field(
default="bearer",
description="The type of token. This will always be bearer.",
examples=["bearer"],
alias="tokenType",
title="Token Type",
)

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"access_token": "1234567890abcdef",
"token_type": "bearer",
"accessToken": "1234567890abcdef",
"tokenType": "bearer",
}
],
"description": "An API token to use for authentication.",
},
"from_attributes": True,
}
from_attributes=True,
populate_by_name=True,
)
18 changes: 9 additions & 9 deletions llama_store/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import re
from typing import Annotated
from pydantic import BaseModel, StringConstraints, validator, Field
from pydantic import BaseModel, ConfigDict, StringConstraints, validator, Field

EMAIL_REGEX = r".+\@.+\..+"
PASSWORD_REGEX = re.compile(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$")
Expand Down Expand Up @@ -51,8 +51,8 @@ def passwords_match(cls, v, values, **kwargs): # pylint: disable=unused-argumen
)
return v

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"email": "[email protected]",
Expand All @@ -61,8 +61,8 @@ def passwords_match(cls, v, values, **kwargs): # pylint: disable=unused-argumen
],
"description": "A new user of the llama store.",
},
"from_attributes": True,
}
from_attributes=True,
)


class User(UserBase):
Expand All @@ -75,8 +75,8 @@ class User(UserBase):
examples=[1, 2, 3],
)

model_config = {
"json_schema_extra": {
model_config = ConfigDict(
json_schema_extra={
"examples": [
{
"id": "1",
Expand All @@ -85,5 +85,5 @@ class User(UserBase):
],
"description": "A user of the llama store",
},
"from_attributes": True,
}
from_attributes=True,
)
Loading

0 comments on commit f0ca810

Please sign in to comment.