カスタムプラグインの開発基礎
最終更新
openapi: 3.0.0
info:
title: 顧客管理API
description: 顧客情報の検索・取得を行うAPI
version: 1.0.0
servers:
- url: https://api.company.com/v1
paths:
/customers/search:
get:
operationId: searchCustomers
summary: 顧客検索
description: 名前やメールアドレスの一部から顧客を検索します。
parameters:
- name: query
in: query
required: true
schema:
type: string
description: 検索キーワード(氏名またはメール)
- name: limit
in: query
schema:
type: integer
default: 10
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/CustomerList'
components:
schemas:
CustomerList:
type: object
properties:
customers:
type: array
items:
type: object
properties:
id: {type: string}
name: {type: string}
email: {type: string}// 推奨: 構造化されたJSON
{
"success": true,
"data": {
"results": [
{"name": "山田太郎", "status": "active"}
]
},
"message": "1件ヒットしました"
}// エラー時も構造化して返す
{
"success": false,
"error": {
"code": "NOT_FOUND",
"message": "指定された顧客が見つかりません"
}
}from fastapi import FastAPI, HTTPException, Header, Query
from pydantic import BaseModel
from typing import List, Optional
app = FastAPI()
# セキュリティ設定(環境変数等で管理することを推奨)
VALID_API_KEY = "sk-secret-key"
# データモデル定義
class Customer(BaseModel):
id: str
name: str
email: str
class SearchResponse(BaseModel):
total: int
customers: List[Customer]
# ダミーデータ
MOCK_DB = [
Customer(id="1", name="山田太郎", email="yamada@example.com"),
Customer(id="2", name="鈴木花子", email="suzuki@example.com"),
]
@app.get("/customers/search", response_model=SearchResponse)
async def search_customers(
query: str = Query(..., description="検索キーワード"),
limit: int = 10,
x_api_key: str = Header(..., alias="X-API-Key")
):
# 簡易認証
if x_api_key != VALID_API_KEY:
raise HTTPException(status_code=401, detail="Invalid API Key")
# 検索ロジック
results = [
c for c in MOCK_DB
if query in c.name or query in c.email
]
return SearchResponse(
total=len(results[:limit]),
customers=results[:limit]
)