Skip to content

Commit f3fa485

Browse files
committed
Refactorings for OData request/response processing (bug fixes)
1 parent 79f5cda commit f3fa485

19 files changed

+188
-130
lines changed

examples/GraphConsole/ConsoleTasks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function downloadPhoto(GraphServiceClient $client,$targetFilePath){
3535
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
3636
//$options->StreamHandle = $fp;
3737
try {
38-
$content = $client->executeQueryDirect($options);
38+
$response = $client->executeQueryDirect($options);
3939

4040
} catch (Exception $e) {
4141
}

examples/SharePoint/FilesExamples.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePa
184184

185185
$fp = fopen($targetFilePath, 'w+');
186186
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
187-
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
187+
$options = new RequestOptions($url);
188188
$options->StreamHandle = $fp;
189189
$ctx->executeQueryDirect($options);
190190
fclose($fp);
@@ -199,7 +199,7 @@ function renameFolder($webUrl, $authCtx, $folderUrl,$folderNewName)
199199
$request = new RequestOptions($url);
200200
$ctx = new ClientContext($url,$authCtx);
201201
$resp = $ctx->executeQueryDirect($request);
202-
$data = json_decode($resp);
202+
$data = json_decode($resp->getContent());
203203

204204
$itemPayload = array(
205205
'__metadata' => array ('type' => $data->d->__metadata->type),

src/OutlookServices/OutlookClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public function __construct(IAuthenticationContext $authContext, $version = Offi
2525
{
2626
$this->version = $version;
2727
$this->serviceRootUrl = $this->serviceRootUrl . $version . "/";
28-
parent::__construct($this->serviceRootUrl, $authContext, new JsonFormat(ODataMetadataLevel::Verbose), $version);
28+
$format = new JsonFormat(ODataMetadataLevel::Verbose);
29+
$format->addProperty("type","#Microsoft.OutlookServices.*");
30+
parent::__construct($this->serviceRootUrl, $authContext,$format, $version);
2931
}
3032

3133

@@ -53,7 +55,6 @@ private function prepareOutlookServicesRequest(RequestOptions $request,ClientAct
5355
}
5456

5557

56-
5758
/**
5859
* @return User
5960
*/

src/Runtime/ClientObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function toJson(ODataFormat $format)
148148
$payload[$key] = $value;
149149
}
150150
if ($format instanceof JsonLightFormat && $format->MetadataLevel == ODataMetadataLevel::Verbose) {
151-
$format->ensureMetadataAnnotation($this, $payload);
151+
$format->ensureMetadataProperty($this, $payload);
152152
}
153153
return $payload;
154154
}

src/Runtime/ClientRequest.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Office365\PHP\Client\Runtime\OData\ODataQueryOptions;
99
use Office365\PHP\Client\Runtime\Utilities\Guid;
1010
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
11-
use Office365\PHP\Client\Runtime\Utilities\Requests;
1211

1312

1413
/**
@@ -69,10 +68,8 @@ public function __construct(ClientRuntimeContext $context)
6968
*/
7069
public function addQuery(ClientAction $query, $resultObject = null)
7170
{
72-
if (isset($resultObject)) {
73-
$queryId = $query->getId();
74-
$this->resultObjects[$queryId] = $resultObject;
75-
}
71+
$queryId = $query->getId();
72+
$this->resultObjects[$queryId] = $resultObject;
7673
$this->queries[] = $query;
7774
}
7875

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

10097

98+
/**
99+
* @param RequestOptions $request
100+
* @return ClientResponse
101+
* @throws Exception
102+
*/
103+
public abstract function executeQueryDirect(RequestOptions $request);
104+
105+
101106
/**
102107
* @param RequestOptions $request
103108
*/
@@ -110,7 +115,7 @@ protected abstract function setRequestHeaders(RequestOptions $request);
110115
public abstract function processResponse($response);
111116

112117
/**
113-
* Build Client Request
118+
* Build Request
114119
* @return RequestOptions
115120
*/
116121
protected abstract function buildRequest();
@@ -126,19 +131,6 @@ public function addQueryAndResultObject(ClientObject $clientObject, ODataQueryOp
126131
}
127132

128133

129-
/**
130-
* @param RequestOptions $request
131-
* @param array $responseInfo
132-
* @return string
133-
* @throws Exception
134-
*/
135-
public function executeQueryDirect(RequestOptions $request, &$responseInfo = array())
136-
{
137-
$this->context->authenticateRequest($request); //Auth mandatory headers
138-
$this->setRequestHeaders($request); //set request headers
139-
return Requests::execute($request,$responseInfo);
140-
}
141-
142134
/**
143135
* @return ClientAction[]
144136
*/

src/Runtime/ClientResponse.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
abstract class ClientResponse
1010
{
11-
public function __construct($payload,$details)
11+
public function __construct($content, $details)
1212
{
13-
$this->Payload = $payload;
13+
$this->Content = $content;
1414
$this->StatusCode = $details['HttpCode'];
1515
}
1616

17+
public function getContent(){
18+
return $this->Content;
19+
}
20+
1721
/**
1822
* @param IEntityType|ClientResult $object
1923
* @param ODataFormat $format
@@ -22,8 +26,12 @@ abstract function map($object,$format);
2226

2327
abstract function validate();
2428

29+
/**
30+
* @var integer
31+
*/
2532
protected $StatusCode;
2633

27-
protected $Payload;
34+
35+
protected $Content;
2836

2937
}

src/Runtime/ClientRuntimeContext.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Office365\PHP\Client\Runtime;
44

5+
use Exception;
56
use Office365\PHP\Client\Runtime\Auth\IAuthenticationContext;
67
use Office365\PHP\Client\Runtime\OData\ODataRequest;
78
use Office365\PHP\Client\Runtime\OData\ODataFormat;
@@ -145,13 +146,12 @@ public function executeQuery()
145146

146147
/**
147148
* @param RequestOptions $options
148-
* @param array $responseInfo
149-
* @return string
150-
* @throws \Exception
149+
* @return ClientResponse
150+
* @throws Exception
151151
*/
152-
public function executeQueryDirect(RequestOptions $options,&$responseInfo = [])
152+
public function executeQueryDirect(RequestOptions $options)
153153
{
154-
return $this->getPendingRequest()->executeQueryDirect($options, $responseInfo);
154+
return $this->getPendingRequest()->executeQueryDirect($options);
155155
}
156156

157157

src/Runtime/ClientValueObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function toJson(ODataFormat $format)
5454
$payload[$key] = $val;
5555
}
5656
if ($format instanceof JsonLightFormat && $format->MetadataLevel == ODataMetadataLevel::Verbose) {
57-
$format->ensureMetadataAnnotation($this, $payload);
57+
$format->ensureMetadataProperty($this, $payload);
5858
}
5959
return $payload;
6060
}

