insidesuki/solapimanager

delSOl ApiClient

2.2.5 2022-10-11 04:52 UTC

README

Provides an abstract repository to your symfony 5.4 project or standalone project, to make query requests to the ContaSol API

Features

  • Support for multiple credentials via $_ENV files and a Class to represent the credentials.
  • Simple Key Value for query Conditions (findOneBy,insert and update)
     // example for findOneBy condition
     $condition = ['codcli'=>1]
   
     // example for insert petition
     $insert = [
               'codcli' => '9989'
               'nomcli' => 'faraday'
           ];

REPOSITORY APIS

Installation

composer require insidesuki/solapimanager

FirstSteps

1- Add to yout .env file

###> 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- Creating Credentials Class

  • Go to DELSOL_API_CREDENTIALS path and create a credential for each API defined in your $_ENV file. Must be extends from 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- Creating a Repository

  • Create a repository that extends from AbstractDelSolRepository, define the table,credential, etc

      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]);
          }
     }
    

Without Symfony

  • Perform all the above steps
  • Creates a file like:
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);