#tool OpenAPIのスキーマ
openapi: 3.0.0
info:
title: Warehouse Address API
version: v1
servers:
- url: 'https://my-warehouse-api-zycua53bta-uc.a.run.app'
paths:
/warehouses/{warehouseId}/address:
get:
summary: Get Warehouse Address
description: Retrieves the address of a warehouse by its ID.
parameters:
- in: path
name: warehouseId
schema:
type: integer
format: int64
required: true
description: The ID of the warehouse.
responses:
'200':
description: Successful response with the warehouse address.
content:
application/json:
schema:
type: string
description: The full address of the warehouse.
'404':
description: Warehouse not found.
#コード
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/warehouses/<int:warehouseId>/address', methods=['GET'])
def get_warehouse_address(warehouseId):
"""
Retrieves the address of a warehouse by its ID.
:param warehouseId: The ID of the warehouse.
:type warehouseId: int
:return: The full address of the warehouse.
:rtype: str
"""
warehouse_data = {
1: "123 Main Street, Anytown, CA 12345",
2: "456 Oak Avenue, Springfield, IL 67890",
3: "789 Pine Lane, Sunnyville, TX 54321",
4: "1011 Maple Drive, Willow Creek, NY 98765",
5: "1213 Cedar Road, Evergreen, CO 01234",
6: "1415 Birch Street, Riverton, FL 76543",
7: "1617 Elm Avenue, Mountain View, WA 23456",
8: "1819 Oakwood Way, Lake City, OR 87654",
9: "2021 Willow Creek Lane, Forest Hills, PA 12345",
10: "2223 Pinewood Drive, Sunnyside, CA 98765"
}
if warehouseId in warehouse_data:
return jsonify(warehouse_data[warehouseId])
else:
return jsonify({'error': 'Warehouse not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
修正後コード
#tool OpenAPIのスキーマ
openapi: 3.0.0
info:
title: Warehouse Address API
version: v1
servers:
- url: 'https://url.us-central1.run.app'
paths:
/warehouses/address:
get:
summary: Get Warehouse Address
description: Retrieves the address of a warehouse by its ID.
parameters:
- in: query
name: warehouseId
schema:
type: integer
format: int64
required: true
description: The ID of the warehouse.
responses:
'200':
description: Successful response with the warehouse address.
content:
application/json:
schema:
type: object
properties:
address:
type: string
description: The full address of the warehouse.
'404':
description: Warehouse not found.
#コード
from fastapi import FastAPI, HTTPException
import functions_framework
app = FastAPI()
warehouse_data = {
1: "123 Main Street, Anytown, CA 12345",
2: "456 Oak Avenue, Springfield, IL 67890",
3: "789 Pine Lane, Sunnyville, TX 54321",
4: "1011 Maple Drive, Willow Creek, NY 98765",
5: "1213 Cedar Road, Evergreen, CO 01234",
6: "1415 Birch Street, Riverton, FL 76543",
7: "1617 Elm Avenue, Mountain View, WA 23456",
8: "1819 Oakwood Way, Lake City, OR 87654",
9: "2021 Willow Creek Lane, Forest Hills, PA 12345",
10: "2223 Pinewood Drive, Sunnyside, CA 98765"
}
@functions_framework.http
def get_warehouse_address(request):
warehouseId = request.args.get("warehouseId")
if warehouseId is not None:
try:
warehouseId = int(warehouseId)
if warehouseId in warehouse_data:
return {"address": warehouse_data[warehouseId]}
else:
raise HTTPException(status_code=404, detail="倉庫が見つかりません")
except ValueError:
raise HTTPException(status_code=400, detail="warehouseId must be an integer")
else:
raise HTTPException(status_code=400, detail="warehouseId is required")