PHP code example of germania-kg / authapi-client

1. Go to this page and download the library: Download germania-kg/authapi-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

germania-kg / authapi-client example snippets



namespace Germania\AuthApiClient;

interface AuthApiInterface
{
    public function getToken(string $username, string $password, bool refresh = false) : AuthTokenInterface;
    public function login(string $username, string $password) : AuthTokenInterface;
    public function refresh(AuthTokenInterface $token) : AuthTokenInterface;
}



class MyClient extends AuthApiAbstract {
  	// Implement abstract methods here
}

$my_client = new MyClient;

// LoggerAwareTrait
$my_client->setLogger( new Monolog );

// LoglevelTrait
$my_client->setErrorLoglevel( \Psr\Log\LogLevel::ERROR) );
$my_client->setSuccessLoglevel( \Psr\Log\LogLevel::INFO) );
$my_client->setErrorLoglevel("error");  
$my_client->setSuccessLoglevel("info");

use Germania\AuthApiClient\GuzzleAuthApi;

// Setup dependencies
$guzzle = new \GuzzleHttp\Client(['base_uri' => "https://api.test.com/"]);

// Optional with PSR-3 Logger support.
$client = new GuzzleAuthApi( $guzzle);
$client = new GuzzleAuthApi( $guzzle, new Monolog);

$guzzle = new \GuzzleHttp\Client( ... );
$client->setGuzzleClient($guzzle);  


use Germania\AuthApiClient\HttpClientAuthApi;  

// Setup dependencies
$base_url = "https://api.test.com/";
$psr_18 = new \GuzzleHttp\Client;

// Optional with PSR-3 Logger support.
$client = new HttpClientAuthApi($base_url, $psr_18);
$client = new HttpClientAuthApi($base_url, $psr_18, new Monolog);

$client->setBaseUrl("https://api.test.com/");

use GuzzleHttp\Client as Guzzle;
$client->setHttpClient( new Guzzle );

use Nyholm\Psr7\Factory\Psr17Factory;
$client->setRequestFactory( new Psr17Factory );
$client->setStreamFactory( new Psr17Factory );

use Germania\AuthApiClient\CacheAuthApiDecorator;
use Germania\AuthApiClient\GuzzleAuthApi;
use GuzzleHttp\Client as Guzzle;

// Setup dependencies
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter;
$inner = new GuzzleAuthApi( new Guzzle );

// Optional with PSR-3 Logger support.
$client = new CacheAuthApiDecorator($inner, $cache);
$client = new CacheAuthApiDecorator($inner, $cache, new Monolog);

$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter;
$client->setCacheItemPool($cache); 


use Germania\AuthApiClient\CacheAuthApiDecorator;
use Germania\AuthApiClient\GuzzleAuthApi;

// Dependencies
$guzzle = new \GuzzleHttp\Client(['base_uri' => "https://api.test.com/");
$cache  = new \Symfony\Component\Cache\Adapter\FilesystemAdapter;
$logger = new \Monolog\Monolog( ... );
               
// AuthAPi clients                                  
$inner  = new GuzzleAuthApi( $guzzle, $logger);
$client = new CacheAuthApiDecorator($inner, $cache, $logger);

// Retrieve AuthToken, either short-living or long-TTL
$token = $client->getToken("username", "password");  
$token = $client->getToken("username", "password", (bool) "refresh");  

// Work with AuthToken
echo get_class($token);     // \Germania\AuthApiClient\AuthToken
echo $token;                // "somerandomstring"
echo $token->getContent();  // "somerandomstring"  
echo $token->getLifeTime(); // e.g. 60


  
use Germania\AuthApiClient\Exceptions\{
  AuthApiRequestException,
  AuthApiResponseException,
  AuthApiExceptionInterface  
};

try {
	$token = $client->getToken("username", "password");  
}
catch (AuthApiRequestException $e) {
  $guzzle_exception = $e->getPrevious(); // Possibly previous Guzzle Exception
}
catch (AuthApiResponseException $e) {
  $json_error = $e->getPrevious(); // Possibly previous JSON error  
  echo $e->getMessage(); // "Access token missing" (or sort of)
}
catch (AuthApiExceptionInterface $e) {
	echo $e->getMessage();
}

interface AuthTokenInterface
{
    // Alias for "getContent"
    public function __toString();

    // Returns the token "text".
  	public function getContent() : string;

    // Returns the lifetime in seconds.
    public function getLifeTime() : int;
}


use Germania\AuthApiClient\AuthToken;

// Pass token string and TTL
$auth_token = new AuthToken( "somerandomstring", 3600);

// Inherited from "Token" class
echo $auth_token;                // "somerandomstring"
echo $auth_token->__toString();  // "somerandomstring"  
echo $auth_token->getContent();  // "somerandomstring"  
echo $auth_token->getLifeTime(); // 3600


use Germania\AuthApiClient\AuthTokenRequestFactory;

# Have your AuthToken and, optionally, another PSR-17 Request Factory at hand:
$auth_token = ...
$inner_request_factory = new \Nyholm\Psr7\Factory\Psr17Factory;

$psr17 = AuthTokenRequestFactory( $auth_token );
$psr17 = AuthTokenRequestFactory( $auth_token ), $inner_request_factory);

$request = $psr17->createRequest("GET", "/");
// true
$request->hasHeader("Authorization");