netflex/api

Netflex API 库

v5.0.2 2023-08-25 10:39 UTC

This package is auto-updated.

Last update: 2024-09-16 16:13:59 UTC


README

Stable version Build status License: MIT Contributors Downloads

[只读] Netflex API 组件的子树分割(见 netflex/api

用于操作 Netflex API 的库。

文档

目录

安装

1. 设置

composer require netflex/api

注意:如果您使用的是 Netflex SDK 2.0 或更高版本或 Laravel 5.5,则步骤 2 和 3(提供者和别名)是不必要的。Netflex API 支持 包发现

2. 提供者

您需要更新应用程序配置以注册包,以便 Laravel 可以加载它,只需在您的 config/app.php 文件的 'providers' 部分末尾添加以下代码即可

config/app.php

<?php

return [
    // ...
    'providers' => [
        Netflex\API\Providers\APIServiceProvider::class,
        // ...
    ],
    // ...
];

3. 外观

您可以使用以下外观访问 Netflex API

  • Netflex\API\Facades\API

您可以在您的 config/app.php 文件中为这些外观设置简写别名。例如

<?php

return [
    // ...
    'aliases' => [
        'API'   => Netflex\API\Facades\API::class,
        // ...
    ],
    // ...
];

4. 配置

发布配置

在您的终端中输入

php artisan vendor:publish

或者

php artisan vendor:publish --provider="Netflex\API\Providers\APIServiceProvider"

或者,您可以手动创建配置文件,在 config/api.php 下,在此处查看示例

独立使用

Netflex API 也可以独立使用。

<?php

use Netflex\API\Client;

$client = new Client([
  'auth' => ['publicKey', 'privateKey']
]);

测试

为了测试目的,请使用提供的 Netflex\API\Testing\MockClient 实现。这可以与外观一起使用,因此在测试时不需要修改您的代码。

要将 MockClient 绑定到容器中,请注册 Netflex\API\Testing\Providers\MockAPIServiceProvider 提供者。

示例

<?php

use Illuminate\Container\Container;
use Illuminate\Support\Facades\Facade;

use Netflex\API\Facades\API;
use Netflex\API\Testing\Providers\MockAPIServiceProvider;

use GuzzleHttp\Psr7\Response;

$container = new Container;

Facade::setFacadeApplication($container);

// Register the service provider
(new MockAPIServiceProvider($container))->register();

// The container binding is now registered, and you can use the Facade.

API::mockResponse(
  new Response(
    200,
    ['Content-Type' => 'application/json'],
    json_encode(['hello' => 'world'])
  )
);

$response = API::get('/'); // Returns the mocked response

echo $response->hello; // Outputs 'world'