承接 usox/json-schema-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

usox/json-schema-api

Composer 安装命令:

composer require usox/json-schema-api

包简介

Json schema powered api creation for php

README 文档

README

Unittests Scrutinizer Code Quality Code Coverage

JsonSchemaApi

This library provides a simple way to create a json api using json-schema to validate the request. You can leverage most of the input validation tasks (variables types, length/content-constraints, lists containing just certain items, etc.) to the json schema validator and work with the data right away.

Json-Schema

Every method needs a corresponding schema-file which describes, how the request data should look alike. You can find a simple example in the example/schema-folder.

Every request has also to follow a basic schema (see dist/request.json) which contains informations about the method.

Validation

Every request and the generated response will be validated against the provided schema.

Requirements

This library requires existing psr7 and psr17 implementations.

Install

composer require usox/json-schema-api

Usage

Just use the factory method to create the endpoint. The factory automatically searches for a existing psr17 stream factory implementation, but you can also provide a factory instance when calling the method.

Get your method-provider ready (see below) and call the factory-method on the endpoint to retrieve a working instance:

The serve method requires a psr request and returns a psr response.

$endpoint = \Usox\JsonSchemaApi\Endpoint::factory(
    $methodProvider
);

$endpoint->serve(
    $psr7Request,
    $psr7Response
);

Optional: PSR15 Middleware

The endpoint class implements the PSR15 RequestHandlerInterface.

MethodProvider

The MethodProvider is the source for your api methods - this could be a simple array, a DI-Container, etc. The class has to implement Usox\JsonSchemApi\Contract\MethodProviderInterface.

class MyMethodProvider implements \Usox\JsonSchemaApi\Contract\MethodProviderInterface
{
    private array $methodList = ['beerlist' => BeerlistMethod::class];

    public function lookup(string $methodName) : ?\Usox\JsonSchemaApi\Contract\ApiMethodInterface 
    {
        $handler = $this->methodList[$methodName] ?? null;
        if ($handler === null) {
            return null;
        }
        return new $handler;
    }
}

API-Method

The lookup-method in the MethodProvider must return an instance of Usox\JsonSchemaApi\Contract\ApiMethodInterface.

Every api method handler must define at least those two methods:

  • The handle method which processes the request and returns the result
  • The getSchemaFile method which returns the path to the schema file which is used to validate the request
class BeerlistMethod implements \Usox\JsonSchemaApi\Contract\ApiMethodInterface
{
    public function handle(stdClass $parameter) : array
    {
        return ['ipa', 'lager', 'weizen'];
    }
    
    public function getSchemaFile() : string
    {
        return '/path/to/schema.json';
    }
}

Example

You can find a working example in the example-folder.

Just cd to the example-folder and fire up the the php internal webserver php -S localhost:8888. Now you can send POST-Requests to the api like this using curl.

curl -X POST -d '{"method": "beerlist", "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888"

Error-Handling

Basically there are three types of errors. All of them get logged.

ApiExceptions

If a handler throws an exception which extends the ApiException exception class, the api will return a Bad Request (400) response including an error message (the exception message) and an error code for reference within a json response.

InternalException

Internal errors, like non-existing schema files, invalid schemas and such, will return a Internal Server Error (500) response and create a log entry (if a logger is provided).

In addition, optionally available context information within the exception will be logged, too.

Throwable

Any Throwable which are thrown within an api handler, will be catched, logged and return a Internal Server Error (500) response.

usox/json-schema-api 适用场景与选型建议

usox/json-schema-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.31k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 usox/json-schema-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 usox/json-schema-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 usox/json-schema-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-27