Repository navigation
Expand file tree
/
Copy pathAbstractWebservice.php
More file actions
191 lines (153 loc) · 4.79 KB
/
Copy pathAbstractWebservice.php
File metadata and controls
191 lines (153 loc) · 4.79 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
include_once 'Response.php';
/**
* Abstract webservice
*
* --------------------------- ASSUMPTIONS
* did not use separate objects for handling the different webservice calls
* as the methods will be executed in succession. It is therefore more
* efficient to reuse the object.
*
* I am assuming that the user credentials can change at any time according
* to the input file
*
* Within the 5 minute window, it is faster to reused the request token for
* multiple calls instead of requesting a new one each time.
*
* We are not using a http request library such as \Zend\Http\Client of a curl
* library
*
* API V2 returns message for GET method in the format: {"status": "ok", "mykey": "myvalue"}
*
* Processing a single large file may result in a token expiring before the file
* processing is complete. I disabled this feature as not sure this is important
* for this exercise :)
*
* @author AckimWilliams
* @see ./problem_description
*/
abstract class AbstractWebservice
{
const API_METHOD_GET = "GET";
const API_METHOD_DELETE = "DELETE";
const API_METHOD_PUT = "PUT";
const API_METHOD_LIST = "GET";
const RESPONSE_OK = "ok";
const RESPONSE_FAIL = "fail";
protected $_webserviceRoot = "http://website.connect.net";
// protected $_credentials = array( "username" => "", "password" => "" );
protected $_requestToken = "";
// private $_maxNumberRetryAttempts = 3;
private $_headers = array
(
'Accept: application/json',
'Content-Type: application/json',
);
// private $_attemptToCorrectStatuses = true;
private $_curlHandle = null;
public function __construct()
{
//setup curl handle
$this->_curlHandle = curl_init();
if ( !$this->_curlHandle )
throw new \Exception( "Could not initialize a curl handle." );
curl_setopt( $this->_curlHandle, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $this->_curlHandle, CURLOPT_RETURNTRANSFER, true );
}
public function __destruct()
{
curl_close ( $this->_curlHandle );
//save credentials or perform cleanup
}
/**
* Set the webservice root uri
*
* @param string $serviceRoot
*/
public function setWebserviceRoot( $serviceRoot )
{
$this->_webserviceRoot = $serviceRoot;
}
/**
* Execute http request
*
* @param string $url
* @param string $requestMethod
* @param array $data
* @throws \Exception
* @return Response http response object
*/
protected function execute( $url, $requestMethod, $data = array(), $headers = array() )
{
//prepare headers
$curlHeaders = $this->_headers;
if ( !empty( $curlHeaders ) )
$curlHeaders = $headers;
//setup curl options
curl_setopt( $this->_curlHandle, CURLOPT_URL, $url );
curl_setopt( $this->_curlHandle, CURLOPT_CUSTOMREQUEST, $requestMethod );
curl_setopt( $this->_curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);
$curlPostData = $data;
curl_setopt( $this->_curlHandle, CURLOPT_POSTFIELDS, $curlPostData );
// proceed with executing curl
$curlResponse = null;
$content = "";
$curlResponse = curl_exec( $this->_curlHandle ); //execute curl request
$content = curl_exec( $this->_curlHandle );
$curlResponse = curl_getinfo( $this->_curlHandle );
$content = json_decode( $content );
$returnResponse = new Response( $content, $curlResponse );
return $returnResponse;
}
// /**
// * Attempt to handle various common http request issues
// *
// * @param array $response Curl response
// * @return boolean
// */
// private function retryRequest( $response )
// {
// if ( $response == null || empty( $response['http_code'] ) )
// {
// //if nothing is returned of call attempt fails, retry
// return true;
// }
// switch( intval( $response['http_code'] ) )
// {
// //reauthenticate if token has expired,
// //not expected for api version 1
// case 401:
// //assuming that the credentials were valid before,
// //there is no reason to assume that it will become
// //invalid within an instance of this object.
// try
// {
// $this->authenticate();
// }
// catch( \Exception $ex )
// {
// return false; //unable to reauthenticate
// }
// return true;
// break;
// //@todo: can add additional callbacks, fixes for
// //other status code, but not important for this exercise
// //or throw an exception
// case 200:
// case 404:
// return false;
// break;
// }
// //catch 503
// return true;
// }
/**
* Authenticate user
*
* @param unknown $username
* @param unknown $password
*
* @throws \Exception
*/
abstract public function authenticate( $username, $password );
}