f1monkey / eve-esi-bundle

此包已被弃用,不再维护。未建议替代包。

EVE Swagger接口(ESI)symfony包

安装: 39

依赖者: 0

建议者: 0

安全性: 0

星级: 0

观察者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

0.2.0 2020-07-02 16:02 UTC

This package is auto-updated.

Last update: 2021-08-30 18:23:16 UTC


README

用于EVE Swagger接口的Symfony4/Symfony5包

安装

$ composer require f1monkey/eve-esi-bundle

添加到 config/bundles.php

<?php
return [
    // ...
    F1monkey\EveEsiBundle\F1monkeyEveEsiBundle::class => ['all' => true],
];

创建配置文件(例如 config/packages/f1monkey_eve_esi.yaml

配置

f1monkey_eve_esi:
    user_agent: 'Test/1.0' # example User-Agent header for SSO/ESI requests, default is null, optional parameter
    oauth: # required if you want to use EVE SSO, otherwise can be skipped
        callback_url: '%env(EVE_ESI_CALLBACK_URL)%' # your app's callback url (same as in your application settings)
        client_id: '%env(EVE_ESI_CLIENT_ID)%' # your app's client id
        client_secret: '%env(EVE_ESI_CLIENT_SECRET)%' # your app's client secret

用法

SSO(获取访问令牌)

生成重定向URL

首先,您需要将用户重定向到认证URL。使用OAuthServiceInterface::createRedirectUrl生成此URL。

<?php

use Doctrine\Common\Collections\ArrayCollection;
use F1monkey\EveEsiBundle\Dto\Scope;
use F1monkey\EveEsiBundle\Service\OAuth\OAuthServiceInterface;

class MyService {
    public function __construct(OAuthServiceInterface $oauthService)
    {
        $this->oauthService = $oauthService;
    }

    /**
     * Generate url to redirect user for authentication
     */
    public function getRedirectUrl()
    {
        // Tokens will be generated with he scopes you set here
        $scopes = new ArrayCollection(
            [
                new Scope('publicData'),
                new Scope('esi-calendar.respond_calendar_events.v1'),
            ]
        );

        return $this->oauthService->createRedirectUrl($scopes);
    }
}
处理授权回调

用户认证后,他将被重定向回配置中指定的回调_url。请求将包含授权代码(例如 https:///callback?code=qwerty)。您应使用此代码来获取访问和刷新令牌

<?php
use F1monkey\EveEsiBundle\Exception\OAuth\OAuthRequestException;

// ...

    public function verifyAuthCode(string $authCode)
    {
        try {
            $tokens = $this->oauthService->verifyCode($authCode);
        } catch (OAuthRequestException $e) {
            // handle request exceptions (4xx and 5xx errors)
            $errorResponse = $e->getErrorResponse();
            $httpCode = $e->getStatusCode();
        }
        $accessToken = $tokens->getAccessToken(); // this token will be used in ESI methods (https://esi.evetech.net)
        $refreshToken = $tokens->getRefreshToken(); // this token is to get a new accessToken when it is expired (@see next method)

        // save access and refresh tokens to database
    }
刷新访问令牌

访问令牌有效期为15分钟。之后,您应使用刷新令牌获取新的令牌

try {
    $tokens = $this->oauthService->refreshToken($refreshToken);
} catch (OAuthRequestException $e) {
    // handle request exceptions (4xx and 5xx errors)
}
// save refresh and access tokens to database

ESI

通过访问令牌获取角色信息
<?php

use F1monkey\EveEsiBundle\Exception\Esi\EsiRequestException;
use F1monkey\EveEsiBundle\Service\Esi\VerifyTokenServiceInterface;

class MyService
{
    // ...
    public function __construct(VerifyTokenServiceInterface $verifyTokenService)
    {
        $this->verifyTokenService = $verifyTokenService;
    }

    public function getCharacterInfo(string $accessToken)
    {
        try {
            $response = $this->verifyTokenService->verifyAccessToken($accessToken);
        } catch (EsiRequestException $e) {
            // handle request exception
        }
        $characterId   = $response->getCharacterId();
        $characterName = $response->getCharacterName();
        $scopes        = $response->getScopes();
    }
}

使用ETag

具有$eTag参数的请求方法应该是可缓存的(文档)。

响应将包含缓存标签(见 HasETagInterface)。如果您在下次调用中传递此值,且值未更改,您将得到NotModifiedException。这意味着您应该使用缓存响应数据。示例

<?php

/** @var \F1monkey\EveEsiBundle\Service\Esi\MarketServiceInterface $service */
$response = $service->getV2CharactersOrders('token', 123456);

$eTag = $response->getEtag();
try {
    $response = $service->getV2CharactersOrders('token', 123456, $eTag);
} catch (\F1monkey\EveEsiBundle\Exception\Esi\NotModifiedException $e) {
    // use previous response
}

测试

运行Codeception测试

$ composer test

运行静态分析器

$ composer phpstan