forked from anshulmeena/EtapestryAPI
/
EtapestryAPI.php
226 lines (190 loc) · 5.06 KB
/
EtapestryAPI.php
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* eTapestry PHP API, include this file in your project.
*
* @package EtapestryAPI
* @author Danny Bouman at Howard County Library System <dannybb@gmail.com>
* @author Anshul Meena at Howard County Library System <anshulmeena1@gmail.com>
* @author Nathaniel Schweinberg at Fight the Current
* <nathaniel@fightthecurrent.org>
*/
require dirname(__FILE__) . '/lib/nusoap.php';
require dirname(__FILE__) . '/lib/utils.php';
require dirname(__FILE__) . '/lib/EtapestryAccount.php';
require dirname(__FILE__) . '/lib/EtapestryJournal.php';
// Log
require dirname(__FILE__) . '/lib/class-error-logger.php';
$GLOBALS['log'] = new Error_Logger();
/**
* Exception class for eTapestry PHP API.
*
* @package EtapestryAPI
*/
class EtapestryAPIException extends Exception
{
public function log_error($message){
$log = $GLOBALS['log'];
$log->add($message);
}
}
class EtapestryAPI
{
/**
* NuSOAP client object
*/
public $nsc;
public $test;
/**
* Number of times to retry if connection to eTapestry fails
*/
private $retryLimit = 5;
/**
* EtapestryAPI login details and intial endpoint.
*/
private $loginId;
private $password;
private $endpoint;
private $error;
/**
* Constructor
*
* @param string $endpoint URL of eTapestry Service
*/
public function __construct($endpoint = false)
{
$this->endpoint = ($endpoint ? $endpoint : (defined('ETAPESTRYAPI_ENDPOINT') ? ETAPESTRYAPI_ENDPOINT : "https://sna.etapestry.com/v2messaging/service?WSDL"));
$this->createNuSOAPClient();
$this->login();
}
/**
* Instantiate NuSOAP client at specified endpoint
*
* @param string $endpoint
* @return boolean true if no fault or error occurred
*/
public function createNuSOAPClient ($retry = 0)
{
// Instantiate nusoap_client
$this->nsc = new nusoap_client($this->endpoint, true);
// Did an error occur?
if ($this->hasFaultOrError($this->nsc)) {
return false;
}
return true;
}
/**
* Instantiate a nusoap_client instance and call login method
*
* @param string $loginId Login ID
* @param string $password Password
*/
public function login($loginId = false, $password = false)
{
$this->loginId = ($loginId ? $loginId : (defined('ETAPESTRYAPI_LOGIN_ID') ? ETAPESTRYAPI_LOGIN_ID : ""));
$this->password = ($password ? $password : (defined('ETAPESTRYAPI_PASSWORD') ? ETAPESTRYAPI_PASSWORD : ""));
// Invoke login method
$response = $this->nusoapCall("login", array($this->loginId, $this->password));
// Determine if the login method returned a value...this will occur
// when the database you are trying to access is located at a different
// environment that can only be accessed using the provided endpoint
if ($response != "")
{
$this->endPoint = $response;
$this->createNuSOAPClient();
// Invoke login method
$response = $this->nusoapCall("login", array($loginId, $password));
}
return $response;
}
/**
* Stops an eTapestry API session by calling the logout
* method given a nusoap_client instance.
*/
public function logout()
{
// Invoke logout method
$response = $this->nusoapCall("logout");
return $response;
}
/**
* Method to determine if a NuSoap fault or error occurred.
* If so, output any relevant info and stop the code from executing.
*
* @param object $nsc NuSoap client
*/
public function hasFaultOrError($nsc, $showErrors = false)
{
//$this->error = NULL;
try
{
if ($nsc->fault || $nsc->getError())
{
if (!$nsc->fault)
{
$message = $nsc->getError();
}
else
{
$code = $nsc->faultcode;
$message = $nsc->faultstring;
}
throw new EtapestryAPIException($message);
}
}
catch (EtapestryAPIException $e)
{
$this->error = $e->getMessage();
$e->log_error($e->getMessage());
if ($showErrors) {
echo $this->error."\n";
}
return true;
}
return false;
}
/**
* Performs nusoap_client call and checks for faults/errors
*
* @param string $operation
* @param array $params
*/
public function nusoapCall ($operation, $params = array())
{
$response = $this->nsc->call($operation,$params);
if ($this->hasFaultOrError($this->nsc, FALSE) && stristr($this->error, "wsdl error")) {
// Retry a call if it has failed to reach api service
$response = $this->retryCall($operation, $params);
if ($response === FALSE) {
echo $this->error;
return false;
}
}
else if ($this->hasFaultOrError($this->nsc)) {
return false;
}
return $response;
}
/**
* Retry a call until retry limit is reached
* limit the error display to 1 from total retry.
*
* @param string $operation
* @param array $params
*/
private function retryCall ($operation, $params = array())
{
$response = false;
for ($try=0; $try<$this->retryLimit; $try++) {
sleep(5);
$this->createNuSOAPClient();
$response = $this->nsc->call($operation,$params);
if (!$this->hasFaultOrError($this->nsc, FALSE)) {
break;
}
}
return $response;
}
public function return_error (){
return $this->error;
}
}