Skip to content

HappyX for FastAPI Programmers

Ethosa edited this page Jul 22, 2023 · 1 revision

HTTP Methods Matching

FastAPI

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
  return "Hello, world!"

HappyX

import happyx

serve "127.0.0.1", 5000:
  get "/":
    return "Hello, world!"

Path Params

FastAPI

@app.get("/user/{id}")
def get_user_by_id(id: int):
  return {"response": id}

HappyX

get "/user/{id:int}":
  return {"response": id}

Request Models

FastAPI

In FastAPI you can use pydantic to create models

from pydantic import BaseModel
from fastapi import FastAPI


class User(BaseModel):
  username: str
  age: int


app = FastAPI()


@app.post("/")
def create_user(user: User):
  # create user
  return {"response": {
    "id": 0,
    "username": user.username
  }}

HappyX

In HappyX you can use built-in request models that supports JSON, XML, form-data and x-www-form-urlencoded body

import happyx

model User:
  username: string
  age: int

serve "127.0.0.1", 5000:
  post "/[user:User]":
    # create user
    return {"response": {
      "id": 0,
      "username": user.username
    }}