gdinko / prim
Laravel Prim API 包装器
v1.0.7
2024-06-17 16:58 UTC
Requires
- php: ^7.4|^8.0|^8.1|^8.2|^8.3
- guzzlehttp/guzzle: ^7.4
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- nunomaduro/collision: ^5.11
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.0
README
安装
您可以通过 composer 安装此包
composer require gdinko/prim
如果您计划使用数据库存储请求/错误日志
php artisan migrate --step
如果您需要导出配置文件
php artisan vendor:publish --tag=prim-config
如果您需要导出迁移文件
php artisan vendor:publish --tag=prim-migrations
如果您需要导出模型
php artisan vendor:publish --tag=prim-models
如果您需要导出命令
php artisan vendor:publish --tag=prim-commands
配置
将此添加到 .env 文件中
PRIM_API_TOKEN= #Get API token from prim.bg PRIM_API_BASE_URL= #Get API Base URL from prim.bg PRIM_API_TIMEOUT= #default 30 PRIM_ENABLE_REQUEST_LOG=true/false #default false | log every request/response in database PRIM_ENABLE_ERROR_LOG=true/false #default true | log every error response in database
用法
运行时设置
Prim::setApiToken('token'); Prim::getApiToken(); Prim::setBaseUrl('endpoint'); Prim::getBaseUrl(); Prim::setTimeout(99); Prim::getTimeout(); Prim::enableRequestLog(); Prim::disableRequestLog(); Prim::enableErrorLog(); Prim::disableErrorLog();
方法
//list all API methods and params Prim::getAllMethods(): array //get info about method params Prim::getMethodParams($method, $verb): array //auto generate validation rules for API method Prim::getValidationRules($method, $verb): array /** * getAccounts * * Prim Endpoint: RPC.common.Api.Accounts.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getAccounts(PrimRequestInterface $request, $wantAll = false): array /** * setAddresses * * Prim Endpoint: RPC.common.Api.Addresses.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setAddresses(PrimRequestInterface $request, $wantAll = false): array /** * getExpenseAllocations * * Prim Endpoint: RPC.common.Api.Allocations.getExpense * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getExpenseAllocations(PrimRequestInterface $request, $wantAll = false): array /** * getAvailabilities * * Prim Endpoint: RPC.common.Api.Availabilities.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getAvailabilities(PrimRequestInterface $request, $wantAll = false): array /** * getAvailabilitiesByLabels * * Prim Endpoint: RPC.common.Api.AvailabilitiesByLabels.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getAvailabilitiesByLabels(PrimRequestInterface $request, $wantAll = false): array /** * getBlanks * * Prim Endpoint: RPC.common.Api.Blanks.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getBlanks(PrimRequestInterface $request, $wantAll = false): array /** * getBonusPoints * * Prim Endpoint: RPC.common.Api.BonusPoints.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getBonusPoints(PrimRequestInterface $request, $wantAll = false): array /** * setBrands * * Prim Endpoint: RPC.common.Api.Brands.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setBrands(PrimRequestInterface $request, $wantAll = false): array /** * getClientTypes * * Prim Endpoint: RPC.common.Api.ClientTypes.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getClientTypes(PrimRequestInterface $request, $wantAll = false): array /** * setContacts * * Prim Endpoint: RPC.common.Api.Contacts.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setContacts(PrimRequestInterface $request, $wantAll = false): array /** * getCurrencies * * Prim Endpoint: RPC.common.Api.Currencies.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getCurrencies(PrimRequestInterface $request, $wantAll = false): array /** * getDeliveryTypes * * Prim Endpoint: RPC.common.Api.DeliveryTypes.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getDeliveryTypes(PrimRequestInterface $request, $wantAll = false): array /** * getDocInfo * * Prim Endpoint: RPC.common.Api.DocInfo.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getDocInfo(PrimRequestInterface $request, $wantAll = false): array /** * getFiles * * Prim Endpoint: RPC.common.Api.Files.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getFiles(PrimRequestInterface $request, $wantAll = false): array /** * annulFinDocFinDoc * * Prim Endpoint: RPC.common.Api.FinDoc.annulFinDoc * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::annulFinDocFinDoc(PrimRequestInterface $request, $wantAll = false): array /** * setGroups * * Prim Endpoint: RPC.common.Api.Groups.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setGroups(PrimRequestInterface $request, $wantAll = false): array /** * getInterationSystems * * Prim Endpoint: RPC.common.Api.InterationSystems.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getInterationSystems(PrimRequestInterface $request, $wantAll = false): array /** * setInvoice * * Prim Endpoint: RPC.common.Api.Invoice.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setInvoice(PrimRequestInterface $request, $wantAll = false): array /** * setItems * * Prim Endpoint: RPC.common.Api.Items.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setItems(PrimRequestInterface $request, $wantAll = false): array /** * getListOperation * * Prim Endpoint: RPC.common.Api.ListOperation.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getListOperation(PrimRequestInterface $request, $wantAll = false): array /** * setMeasures * * Prim Endpoint: RPC.common.Api.Measures.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setMeasures(PrimRequestInterface $request, $wantAll = false): array /** * getOperations * * Prim Endpoint: RPC.common.Api.Operations.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getOperations(PrimRequestInterface $request, $wantAll = false): array /** * getParams * * Prim Endpoint: RPC.common.Api.Params.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getParams(PrimRequestInterface $request, $wantAll = false): array /** * setPartners * * Prim Endpoint: RPC.common.Api.Partners.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setPartners(PrimRequestInterface $request, $wantAll = false): array /** * getPayTypes * * Prim Endpoint: RPC.common.Api.PayTypes.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getPayTypes(PrimRequestInterface $request, $wantAll = false): array /** * fullAnnulPoPo * * Prim Endpoint: RPC.common.Api.Po.fullAnnulPo * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::fullAnnulPoPo(PrimRequestInterface $request, $wantAll = false): array /** * getPos * * Prim Endpoint: RPC.common.Api.Pos.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getPos(PrimRequestInterface $request, $wantAll = false): array /** * setPrices * * Prim Endpoint: RPC.common.Api.Prices.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setPrices(PrimRequestInterface $request, $wantAll = false): array /** * getPricesLists * * Prim Endpoint: RPC.common.Api.PricesLists.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getPricesLists(PrimRequestInterface $request, $wantAll = false): array /** * setServices * * Prim Endpoint: RPC.common.Api.Services.set * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::setServices(PrimRequestInterface $request, $wantAll = false): array /** * getSoInvoicePdfSo * * Prim Endpoint: RPC.common.Api.So.getSoInvoicePdf * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getSoInvoicePdfSo(PrimRequestInterface $request, $wantAll = false): array /** * getSoOrderTypes * * Prim Endpoint: RPC.common.Api.SoOrderTypes.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getSoOrderTypes(PrimRequestInterface $request, $wantAll = false): array /** * deleteStoreIn * * Prim Endpoint: RPC.common.Api.StoreIn.delete * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::deleteStoreIn(PrimRequestInterface $request, $wantAll = false): array /** * deleteStoreOut * * Prim Endpoint: RPC.common.Api.StoreOut.delete * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::deleteStoreOut(PrimRequestInterface $request, $wantAll = false): array /** * getStores * * Prim Endpoint: RPC.common.Api.Stores.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getStores(PrimRequestInterface $request, $wantAll = false): array /** * getTaxDeals * * Prim Endpoint: RPC.common.Api.TaxDeals.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getTaxDeals(PrimRequestInterface $request, $wantAll = false): array /** * getTaxInstances * * Prim Endpoint: RPC.common.Api.TaxInstances.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getTaxInstances(PrimRequestInterface $request, $wantAll = false): array /** * getVendorTypes * * Prim Endpoint: RPC.common.Api.VendorTypes.get * * * @param \Gdinko\Prim\Interfaces\PrimRequestInterface $request * @param boolean $wantAll * @return array * * @throws \Gdinko\Prim\Exceptions\PrimException * @throws \Gdinko\Prim\Exceptions\PrimValidationException */ Prim::getVendorTypes(PrimRequestInterface $request, $wantAll = false): array
命令
#get prim api status (use -h to view options) #need to run migrations for this command to store data php artisan prim:api-status
模型
PrimApiStatus PrimLog PrimErrorLog
事件
PrimHookEvent
接口
如果您需要自定义 PrimRequest,需要实现此接口
PrimRequestInterface
请求
实现 PrimRequestInterface
PrimRequest PrimNoValidationRequest //no client site data validation
请求示例
$pr = new PrimRequest( ['code' => 'Code', 'name' => 'Name'], //request data false, //get all | true / false Prim::getValidationRules('RPC.common.Api.Accounts', 'get') // auto generate validation rules | or skip this param | or pass custom validation rules )
监听 Prim 钩子事件
- 在 Prim 控制台中订阅钩子并设置您的 API 钩子 URL:[your domain]/api/prim/hook?api_token=[laravel:sanctum:token]
//subscribe to PrimHookEvent in EventServiceProvider->boot method //https://laravel.net.cn/docs/9.x/events Event::listen(function (PrimHookEvent $event) { //do something cool with prim payload dd($event->payload); });
示例
获取所有 Prim API 方法
use Gdinko\Prim\Facades\Prim; use Gdinko\Prim\Exceptions\PrimException; use Gdinko\Prim\Exceptions\PrimValidationException; try { $methods = Prim::getAllMethods(); dd($methods); } catch (PrimException | PrimValidationException $pe) { echo class_basename($pe) . '<br />'; echo $pe->getCode() . '<br />'; echo $pe->getMessage() . '<br />'; dd($pe->getErrors()); } catch (\Exception $e) { echo class_basename($e) . '<br />'; echo $e->getCode() . '<br />'; echo $e->getMessage() . '<br />'; }
获取所有账户
*此示例用法适用于所有方法
use Gdinko\Prim\Facades\Prim; use Gdinko\Prim\Exceptions\PrimException; use Gdinko\Prim\Exceptions\PrimValidationException; use Gdinko\Prim\Requests\PrimRequest; try { $accounts = Prim::getAccounts( new PrimRequest( [], //empty params true, //get all flag ) ); dd($accounts); } catch (PrimException | PrimValidationException $pe) { echo class_basename($pe) . '<br />'; echo $pe->getCode() . '<br />'; echo $pe->getMessage() . '<br />'; dd($pe->getErrors()); } catch (\Exception $e) { echo class_basename($e) . '<br />'; echo $e->getCode() . '<br />'; echo $e->getMessage() . '<br />'; }
获取所有账户,不进行客户端站点验证请求,并获取所有 prim 响应,而不仅仅是结果字段
*此示例用法适用于所有方法
use Gdinko\Prim\Facades\Prim; use Gdinko\Prim\Exceptions\PrimException; use Gdinko\Prim\Exceptions\PrimValidationException; use Gdinko\Prim\Requests\PrimNoValidationRequest; try { $accounts = Prim::getAccounts( new PrimNoValidationRequest( [], //empty params, no client site validation only prim validation true, //get all flag ), true ); dd($accounts); } catch (PrimException $pe) { echo class_basename($pe) . '<br />'; echo $pe->getCode() . '<br />'; echo $pe->getMessage() . '<br />'; dd($pe->getErrors()); } catch (\Exception $e) { echo class_basename($e) . '<br />'; echo $e->getCode() . '<br />'; echo $e->getMessage() . '<br />'; }
使用自动生成的请求验证规则搜索账户
*此示例用法适用于所有方法
use Gdinko\Prim\Facades\Prim; use Gdinko\Prim\Exceptions\PrimException; use Gdinko\Prim\Exceptions\PrimValidationException; use Gdinko\Prim\Requests\PrimRequest; try { $accounts = Prim::getAccounts( new PrimRequest( ['code' => 'CoDe', 'name' => 'NaMe'], //request params false, //dot not return all Prim::getValidationRules('RPC.common.Api.Accounts', 'get') //auto generate validation rules ) ); dd($accounts); } catch (PrimException | PrimValidationException $pe) { echo class_basename($pe) . '<br />'; echo $pe->getCode() . '<br />'; echo $pe->getMessage() . '<br />'; dd($pe->getErrors()); } catch (\Exception $e) { echo class_basename($e) . '<br />'; echo $e->getCode() . '<br />'; echo $e->getMessage() . '<br />'; }
测试
composer test
变更日志
请参阅 CHANGELOG 获取有关最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 dinko359@gmail.com 反馈,而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。