adexos / m2-jane-sdk-bridge
此包最新版本(2.0.2)没有提供许可证信息。
Adexos Jane SDK Bridge for Magento 2
2.0.2
2024-04-29 12:52 UTC
Requires
- php: ^7.4 || ^8.0
- nyholm/psr7: ^1.4
- php-http/logger-plugin: ^1.3
- symfony/http-client: >= 4.4
README
如何安装
您可以通过输入以下命令来安装: composer require adexos/m2-jane-sdk-bridge
如何使用
此 SDK Bridge 是一个工具,它通过您的 Magento 2 应用程序更轻松地实现 Jane SDK 生成的客户端。
在您的 di.xml
中,您需要声明
<!-- Start HTTP Config --> <type name="Vendor\MySDKBridge\Http\MyClient"> <arguments> <argument name="client" xsi:type="object">MyHttpClient</argument> </arguments> </type> <virtualType name="MyHttpClient" type="Adexos\JaneSDKBridge\Http\Client"> <arguments> <argument name="plugins" xsi:type="array"> <item name="hostPlugin" xsi:type="object">MyClientHostPlugin</item> <item name="pathPlugin" xsi:type="object">MyClientPathPlugin</item> </argument> <!-- You can change it if you are willing to override the generated client or extends it --> <argument name="clientName" xsi:type="string">PathTo\Generated\Jane\Client</argument> </arguments> </virtualType> <virtualType name="MyClientHostPlugin" type="Adexos\JaneSDKBridge\Http\Plugins\HostPlugin"> <arguments> <argument name="configPath" xsi:type="const"> Vendor\MySDKBridge\Model\Config::PATH_TO_ENDPOINT_URL_YOU_MUST_DEFINE </argument> </arguments> </virtualType> <virtualType name="MyClientPathPlugin" type="Adexos\JaneSDKBridge\Http\Plugins\PathPlugin"> <arguments> <argument name="configPath" xsi:type="const"> Vendor\MySDKBridge\Model\Config::XPATH_TO_ENDPOINT_URL_YOU_MUST_DEFINE </argument> </arguments> </virtualType> <!-- End HTTP Config -->
您的 Vendor\MySDKBridge\Http\MyClient
所在的位置
<?php declare(strict_types=1); namespace Vendor\MySDKBridge\Http; use Adexos\JaneSDKBridge\Http\Bridge\ClientExtender; class MyClient { private \Adexos\JaneSDKBridge\Http\Client $client; public function __construct(\Adexos\JaneSDKBridge\Http\Client $client) { $this->client = $client; } public function getClient(): \PathTo\Generated\JaneClient { //This class allows you to have an attachment point to the HTTP client //and to have hint types because of the generic SDKs //you can do whatever you want here such as do some checks or passing scope for the bearer token : // return $this->client->getClient(['scope' => 'my_custom_scope'']); return $this->client->getClient(); } }
您必须扩展此特定类以实现注入客户端的自动完成。
然后,在类中您可以注入
<?php declare(strict_types=1); namespace Vendor\Magento\Helper; use Magento\Customer\Api\Data\CustomerInterface; use Vendor\MySDKBridge\Http\MyClient; class UpdateCustomers { private MyClient $client; public function __construct(MyClient $client) public function updateCustomer(CustomerInterface $customer): void { //or whatever endpoint available on your JanePHP SDK $this->client->getClient()->updateCustomer(['firstname' => $customer->getFirstname()]); } }
身份验证
您可能希望以通用方式使用 JanePHP 提供的身份验证(例如 API 密钥或 Bearer)。
JanePHP 在您的 SDK 的 Authentication
文件夹中生成(如果需要)一个身份验证类
PathTo\Generated\Authentication\ApiKeyAuthentication
为此,请使用此模块中提供的 ApiKeyPluginWrapper
并将其添加到您的插件中
您可以使用以下认证方法
- Bearer 授权
- 基本认证授权
<type name="Vendor\MySDKBridge\Http\MyClient"> <arguments> <argument name="client" xsi:type="object">MyHttpClient</argument> </arguments> </type> <virtualType name="MyHttpClient" type="Adexos\JaneSDKBridge\Http\Client"> <arguments> <argument name="plugins" xsi:type="array"> <!-- Your plugins... --> </argument> <argument name="apiKeyPlugin" xsi:type="object">MyApiKeyPlugin</argument> <argument name="bearerPlugin" xsi:type="object">MyBearerPlugin</argument> <argument name="basicAuthPlugin" xsi:type="object">MyBasicAuthPlugin</argument> </arguments> </type> <virtualType name="MyApiKeyPlugin" type="Adexos\JaneSDKBridge\Http\Plugins\Auth\ApiKeyPluginWrapper"> <arguments> <argument name="authenticationPluginClass" xsi:type="string"> PathTo\Generated\Authentication\ApiKeyAuthentication </argument> <!-- XML path to your ENCRYPTED API Key --> <argument name="configPath" xsi:type="const"> Vendor\MySDKBridge\Model\Config::XML_PATH_ZDFR_CUSTOMER_SDK_API_KEY </argument> </arguments> </virtualType> <!-- Not needed if no Bearer are required --> <virtualType name="MyBearerPlugin" type="Adexos\JaneSDKBridge\Http\Plugins\Auth\BearerPluginWrapper"> <arguments> <argument name="bearerPluginClass" xsi:type="string"> PathTo\Generated\Authentication\Bearer </argument> </arguments> </virtualType> <!-- Not needed if no Basic auth are required --> <virtualType name="MyBasicAuthPlugin" type="Adexos\JaneSDKBridge\Http\Plugins\Auth\BasicAuthPluginWrapper"> <arguments> <argument name="basicAuthPluginClass" xsi:type="string"> PathTo\Generated\Authentication\BasicAuth </argument> <argument name="configPathUsername" xsi:type="const"> Vendor\MySDKBridge\Model\Config::XML_PATH_API_BASIC_AUTH_USERNAME </argument> <argument name="configPathPassword" xsi:type="const"> Vendor\MySDKBridge\Model\Config::XML_PATH_API_BASIC_AUTH_PASSWORD </argument> </arguments> </virtualType>
/!\ 如果您想加密 API 密钥或密码,您可以在
system.xml
文件中使用 Magento 2 后端模型Magento\Config\Model\Config\Backend\Encrypted
,如下例所示
<field id="api_key" translate="label" type="obscure" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Api key</label> <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model> </field>
免责声明
您的 PHP 应用程序必须实现自己的 Client
或安装一些包来处理
Psr\Http\Client\ClientInterface
PSR-18Psr\Http\Message\RequestFactoryInterface
PSR-17Psr\Http\Message\StreamFactoryInterface
PSR-17
在此包中
- PSR-18 由
symfony/http-client >= 4.4
处理 - PSR-17 由
nyholm/psr7 ^1.4
处理
只要您使用的包遵守 PSR 标准,您就可以使用您想要的任何包。
日志记录器
- 此桥接实现了 http 日志记录插件,如果您启用此功能,则可以将所有请求和响应记录到 loggerInterface。您可以在 Magento 的配置部分中管理此设置。