-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestServiceHandler.php
More file actions
169 lines (137 loc) · 5.08 KB
/
RestServiceHandler.php
File metadata and controls
169 lines (137 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require_once('../v3-php-sdk-2.0.5/config.php');
require_once('../v3-php-sdk-2.0.5/Core/RestCalls/RestHandler.php');
require_once('../v3-php-sdk-2.0.5/Core/RestCalls/FaultHandler.php');
require_once('../v3-php-sdk-2.0.5/Utility/IntuitErrorHandler.php');
/**
* RestServiceHandler contains the logic for preparing the REST request, calls REST services and returns the response.
*/
class RestServiceHandler extends RestHandler
{
/**
* The context
* @var ServiceContext
*/
private $serviceContext;
/**
* Initializes a new instance of the SyncRestHandler class.
*
* @param ServiceContext $context The context
*/
public function RestServiceHandler($context)
{
parent::__construct($context);
$this->context = $context;
return $this;
}
public function GetReportsResponse($requestParameters, $requestBody, $oauthRequestUri)
{
$this->context->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Called PrepareRequest method");
// This step is required since the configuration settings might have been changed.
$this->RequestCompressor = CoreHelper::GetCompressor($this->context, true);
$this->ResponseCompressor = CoreHelper::GetCompressor($this->context, false);
$this->RequestSerializer = CoreHelper::GetSerializer($this->context, true);
$this->ResponseSerializer = CoreHelper::GetSerializer($this->context, false);
// Determine dest URI
$requestUri='';
if ($requestParameters->ApiName)
{
// Example: "https://appcenter.intuit.com/api/v1/Account/AppMenu"
$requestUri = $this->context->baseserviceURL . $requestParameters->ApiName;
}
else if ($oauthRequestUri)
{
// Prepare the request Uri from base Uri and resource Uri.
$requestUri = $oauthRequestUri;
}
else if ($requestParameters->ResourceUri)
{
$requestUri = $this->context->baseserviceURL . $requestParameters->ResourceUri;
}
else {
}
$oauth = new OAuth($this->context->requestValidator->ConsumerKey, $this->context->requestValidator->ConsumerSecret);
$oauth->setToken($this->context->requestValidator->AccessToken, $this->context->requestValidator->AccessTokenSecret);
$oauth->enableDebug();
$oauth->setAuthType(OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->disableSSLChecks();
$httpHeaders = array();
if ('QBO'==$this->context->serviceType ||
'QBD'==$this->context->serviceType)
{
// IDS call
$httpHeaders = array(
'accept' => 'application/json');
// Log Request Body to a file
$this->RequestLogging->LogPlatformRequests($requestBody, $requestUri, $httpHeaders, TRUE);
if ($this->ResponseCompressor)
$this->ResponseCompressor->PrepareDecompress($httpHeaders);
}
else
{
// IPP call
$httpHeaders = array('accept' => 'application/json');
}
try
{
$OauthMethod = OAUTH_HTTP_METHOD_GET;
$oauth->fetch($requestUri, $requestBody, $OauthMethod, $httpHeaders);
}
catch ( OAuthException $e )
{
//echo "ERROR:\n";
//print_r($e->getMessage()) . "\n";
list($response_code, $response_xml, $response_headers) = $this->GetOAuthResponseHeaders($oauth);
$this->RequestLogging->LogPlatformRequests($response_xml, $requestUri, $response_headers, FALSE);
return FALSE;
}
list($response_code, $response_xml, $response_headers) = $this->GetOAuthResponseHeaders($oauth);
// Log Request Body to a file
$this->RequestLogging->LogPlatformRequests($response_xml, $requestUri, $response_headers, FALSE);
return array($response_code,$response_xml);
}
/**
* Returns the response headers and response code from a called OAuth object
*
* @param OAuth $oauth A called OAuth object
* @return array elements are 0: HTTP response code; 1: response content, 2: HTTP response headers
*/
private function GetOAuthResponseHeaders($oauth)
{
$response_code = NULL;
$response_xml = NULL;
$response_headers = array();
try {
$response_xml = $oauth->getLastResponse();
$response_headers = array();
$response_headers_raw = $oauth->getLastResponseHeaders();
$response_headers_rows = explode("\r\n",$response_headers_raw);
foreach($response_headers_rows as $header) {
$keyval = explode(":",$header);
if (2==count($keyval))
$response_headers[$keyval[0]] = trim($keyval[1]);
if (FALSE !== strpos($header, 'HTTP'))
list(,$response_code,) = explode(' ', $header);
}
// Decompress, if applicable
if ('QBO'==$this->context->serviceType ||
'QBD'==$this->context->serviceType)
{
// Even if accept-encoding is set to deflate, server never (as far as we know) actually chooses
// to respond with Content-Encoding: deflate. Thus, the inspection of 'Content-Encoding' response
// header rather than assuming that server will respond with encoding specified by accept-encoding
if ($this->ResponseCompressor &&
$response_headers &&
array_key_exists('Content-Encoding', $response_headers))
{
$response_xml = $this->ResponseCompressor->Decompress($response_xml, $response_headers);
}
}
}
catch(Exception $e)
{
}
return array($response_code, $response_xml, $response_headers);
}
}
?>