Skip to content

Commit

Permalink
feat: apply support and add method setHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimerson21 committed Feb 23, 2022
1 parent 3451445 commit 7c65a22
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
25 changes: 22 additions & 3 deletions SAPb1/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ class Query{
private $query = [];
private $filters = [];
private $maxPageSize = 20;
private $headers = [];
private $apply = '';

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

/**
Expand Down Expand Up @@ -78,11 +81,19 @@ public function orWhere(Filters\Filter $filter) : Query{
/**
* Specifies the navigation properties to expand.
*/
public function expand($name){
public function expand($name) : Query{
$this->query['expand'] = $name;
return $this;
}

/**
* Adds a apply string to the results.
*/
public function apply(string $apply) : Query{
$this->apply = $apply;
return $this;
}

/**
* Returns a count of the result.
*/
Expand Down Expand Up @@ -119,6 +130,11 @@ private function doRequest(string $action = '', callable $callback = null){
$requestQuery .= '$' . $name . '=' . rawurlencode($value) . '&';
}

// Append the apply to the query string.
if($this->apply != ''){
$requestQuery .= '$apply='.rawurlencode($this->apply). '&';
}

// Append the filters to the query string.
if(count($this->filters) > 0){
$requestQuery .= '$filter=';
Expand All @@ -137,9 +153,10 @@ 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, $this->config->getSSLOptions());
$request->setMethod('GET');
$request->setHeaders($this->headers);

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

// Set the SAP B1 session data.
$request->setCookies($this->session);
Expand All @@ -158,6 +175,8 @@ private function doRequest(string $action = '', callable $callback = null){

$result = $response->getJson();



// If a callback is specified, then call it for each result in
// the collection.
if(null != $callback){
Expand Down
24 changes: 15 additions & 9 deletions SAPb1/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public function setCookies(array $cookies) : Request{
}

/**
* Add a header to request data.
* Sets the request headers.
*/
public function addHeader(string $header) : Void{
$this->headers [] = $header;
public function setHeaders(array $headers) : Request{
$this->headers = array_merge($this->headers, $headers);
return $this;
}

/**
Expand All @@ -60,24 +61,29 @@ public function getResponse() : Response{

$postdata = (null != $this->postParams) ? json_encode($this->postParams) : '';

$this->addHeader('Content-Type: application/json');
$this->addHeader("Content-Length: " . strlen($postdata));

$header = "Content-Type: application/json\r\n";
$header.= "Content-Length: " . strlen($postdata) . "\r\n";
if(count($this->cookies) > 0){
$header = "Cookie: ";
$header.= "Cookie: ";
foreach($this->cookies as $name => $value){
$header.= $name .'='. $value . ';';
}
$header.= "\r\n";
}

$this->addHeader($header);
if(count($this->headers)){
foreach($this->headers as $name => $value){
$header.= $name .':'. $value . "\r\n";
}
}

$options = array(
'http' => array(
'ignore_errors' => true,
'method' => $this->method,
'content' => $postdata,
'header' => implode("\r\n", $this->headers),
'header' => $header,
),
"ssl" => $this->sslOptions
);
Expand Down
4 changes: 2 additions & 2 deletions SAPb1/SAPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function getSession() : array{
/**
* Returns a new instance of SAPb1\Query, which allows for cross joins.
*/
public function query($join) : Query{
return new Query($this->config, $this->session, '$crossjoin('. str_replace(' ', '', $join) . ')');
public function query($join, $headers = []) : Query{
return new Query($this->config, $this->session, '$crossjoin('. str_replace(' ', '', $join) . ')', $headers);
}

/**
Expand Down
21 changes: 18 additions & 3 deletions SAPb1/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Service{
private $config;
private $session;
private $serviceName;
private $headers = [];
public $serviceViewName;

/**
* Initializes a new instance of Service.
Expand All @@ -21,16 +23,20 @@ public function __construct(Config $configOptions, array $session, string $servi
}

/**
* Creates an entity. Returns the newly created entity on success.
* Creates an entity.
* Throws SAPb1\SAPException if an error occurred.
*/
public function create(array $data) : object{
public function create(array $data){

$response = $this->doRequest('POST', $data);

if($response->getStatusCode() === 201){
return $response->getJson();
}

if($response->getStatusCode() === 204){
return true;
}

throw new SAPException($response);
}
Expand Down Expand Up @@ -96,7 +102,15 @@ 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);
return new Query($this->config, $this->session, $this->serviceName, $this->headers);
}

/**
* Specifies request headers.
*/
public function setHeaders($headers) : Service{
$this->headers = $headers;
return $this;
}

/**
Expand Down Expand Up @@ -157,6 +171,7 @@ 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);
$request->setHeaders($this->headers);
$request->setPost($postData);

return $request->getResponse();
Expand Down

0 comments on commit 7c65a22

Please sign in to comment.