src/Runtime/OData/JsonFormat.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ public function __construct($metadataLevel)
1414
parent::__construct($metadataLevel);
1515
$this->Streaming = false;
1616
$this->IEEE754Compatible = true;
17-
$this->Annotations["collection"] = "value";
18-
$this->Annotations["annotation"] = "@odata.*";
17+
$this->addProperty("control","*@odata.*");
18+
$this->addProperty("collection","value");
1919
}
2020

21+
2122
/**
2223
* @return string
2324
* @throws Exception

src/Runtime/OData/JsonLightFormat.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,31 @@
1111
class JsonLightFormat extends ODataFormat
1212
{
1313

14-
1514
public function __construct($metadataLevel)
1615
{
1716
parent::__construct($metadataLevel);
1817
if($metadataLevel === ODataMetadataLevel::Verbose){
19-
$this->Annotations['__deferred'] = "deferred";
20-
$this->Annotations['__metadata'] = "metadata";
21-
$this->Annotations['collection'] = "results";
22-
$this->Annotations['security'] = "d";
18+
$this->addProperty('deferred',"__deferred");
19+
$this->addProperty('metadata',"__metadata");
20+
$this->addProperty('collection',"results");
21+
$this->addProperty('security',"d");
2322
}
2423
}
2524

26-
public function setFunctionAnnotation($fnName){
27-
$this->Annotations['function'] = $fnName;
25+
26+
public function getFunctionProperty(){
27+
return $this->getProperty('function');
28+
}
29+
30+
public function getSecurityProperty(){
31+
return $this->getProperty('security');
2832
}
2933

3034
/**
3135
* @param $type IEntityType
3236
* @param $payload array
3337
*/
34-
public function ensureMetadataAnnotation($type, &$payload)
38+
public function ensureMetadataProperty($type, &$payload)
3539
{
3640
$typeName = $type->getTypeName();
3741
if (substr($typeName, 0, 3) !== "SP.")

0 commit comments

Comments
 (0)