insidesuki/solapimanager

delSOl ApiClient

2.2.5 2022-10-11 04:52 UTC

README

为您的 symfony 5.4 项目或独立项目提供一个抽象仓库,以便对 ContaSol API 进行查询请求

特性

  • 支持通过 $_ENV 文件进行多凭证操作,并有一个类来表示凭证。
  • 简单的键值查询条件(findOneBy、insert 和 update)
     // example for findOneBy condition
     $condition = ['codcli'=>1]
   
     // example for insert petition
     $insert = [
               'codcli' => '9989'
               'nomcli' => 'faraday'
           ];

REPOSITORY APIS

安装

composer require insidesuki/solapimanager

第一步

1- 添加到 .env 文件

###> DELSOL API CREDENTIAL

### Namespace to use as credentials folder
DELSOL_API_CREDENTIALS="YourProjectNamespace\\ToUse\\AS\\CredentialFolderStore\\"


APIDELSOL_BASE_URL=https://api.sdelsol.com
APIDELSOL_AUTH_URL=https://api.sdelsol.com/login/Autenticar
APIDELSOL_TOKEN_RESPONSE=resultado
APIDELSOL_TOKEN_EXPIRES=60;

## CREDENTIALS1, use as key name the one you want
API1_NAME=delSol1
API1_CODIGO_FABRICANTE=***
API1_CODIGO_CLIENTE=*****
API1_DB=****
API1_SECRET=******

## CREDENTIALS1
API2_NAME=delSol1
API2_CODIGO_FABRICANTE=***
API2_CODIGO_CLIENTE=*****
API2_DB=****
API2_SECRET=******

###< delsolApiClient ###

2- 创建凭证类

  • 转到 DELSOL_API_CREDENTIALS 路径,为在 $_ENV 文件中定义的每个 API 创建一个凭证。必须继承自 AbstractDelSolCredential

    namespace F2admin\Application\Contabilidad\Infrastructure\Storage\DelSolApi\Credentials; use Insidesuki\ApiClient\Authentification\Contracts\ApiBearerCredentialInterface;

    class DefaultSolCredential extends AbstractDelSolCredential implements ApiBearerCredentialInterface {

    public function getApiName(): string
    {
	    return $_ENV['API1_NAME'];
    }
    public function getBodyAuth(): array
    {
	    return [
		    'codigoFabricante' => $_ENV['API1_CODIGO_FABRICANTE'],
		    'codigoCliente'    => $_ENV['API1_CODIGO_CLIENTE'],
		    'baseDatosCliente' => $_ENV['API1_DB'],
		    'password'         => $_ENV['API1_SECRET']
	    ];
    }
}

3- 创建仓库

  • 创建一个继承自 AbstractDelSolRepository 的仓库,定义表、凭证等

      namespace YourProject\Namespace\Repository;
      use Symfony\Contracts\HttpClient\HttpClientInterface;
    
     class ClientExampleRepository extends AbstractDelSolRepository
     {
          public function __construct(HttpClientInterface $httpClient)
          {
              parent::__construct($httpClient);
          }
            
          // define the delSol tablename
          public function tableName(): string
         {
             return 'f_cli';
         }
    
          public function findById(int $idClient)
          {
              return $this->delSolManager->findOne(['codcli' => $idClient]);
          }
     }
    

不使用 Symfony

  • 执行上述所有步骤
  • 创建一个文件,例如
require __DIR__ . '/vendor/autoload.php';
use YourProject\Namespace\Repository\ClientExampleRepository;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\NativeHttpClient;

try {
    	$dotEnv = new Dotenv();
        $dotEnv->load(__DIR__ . '/../config/.env.local');

        $clientRepository = new ClientExampleRepository(
	      new NativeHttpClient()
        );

        // set the credential to use
        $clientRepository->setManager('DelSolCredentialA');
        $client = $clientRepository->findById(1);
}
catch (Exception $e) {
    dd($e->getMessage());
}

dd($client);