api-response-mocker
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAPI Response Mocker
API 响应模拟工具
Generate realistic mock API responses with fake data using Faker.
使用Faker生成带有虚假数据的逼真模拟API响应。
Features
功能特性
- Schema-Based Generation: Define response structure
- Faker Integration: Realistic fake data
- Nested Objects: Complex nested structures
- Arrays: Generate lists of objects
- Relationships: Reference other mock data
- Multiple Formats: JSON, XML output
- 基于Schema的生成:定义响应结构
- 集成Faker:生成逼真的虚假数据
- 嵌套对象:支持复杂的嵌套结构
- 数组生成:生成对象列表
- 数据关联:引用其他模拟数据
- 多格式输出:JSON、XML格式
Quick Start
快速开始
python
from api_mocker import APIMocker
mocker = APIMocker()python
from api_mocker import APIMocker
mocker = APIMocker()Generate user response
Generate user response
user = mocker.generate({
"id": "uuid",
"name": "name",
"email": "email",
"created_at": "datetime"
})
user = mocker.generate({
"id": "uuid",
"name": "name",
"email": "email",
"created_at": "datetime"
})
Generate list of users
Generate list of users
users = mocker.generate_list({
"id": "uuid",
"name": "name",
"email": "email"
}, count=10)
undefinedusers = mocker.generate_list({
"id": "uuid",
"name": "name",
"email": "email"
}, count=10)
undefinedCLI Usage
CLI 使用方法
bash
undefinedbash
undefinedGenerate from schema file
Generate from schema file
python api_mocker.py --schema user_schema.json --output user.json
python api_mocker.py --schema user_schema.json --output user.json
Generate list
Generate list
python api_mocker.py --schema product.json --count 50 --output products.json
python api_mocker.py --schema product.json --count 50 --output products.json
Generate with seed (reproducible)
Generate with seed (reproducible)
python api_mocker.py --schema order.json --seed 42 --output order.json
python api_mocker.py --schema order.json --seed 42 --output order.json
Preview without saving
Preview without saving
python api_mocker.py --schema customer.json --preview
undefinedpython api_mocker.py --schema customer.json --preview
undefinedSchema Format
Schema 格式
Define fields using Faker provider names:
json
{
"id": "uuid",
"first_name": "first_name",
"last_name": "last_name",
"email": "email",
"phone": "phone_number",
"company": "company",
"address": {
"street": "street_address",
"city": "city",
"state": "state",
"zip": "zipcode",
"country": "country"
},
"created_at": "date_time_this_year",
"is_active": "boolean"
}使用Faker提供者名称定义字段:
json
{
"id": "uuid",
"first_name": "first_name",
"last_name": "last_name",
"email": "email",
"phone": "phone_number",
"company": "company",
"address": {
"street": "street_address",
"city": "city",
"state": "state",
"zip": "zipcode",
"country": "country"
},
"created_at": "date_time_this_year",
"is_active": "boolean"
}Available Data Types
可用数据类型
Personal
个人信息
- ,
name,first_namelast_name - ,
emailsafe_email phone_numberssn
- ,
name,first_namelast_name - ,
emailsafe_email phone_numberssn
Address
地址信息
- ,
addressstreet_address - ,
city,statestate_abbr - ,
zipcodepostcode - ,
countrycountry_code - ,
latitudelongitude
- ,
addressstreet_address - ,
city,statestate_abbr - ,
zipcodepostcode - ,
countrycountry_code - ,
latitudelongitude
Internet
互联网相关
- ,
urldomain_name - ,
ipv4ipv6 - ,
user_namepassword - ,
uuiduuid4 mac_address
- ,
urldomain_name - ,
ipv4ipv6 - ,
user_namepassword - ,
uuiduuid4 mac_address
Business
商业信息
- ,
companycompany_suffix - ,
jobjob_title - ,
bscatch_phrase
- ,
companycompany_suffix - ,
jobjob_title - ,
bscatch_phrase
Financial
金融相关
credit_card_number- ,
ibanbban currency_code- (custom: returns float)
price
credit_card_number- ,
ibanbban currency_code- (自定义:返回浮点数)
price
Date/Time
日期/时间
- ,
datetime - ,
date_timedate_time_this_year date_of_birthiso8601
- ,
datetime - ,
date_timedate_time_this_year date_of_birthiso8601
Text
文本内容
- ,
text,sentenceparagraph - ,
wordwords slug
- ,
text,sentenceparagraph - ,
wordwords slug
Numeric
数值类型
- ,
random_intrandom_number - (use
random_float){"type": "float", "min": 0, "max": 100} boolean
- ,
random_intrandom_number - (使用
random_float){"type": "float", "min": 0, "max": 100} boolean
Advanced Schemas
高级Schema示例
Arrays
数组
json
{
"id": "uuid",
"name": "name",
"tags": {
"_array": true,
"_count": 3,
"_item": "word"
},
"orders": {
"_array": true,
"_count": 5,
"_item": {
"order_id": "uuid",
"amount": "random_int",
"date": "date"
}
}
}json
{
"id": "uuid",
"name": "name",
"tags": {
"_array": true,
"_count": 3,
"_item": "word"
},
"orders": {
"_array": true,
"_count": 5,
"_item": {
"order_id": "uuid",
"amount": "random_int",
"date": "date"
}
}
}Custom Values
自定义值
json
{
"id": "uuid",
"status": {
"_choice": ["pending", "active", "completed"]
},
"priority": {
"_range": [1, 5]
},
"score": {
"_float": {"min": 0.0, "max": 100.0, "decimals": 2}
}
}json
{
"id": "uuid",
"status": {
"_choice": ["pending", "active", "completed"]
},
"priority": {
"_range": [1, 5]
},
"score": {
"_float": {"min": 0.0, "max": 100.0, "decimals": 2}
}
}Nested Objects
嵌套对象
json
{
"user": {
"id": "uuid",
"profile": {
"bio": "paragraph",
"avatar_url": "image_url",
"social": {
"twitter": "user_name",
"linkedin": "url"
}
}
}
}json
{
"user": {
"id": "uuid",
"profile": {
"bio": "paragraph",
"avatar_url": "image_url",
"social": {
"twitter": "user_name",
"linkedin": "url"
}
}
}
}API Reference
API 参考文档
APIMocker Class
APIMocker 类
python
class APIMocker:
def __init__(self, locale: str = "en_US", seed: int = None)
# Generation
def generate(self, schema: dict) -> dict
def generate_list(self, schema: dict, count: int = 10) -> list
# File operations
def from_schema_file(self, filepath: str) -> dict
def save(self, data: any, filepath: str, format: str = "json")
# Utilities
def set_seed(self, seed: int)
def get_faker(self) -> Fakerpython
class APIMocker:
def __init__(self, locale: str = "en_US", seed: int = None)
# Generation
def generate(self, schema: dict) -> dict
def generate_list(self, schema: dict, count: int = 10) -> list
# File operations
def from_schema_file(self, filepath: str) -> dict
def save(self, data: any, filepath: str, format: str = "json")
# Utilities
def set_seed(self, seed: int)
def get_faker(self) -> FakerExample Schemas
示例Schema
User Response
用户响应
json
{
"id": "uuid",
"username": "user_name",
"email": "email",
"profile": {
"first_name": "first_name",
"last_name": "last_name",
"avatar": "image_url",
"bio": "sentence"
},
"created_at": "iso8601",
"last_login": "date_time_this_month"
}json
{
"id": "uuid",
"username": "user_name",
"email": "email",
"profile": {
"first_name": "first_name",
"last_name": "last_name",
"avatar": "image_url",
"bio": "sentence"
},
"created_at": "iso8601",
"last_login": "date_time_this_month"
}E-commerce Product
电商产品
json
{
"sku": "uuid",
"name": "catch_phrase",
"description": "paragraph",
"price": {"_float": {"min": 9.99, "max": 999.99}},
"currency": "currency_code",
"category": {"_choice": ["Electronics", "Clothing", "Home", "Sports"]},
"in_stock": "boolean",
"rating": {"_float": {"min": 1, "max": 5, "decimals": 1}},
"reviews_count": {"_range": [0, 500]}
}json
{
"sku": "uuid",
"name": "catch_phrase",
"description": "paragraph",
"price": {"_float": {"min": 9.99, "max": 999.99}},
"currency": "currency_code",
"category": {"_choice": ["Electronics", "Clothing", "Home", "Sports"]},
"in_stock": "boolean",
"rating": {"_float": {"min": 1, "max": 5, "decimals": 1}},
"reviews_count": {"_range": [0, 500]}
}API Error Response
API 错误响应
json
{
"error": {
"code": {"_choice": ["NOT_FOUND", "UNAUTHORIZED", "BAD_REQUEST"]},
"message": "sentence",
"request_id": "uuid",
"timestamp": "iso8601"
}
}json
{
"error": {
"code": {"_choice": ["NOT_FOUND", "UNAUTHORIZED", "BAD_REQUEST"]},
"message": "sentence",
"request_id": "uuid",
"timestamp": "iso8601"
}
}Dependencies
依赖项
- faker>=22.0.0
- faker>=22.0.0