Skip to content

Commit

Permalink
runtime enhancements (odata serialization & remove state from client …
Browse files Browse the repository at this point in the history
…request)
  • Loading branch information
vgrem committed Jan 15, 2023
1 parent 39cbda5 commit 49756ff
Show file tree
Hide file tree
Showing 50 changed files with 941 additions and 213 deletions.
11 changes: 11 additions & 0 deletions src/Complex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Office365;

namespace Office365;

use Office365\Runtime\ClientValue;

class Complex extends ClientValue
{
}
4 changes: 2 additions & 2 deletions src/GraphServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(callable $acquireToken)
{
$this->acquireTokenFunc = $acquireToken;
$this->getPendingRequest()->beforeExecuteRequest(function (RequestOptions $request) {
$this->authenticateRequest($request);
$this->prepareRequest($request);
});
parent::__construct();
Expand All @@ -50,8 +51,7 @@ public function __construct(callable $acquireToken)
function getPendingRequest()
{
if(!$this->pendingRequest){
$format = new JsonFormat(ODataMetadataLevel::Verbose);
$this->pendingRequest = new ODataRequest($this,$format);
$this->pendingRequest = new ODataRequest(new JsonFormat(ODataMetadataLevel::Verbose));
}
return $this->pendingRequest;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Outlook/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Office365\Outlook;

use Office365\Runtime\ClientValue;
class EmailAddress extends ClientValue
use Office365\Complex;

class EmailAddress extends Complex
{
/**
* @param string $name
Expand Down
5 changes: 3 additions & 2 deletions src/Outlook/ItemBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Office365\Outlook;

use Office365\Runtime\ClientValue;
class ItemBody extends ClientValue
use Office365\Complex;

class ItemBody extends Complex
{

function __construct($contentType=null,$content=null)
Expand Down
5 changes: 3 additions & 2 deletions src/Outlook/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Office365\Outlook;

use Office365\Runtime\ClientValue;
class Location extends ClientValue
use Office365\Complex;

class Location extends Complex
{
/**
* @var string
Expand Down
5 changes: 3 additions & 2 deletions src/Outlook/PhysicalAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Office365\Outlook;

use Office365\Runtime\ClientValue;
class PhysicalAddress extends ClientValue
use Office365\Complex;

class PhysicalAddress extends Complex
{
/**
* @var string
Expand Down
5 changes: 3 additions & 2 deletions src/Outlook/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Office365\Outlook;

use Office365\Runtime\ClientValue;
class Recipient extends ClientValue
use Office365\Complex;

class Recipient extends Complex
{
function __construct(EmailAddress $emailAddress=null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/Actions/ClientAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class ClientAction
/**
* @param ClientRuntimeContext $context
* @param ClientObject|null $bindingType
* @param ClientObject|ClientValue|ClientResult $returnType
* @param ClientObject|ClientValue|ClientResult|null $returnType
*/
public function __construct($context, $bindingType,$returnType)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Runtime/Actions/InvokeMethodQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function __construct($bindingType, $methodName=null, $methodParameters=nu
*/
public function getPath(){
if ($this->IsStatic) {
$request = $this->getContext()->getPendingRequest();
$entityTypeName = $request->normalizeTypeName($this->BindingType);
$entityTypeName = (string)$this->BindingType->getServerTypeInfo();
$staticName = implode(".",[$entityTypeName, $this->MethodName]);
return new ServiceOperationPath($staticName,$this->MethodParameters);
}
Expand Down
125 changes: 24 additions & 101 deletions src/Runtime/ClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,131 +30,63 @@ abstract class ClientRequest
*/
protected $afterExecute;

/**
* @var ClientRuntimeContext
*/
protected $context;


/**
* @var ClientAction[]
*/
protected $queries = array();


/**
* @var ClientAction
*/
protected $currentQuery = null;

/** @var Guid */
/** @var Guid */
protected $requestId;

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


/**
* ClientRequest constructor.
* @param ClientRuntimeContext $context
*/
public function __construct(ClientRuntimeContext $context)
public function __construct()
{
$this->context = $context;
$this->beforeExecute = new EventHandler();
$this->afterExecute = new EventHandler();
$this->requestId = Guid::newGuid();
$this->requestStatus = ClientRequestStatus::Active;
}


/**
* @return ClientAction|null
*/
protected function getNextQuery()
{
$qry = array_shift($this->queries);
$this->currentQuery = $qry;
return $qry;
}


/**
* @return ClientAction
*/
public function getCurrentQuery(){
return $this->currentQuery;
}


/**
* Add query into request queue
* @param ClientAction $query
* @param bool $executeFirst
*/
public function addQuery(ClientAction $query, $executeFirst=false)
{
if($executeFirst)
array_unshift($this->queries , $query);
else
$this->queries[] = $query;
}

/**
* @param ClientAction $query
* @param ClientObject|ClientResult $resultObject
*/
public function addQueryAndResultObject(ClientAction $query, $resultObject = null)
{
$query->ReturnType = $resultObject;
$this->addQuery($query,false);
}


/**
* @param callable $event
*/
public function beforeExecuteRequest(callable $event)
{
$this->beforeExecute->addEvent($event,false);
$this->beforeExecute->addEvent($event, false);
}

/**
* @param callable $event
*/
public function beforeExecuteRequestOnce(callable $event)
{
$this->beforeExecute->addEvent($event,true);
$this->beforeExecute->addEvent($event, true);
}

/**
* @param callable $event
* @param bool $once
*/
public function afterExecuteRequest(callable $event,$once=true)
public function afterExecuteRequest(callable $event, $once = true)
{
$this->afterExecute->addEvent($event,$once);
$this->afterExecute->addEvent($event, $once);
}

/**
* Submit a query
* @throws Exception
*/
public function executeQuery()
public function executeQuery($query)
{
while ($this->getNextQuery() !== null) {
try{
$request = $this->buildRequest();
$this->beforeExecute->triggerEvent(array($request));
$response = $this->executeQueryDirect($request);
$this->processResponse($response);
$this->afterExecute->triggerEvent(array($response));
$this->requestStatus = ClientRequestStatus::CompletedSuccess;
}
catch(Exception $e){
$this->requestStatus = ClientRequestStatus::CompletedException;
throw $e;
}
try {
$request = $this->buildRequest($query);
$response = $this->executeQueryDirect($request);
$this->processResponse($response, $query);
$this->afterExecute->triggerEvent(array($response));
$this->requestStatus = ClientRequestStatus::CompletedSuccess;
} catch (Exception $e) {
$this->requestStatus = ClientRequestStatus::CompletedException;
throw $e;
}
}

Expand All @@ -165,7 +97,7 @@ public function executeQuery()
*/
public function executeQueryDirect(RequestOptions $request)
{
$this->context->authenticateRequest($request);
$this->beforeExecute->triggerEvent(array($request));
$response = Requests::execute($request);
$this->validate($response);
return $response;
Expand All @@ -174,31 +106,22 @@ public function executeQueryDirect(RequestOptions $request)

/**
* @param Response $response
* @param ClientAction $query
*/
public abstract function processResponse($response);
public abstract function processResponse($response, $query);

/**
* Build Request
* @param ClientAction $query
* @return RequestOptions
*/
abstract public function buildRequest();


/**
* @return ClientAction[]
*/
public function getActions(){
return $this->queries;
}

public function clearActions(){
$this->queries = array();
}
abstract public function buildRequest($query);

/**
* @return int
*/
public function getRequestStatus(){
public function getRequestStatus()
{
return $this->requestStatus;
}

Expand All @@ -211,7 +134,7 @@ public function getRequestStatus(){
public function validate($response)
{
if ($response->getStatusCode() >= 400) {
throw new RequestException($response->getContent(),$response->getStatusCode());
throw new RequestException($response->getContent(), $response->getStatusCode());
}
return true;
}
Expand Down
Loading

0 comments on commit 49756ff

Please sign in to comment.