Skip to content

Commit

Permalink
feat: support http auth basic
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexR1712 committed Sep 13, 2024
1 parent fdf21a7 commit 7fdb694
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 50 deletions.
13 changes: 12 additions & 1 deletion SAPb1/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SAPb1;

class Config{
class Config {

private $config = [];

Expand Down Expand Up @@ -41,4 +41,15 @@ private function get(string $name, $default = null){
}
return $default;
}


public function getAuthBasicString() : string{
$username = json_encode([
'UserName' => $this->get('username'),
'CompanyDB' => $this->get('company'),
]);
$password = $this->get('password');

return base64_encode($username . ':' . $password);
}
}
7 changes: 2 additions & 5 deletions SAPb1/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Query{

private $config;
private $session;
private $serviceName;
private $query = [];
private $filters = [];
Expand All @@ -16,9 +15,8 @@ class Query{
/**
* Initializes a new instance of Query.
*/
public function __construct(Config $config, array $session, string $serviceName, array $headers){
public function __construct(Config $config, string $serviceName, array $headers){
$this->config = $config;
$this->session = $session;
$this->serviceName = $serviceName;
$this->headers = $headers;
}
Expand Down Expand Up @@ -153,13 +151,12 @@ private function doRequest(string $action = '', callable $callback = null){
// Execute the service API with the query string.
$request = new Request($this->config->getServiceUrl($this->serviceName . $action) . ($requestQuery !== '' ? '?' : '') . $requestQuery, $this->config->getSSLOptions());
$request->setMethod('GET');
$request->setHeaders(['Authorization' => 'Basic ' . $this->config->getAuthBasicString()]);
$request->setHeaders($this->headers);

// Set the maxpagesize odata parameter.
$request->setHeaders(["Prefer" => "odata.maxpagesize={$this->maxPageSize}"]);

// Set the SAP B1 session data.
$request->setCookies($this->session);
$response = $request->getResponse();

// Check if the response code is successful.
Expand Down
42 changes: 5 additions & 37 deletions SAPb1/SAPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,26 @@
*/
class SAPClient{

private $config = [];
private $session = [];
private Config $config;

/**
* Initializes SAPClient with configuration and session data.
* Initializes SAPClient with configuration and auth basic data.
*/
public function __construct(array $configOptions, array $session){
public function __construct(array $configOptions){
$this->config = new Config($configOptions);
$this->session = $session;
}

/**
* Returns a new instance of SAPb1\Service.
*/
public function getService(string $serviceName) : Service{
return new Service($this->config, $this->session, $serviceName);
}

/**
* Returns the current SAP B1 session data.
*/
public function getSession() : array{
return $this->session;
return new Service($this->config, $serviceName);
}

/**
* Returns a new instance of SAPb1\Query, which allows for cross joins.
*/
public function query($join, $headers = []) : Query{
return new Query($this->config, $this->session, '$crossjoin('. str_replace(' ', '', $join) . ')', $headers);
}

/**
* Creates a new SAP B1 session and returns a new instance of SAPb1\Client.
* Throws SAPb1\SAPException if an error occurred.
*/
public static function createSession(array $configOptions, string $username, string $password, string $company) : SAPClient{

$config = new Config($configOptions);

$request = new Request($config->getServiceUrl('Login'), $config->getSSLOptions());
$request->setMethod('POST');
$request->setPost(['UserName' => $username, 'Password' => $password, 'CompanyDB' => $company]);
$response = $request->getResponse();

if($response->getStatusCode() === 200){
return new SAPClient($config->toArray(), $response->getCookies());
}

throw new SAPException($response, [
'action' => 'login',
'status_code' => $response->getStatusCode(),
]);
return new Query($this->config,'$crossjoin('. str_replace(' ', '', $join) . ')', $headers);
}
}
18 changes: 11 additions & 7 deletions SAPb1/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
*/
class Service{

private $config;
private $session;
private Config $config;
private $serviceName;
private $headers = [];
public $serviceViewName;

/**
* Initializes a new instance of Service.
*/
public function __construct(Config $configOptions, array $session, string $serviceName){
public function __construct(Config $configOptions, string $serviceName){
$this->config = $configOptions;
$this->session = $session;
$this->serviceName = $serviceName;
}

Expand Down Expand Up @@ -123,7 +121,7 @@ public function action($id, string $action) : bool{
* Returns a new instance of SAPb1\Query.
*/
public function queryBuilder() : Query{
return new Query($this->config, $this->session, $this->serviceName, $this->headers);
return new Query($this->config, $this->serviceName, $this->headers);
}

/**
Expand All @@ -140,7 +138,10 @@ public function setHeaders($headers) : Service{
public function getMetaData() : array{
$request = new Request($this->config->getServiceUrl('$metadata'), $this->config->getSSLOptions());
$request->setMethod('GET');
$request->setCookies($this->session);
$authHeader = ['Authorization' => 'Basic ' . $this->config->getAuthBasicString()];
$this->headers = array_merge($this->headers, $authHeader);
$request->setHeaders($this->headers);

$response = $request->getResponse();

$dom = new \DOMDocument();
Expand Down Expand Up @@ -191,7 +192,10 @@ public function getMetaData() : array{
private function doRequest($method, $postData, $action = '') : Response{
$request = new Request($this->config->getServiceUrl($this->serviceName) . $action, $this->config->getSSLOptions());
$request->setMethod($method);
$request->setCookies($this->session);

$authHeader = ['Authorization' => 'Basic ' . $this->config->getAuthBasicString()];
$this->headers = array_merge($this->headers, $authHeader);

$request->setHeaders($this->headers);
$request->setPost($postData);

Expand Down

0 comments on commit 7fdb694

Please sign in to comment.