mexzziv / pet-api
类似于 OpenAPI-Specification 的 API
dev-main
2021-04-30 19:28 UTC
Requires
- php: ^7.2.5|^8.0
- fideloper/proxy: ^4.4
- fruitcake/laravel-cors: ^2.0
- guzzlehttp/guzzle: ^6.3.1|^7.0.1
- laravel/framework: ^7.29
- laravel/tinker: ^2.5
Requires (Dev)
- facade/ignition: ^2.0
- fakerphp/faker: ^1.9.1
- mockery/mockery: ^1.3.1
- nunomaduro/collision: ^4.3
- phpunit/phpunit: ^8.5.8|^9.3.3
This package is auto-updated.
Last update: 2024-09-29 06:08:37 UTC
README
Proyecto
该 API 使用 Laravel 7x 版本开发,数据库使用 MySQL。
安装时,可以从 https://github.com/mexzziv/pet 克隆仓库,或者使用以下命令:
composer required mexzziv/pet-api
输入完成后,将 .env.example 文件重命名为 .env,以便项目检测环境变量。
将 .env 文件重命名后,执行以下命令以生成加密密钥:
php artisan key:generate
初始化项目
为了集成数据库,请在以下行中修改 .env 文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
使用以下命令在数据库中创建表格:
php artisan migrate
执行以下命令填充数据库:
php artisan db:seed --class=PetSeeder
如果需要修改要填充的数据,请进入文件 database/seeds/PetSeeder.php
class PetSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { ... } }
API 管理
存在 3 个路由,分别是:
v1/pets/
v1/pets/create
v1/pets/{id}
GET -> v1/pets/
该路由返回数据库中的记录,限制为 100 条记录
响应视图
[
{
"id": 1,
"name": "kFIu4",
"tag": "dF5Vf"
},
{
"id": 2,
"name": "aPY3I",
"tag": "fEWAH"
},
{
"id": 3,
"name": "36bwx",
"tag": "Mo9dC"
},
{
"id": 4,
"name": "vEXzK",
"tag": "xHpca"
},
{
"id": 5,
"name": "BSyBl",
"tag": "eSodR"
},
{
"id": 6,
"name": "t6JWu",
"tag": "iLFFh"
},
{
"id": 7,
"name": "Rl2Wp",
"tag": "4ClMK"
},
{
"id": 8,
"name": "at8JK",
"tag": "UJUVW"
},
{
"id": 9,
"name": "DLQmn",
"tag": "z4NO5"
},
{
"id": 10,
"name": "SlXvq",
"tag": "RzvBW"
},
{
"id": 11,
"name": "ytnVK",
"tag": "cww9Y"
},
{
"id": 12,
"name": "Firus",
"tag": "Perro"
},
{
"id": 13,
"name": "Don Gato",
"tag": "Gato"
},
{
"id": 14,
"name": "Cerebro",
"tag": "Raton"
},
{
"id": 15,
"name": "Piolin",
"tag": "Canario"
},
{
"id": 16,
"name": "Dino",
"tag": "Dinosuario"
}
]
POST -> v1/pets/
为了在数据库中注册记录,该端点需要在 POST 请求中包含参数 name,如果注册成功,将返回以下结果:
{
"code": 200,
"status": "Create a pet",
"response": {
"name": "Mono",
"tag": "pets",
"updated_at": "2021-04-30T18:49:17.000000Z",
"created_at": "2021-04-30T18:49:17.000000Z",
"id": 17
}
}
如果请求中缺少参数 name,将返回以下消息:
{
"code": 2,
"status": "Validated fail",
"response": {
"name": ["The name field is required."]
}
}
如果在过程中发生错误,将发送以下消息:
{
"code": 3,
"status": "error",
"response": null
}
GET -> v1/pets/{id}
用于搜索具有特定 id 的记录的端点只需要作为参数的 id,如果找到匹配项,将返回以下消息:
{
"code": 200,
"status": "Pet find by id: 3",
"response": {
"id": 3,
"name": "36bwx",
"tag": "Mo9dC",
"created_at": null,
"updated_at": null
}
}
如果搜索没有结果,将返回以下消息:
{
"code": 404,
"status": "Pet not found",
"response": null
}
Postman
为了测试端点,可以使用以下代码:
{
"info": {
"_postman_id": "e410fa68-8a50-4482-be07-a0116e40086f",
"name": "Pets API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "http://tu_url_va_aqui/api/v1/pets/{id}",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://tu_url_va_aqui/api/v1/pets/{id}",
"protocol": "http",
"host": [
"develop",
"aktivamas",
"com"
],
"path": [
"api",
"v1",
"pets",
"3"
]
},
"description": "Get pet by ID"
},
"response": []
},
{
"name": "http://tu_url_va_aqui/api/v1/pets",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://tu_url_va_aqui/api/v1/pets",
"protocol": "http",
"host": [
"develop",
"aktivamas",
"com"
],
"path": [
"api",
"v1",
"pets"
]
},
"description": "Get all pets"
},
"response": []
},
{
"name": "http://tu_url_va_aqui/api/v1/pets/create?name=Mono",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://tu_url_va_aqui/api/v1/pets/create?name=Mono",
"protocol": "http",
"host": [
"develop",
"aktivamas",
"com"
],
"path": [
"api",
"v1",
"pets",
"create"
],
"query": [
{
"key": "name",
"value": "Mono"
}
]
},
"description": "Create pet in a API"
},
"response": []
}
]
}