fenix007/apiwrapper

Laravel API 包装器

v0.0.5 2017-07-14 20:13 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:05:29 UTC


README

A Laraval Api wrapper base package for fast build custom api wrapper

包包含抽象类,您需要扩展它们和一些辅助函数。组织您的包文件,使

project
│   Client extends Fenix007\Wrapper\Client
│   Methods
|   YourApiWrapperProvider
│
└───Api
│      YourTestMethodApi
│   
└───config
|       yourconfig.php override apiwrapper.php config
|
└───HttpClient
|       HttpClient with getSecurityParamsToUri function
|
└───Mappers
|       YourMapper extends Fenix007\Wrapper\Mappers\AbstractMapper
|
└───Models
        YourModdel extends Fenix007\Wrapper\Models\AbstractModel

您的包提供者返回 Client 类,允许快速访问 Api/classes 功能。您可以在方法类中描述所有 Api 方法,如下所示

<?php

namespace Siqwell\Eagle;

class Methods
{
    const TRANSLATIONS_GET_LIST = ['method' => 'GET', 'path' => '/streaming/translations.json'];
    const TRANSLATION_GET_INFO = ['method' => 'GET', 'path' => '/streaming/translations/{id}.json'];
}

在 Api 类中,您可以这样做

<?php
class RecordApi extends Fenix007\Wrapper\Api\AbstractApi
{
    /**
     * @param $id
     * @throws \Exception
     * @return \Siqwell\Eagle\Models\Record
     * ID – идентификатор записи
     */
    public function find($id) : ?Record
    {
        $parameters = [
            'id' => $id
        ];
        
        $result = $this->get(
            Request::createFromMethod(Methods::RECORD_GET_INFO, $parameters),
            RecordMapper::class
        );
        
        return $result;
    }
}

因此,在项目中,您可以使用 YourFacade::Record->find(1),这将返回 RecordModel

为 phpunit 测试,在 test/.../Api 目录中添加文件夹,创建一个从 \Fenix007\Wrapper\Tests\Api\TestCase 扩展的 TestCase 类,在那里您必须定义 $rootDir 和 $resourceDir 文件夹,如下

<?php
namespace Siqwell\Eagle\Tests\Api;

class TestCase extends \Fenix007\Wrapper\Tests\Api\TestCase
{
    const DYNAMIC_FIELDS = [
        'current_screenshot',
        'current_screenshot_small',
        'screenshot',
        'screenshot_small',
        'view_count',
        'updated_at'
    ];

    protected $rootDir = ROOT_DIR;
    protected $resourceDir = RESOURCES_DIR;
}

test/.../Api 目录中扩展 API 测试类。对于模拟的 JSON 响应,使用 test/.../Resources 目录,并使用与 URI 请求相同的目录结构。如果您想从 Resources 模拟文件夹进行测试,您需要在 setUp 方法中通过 createFakeHttpClient 函数传递测试 HttpClient

<?php
    protected function setUp()
    {
        parent::setUp();

        //TODO: change on real API
        $this->translationApi = new TranslationApi($this->createFakeHttpClient());
    }