Skip to content

Commit

Permalink
Refactorings for OData request/response processing (bug fixes)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Oct 20, 2019
1 parent 79f5cda commit f3fa485
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 130 deletions.
2 changes: 1 addition & 1 deletion examples/GraphConsole/ConsoleTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function downloadPhoto(GraphServiceClient $client,$targetFilePath){
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
//$options->StreamHandle = $fp;
try {
$content = $client->executeQueryDirect($options);
$response = $client->executeQueryDirect($options);

} catch (Exception $e) {
}
Expand Down
4 changes: 2 additions & 2 deletions examples/SharePoint/FilesExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePa

$fp = fopen($targetFilePath, 'w+');
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
$options = new RequestOptions($url);
$options->StreamHandle = $fp;
$ctx->executeQueryDirect($options);
fclose($fp);
Expand All @@ -199,7 +199,7 @@ function renameFolder($webUrl, $authCtx, $folderUrl,$folderNewName)
$request = new RequestOptions($url);
$ctx = new ClientContext($url,$authCtx);
$resp = $ctx->executeQueryDirect($request);
$data = json_decode($resp);
$data = json_decode($resp->getContent());

$itemPayload = array(
'__metadata' => array ('type' => $data->d->__metadata->type),
Expand Down
5 changes: 3 additions & 2 deletions src/OutlookServices/OutlookClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function __construct(IAuthenticationContext $authContext, $version = Offi
{
$this->version = $version;
$this->serviceRootUrl = $this->serviceRootUrl . $version . "/";
parent::__construct($this->serviceRootUrl, $authContext, new JsonFormat(ODataMetadataLevel::Verbose), $version);
$format = new JsonFormat(ODataMetadataLevel::Verbose);
$format->addProperty("type","#Microsoft.OutlookServices.*");
parent::__construct($this->serviceRootUrl, $authContext,$format, $version);
}


Expand Down Expand Up @@ -53,7 +55,6 @@ private function prepareOutlookServicesRequest(RequestOptions $request,ClientAct
}



/**
* @return User
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/ClientObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function toJson(ODataFormat $format)
$payload[$key] = $value;
}
if ($format instanceof JsonLightFormat && $format->MetadataLevel == ODataMetadataLevel::Verbose) {
$format->ensureMetadataAnnotation($this, $payload);
$format->ensureMetadataProperty($this, $payload);
}
return $payload;
}
Expand Down
30 changes: 11 additions & 19 deletions src/Runtime/ClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Office365\PHP\Client\Runtime\OData\ODataQueryOptions;
use Office365\PHP\Client\Runtime\Utilities\Guid;
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
use Office365\PHP\Client\Runtime\Utilities\Requests;


/**
Expand Down Expand Up @@ -69,10 +68,8 @@ public function __construct(ClientRuntimeContext $context)
*/
public function addQuery(ClientAction $query, $resultObject = null)
{
if (isset($resultObject)) {
$queryId = $query->getId();
$this->resultObjects[$queryId] = $resultObject;
}
$queryId = $query->getId();
$this->resultObjects[$queryId] = $resultObject;
$this->queries[] = $query;
}

Expand All @@ -98,6 +95,14 @@ public function afterExecuteQuery(callable $event)
public abstract function executeQuery();


/**
* @param RequestOptions $request
* @return ClientResponse
* @throws Exception
*/
public abstract function executeQueryDirect(RequestOptions $request);


/**
* @param RequestOptions $request
*/
Expand All @@ -110,7 +115,7 @@ protected abstract function setRequestHeaders(RequestOptions $request);
public abstract function processResponse($response);

/**
* Build Client Request
* Build Request
* @return RequestOptions
*/
protected abstract function buildRequest();
Expand All @@ -126,19 +131,6 @@ public function addQueryAndResultObject(ClientObject $clientObject, ODataQueryOp
}


/**
* @param RequestOptions $request
* @param array $responseInfo
* @return string
* @throws Exception
*/
public function executeQueryDirect(RequestOptions $request, &$responseInfo = array())
{
$this->context->authenticateRequest($request); //Auth mandatory headers
$this->setRequestHeaders($request); //set request headers
return Requests::execute($request,$responseInfo);
}

/**
* @return ClientAction[]
*/
Expand Down
14 changes: 11 additions & 3 deletions src/Runtime/ClientResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

abstract class ClientResponse
{
public function __construct($payload,$details)
public function __construct($content, $details)
{
$this->Payload = $payload;
$this->Content = $content;
$this->StatusCode = $details['HttpCode'];
}

public function getContent(){
return $this->Content;
}

/**
* @param IEntityType|ClientResult $object
* @param ODataFormat $format
Expand All @@ -22,8 +26,12 @@ abstract function map($object,$format);

abstract function validate();

/**
* @var integer
*/
protected $StatusCode;

protected $Payload;

protected $Content;

}
10 changes: 5 additions & 5 deletions src/Runtime/ClientRuntimeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Office365\PHP\Client\Runtime;

use Exception;
use Office365\PHP\Client\Runtime\Auth\IAuthenticationContext;
use Office365\PHP\Client\Runtime\OData\ODataRequest;
use Office365\PHP\Client\Runtime\OData\ODataFormat;
Expand Down Expand Up @@ -145,13 +146,12 @@ public function executeQuery()

/**
* @param RequestOptions $options
* @param array $responseInfo
* @return string
* @throws \Exception
* @return ClientResponse
* @throws Exception
*/
public function executeQueryDirect(RequestOptions $options,&$responseInfo = [])
public function executeQueryDirect(RequestOptions $options)
{
return $this->getPendingRequest()->executeQueryDirect($options, $responseInfo);
return $this->getPendingRequest()->executeQueryDirect($options);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/ClientValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function toJson(ODataFormat $format)
$payload[$key] = $val;
}
if ($format instanceof JsonLightFormat && $format->MetadataLevel == ODataMetadataLevel::Verbose) {
$format->ensureMetadataAnnotation($this, $payload);
$format->ensureMetadataProperty($this, $payload);
}
return $payload;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Runtime/OData/JsonFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public function __construct($metadataLevel)
parent::__construct($metadataLevel);
$this->Streaming = false;
$this->IEEE754Compatible = true;
$this->Annotations["collection"] = "value";
$this->Annotations["annotation"] = "@odata.*";
$this->addProperty("control","*@odata.*");
$this->addProperty("collection","value");
}


/**
* @return string
* @throws Exception
Expand Down
20 changes: 12 additions & 8 deletions src/Runtime/OData/JsonLightFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@
class JsonLightFormat extends ODataFormat
{


public function __construct($metadataLevel)
{
parent::__construct($metadataLevel);
if($metadataLevel === ODataMetadataLevel::Verbose){
$this->Annotations['__deferred'] = "deferred";
$this->Annotations['__metadata'] = "metadata";
$this->Annotations['collection'] = "results";
$this->Annotations['security'] = "d";
$this->addProperty('deferred',"__deferred");
$this->addProperty('metadata',"__metadata");
$this->addProperty('collection',"results");
$this->addProperty('security',"d");
}
}

public function setFunctionAnnotation($fnName){
$this->Annotations['function'] = $fnName;

public function getFunctionProperty(){
return $this->getProperty('function');
}

public function getSecurityProperty(){
return $this->getProperty('security');
}

/**
* @param $type IEntityType
* @param $payload array
*/
public function ensureMetadataAnnotation($type, &$payload)
public function ensureMetadataProperty($type, &$payload)
{
$typeName = $type->getTypeName();
if (substr($typeName, 0, 3) !== "SP.")
Expand Down
6 changes: 3 additions & 3 deletions src/Runtime/OData/MetadataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static function getMetadata(ClientRuntimeContext $ctx)
{
$metadataUrl = $ctx->getServiceRootUrl() . "/\$metadata";
$options = new RequestOptions($metadataUrl);
$content = $ctx->executeQueryDirect($options);
return $content;
$response = $ctx->executeQueryDirect($options);
return $response->getContent();
}

}
}
18 changes: 14 additions & 4 deletions src/Runtime/OData/ODataFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@

abstract class ODataFormat
{


public function __construct($metadataLevel)
{
$this->MetadataLevel = $metadataLevel;
$this->Annotations = array();
$this->properties = array();

}


public function addAnnotation($name, $value)
public function addProperty($name, $value)
{
$this->Annotations[$name] = $value;
$this->properties[$name] = $value;
}


public function getProperty($name){
if(isset($this->properties[$name]))
return $this->properties[$name];
return null;
}


Expand Down Expand Up @@ -55,6 +65,6 @@ public function isAtom()
/**
* @var array
*/
public $Annotations;
protected $properties;

}
35 changes: 27 additions & 8 deletions src/Runtime/OData/ODataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Office365\PHP\Client\Runtime\OData;

use Cassandra\Value;
use Exception;
use Office365\PHP\Client\Runtime\ClientAction;
use Office365\PHP\Client\Runtime\ClientRequestStatus;
Expand All @@ -13,6 +14,7 @@
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
use Office365\PHP\Client\Runtime\HttpMethod;
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
use Office365\PHP\Client\Runtime\Utilities\Requests;


/**
Expand All @@ -32,6 +34,22 @@ public function __construct(ClientRuntimeContext $context)
$this->currentQuery = null;
}



/**
* @param RequestOptions $request
* @return ClientResponse
* @throws Exception
*/
public function executeQueryDirect(RequestOptions $request)
{
$this->context->authenticateRequest($request); //Auth mandatory headers
$this->setRequestHeaders($request); //set request headers
$content = Requests::execute($request,$responseInfo);
return new ODataResponse($content,$responseInfo);
}


/**
* Submit query to OData service
* @throws Exception
Expand All @@ -48,18 +66,14 @@ public function executeQuery()
));
}

$responseInfo = array();
$payload = $this->executeQueryDirect($request, $responseInfo);
$response = new ODataResponse($payload,$responseInfo);
$response = $this->executeQueryDirect($request);
$response->validate();
if (is_callable($this->eventsList["AfterExecuteQuery"])) {
call_user_func_array($this->eventsList["AfterExecuteQuery"], array(
$response
));
}
if (!empty($payload)) {
$this->processResponse($response);
}
$this->processResponse($response);
$this->requestStatus = ClientRequestStatus::CompletedSuccess;
}
catch(Exception $e){
Expand All @@ -75,11 +89,16 @@ public function executeQuery()
*/
public function processResponse($response)
{
$queryId = $this->currentQuery->getId();
if (!array_key_exists($queryId, $this->resultObjects)) {
$payload = $response->getContent();
if (empty($payload)) {
return;
}

$queryId = $this->currentQuery->getId();
$resultObject = $this->resultObjects[$queryId];
if (is_null($resultObject)) {
return;
}
$response->map($resultObject, $this->getFormat());
}

Expand Down
Loading

0 comments on commit f3fa485

Please sign in to comment.