33
33
*/
34
34
class mapping_session extends mapping_base {
35
35
36
-
37
36
/** @var string Error string from authentication request. */
38
37
private static $ authenticationerror = '' ;
39
38
40
- /**
41
- * Base constructor.
42
- *
43
- * The $extra parameter has been added here for future-proofing.
44
- * This allows named parameters to be used and allows classes extending to
45
- * make use of parameters in newer versions even if they don't exist in older versions.
46
- *
47
- * @param string $action The action of this method.
48
- * @param string $url The base url of this backpack.
49
- * @param mixed $postparams List of parameters for this method.
50
- * @param bool $multiple This method returns an array of responses.
51
- * @param string $method get or post methods.
52
- * @param bool $isjson json decode the response.
53
- * @param bool $authrequired Authentication is required for this request.
54
- * @param int $backpackapiversion OpenBadges version.
55
- * @param string $requestexporter Name of a class to export parameters for this method.
56
- * @param string $responseexporter Name of a class to export response for this method.
57
- * @param mixed ...$extra Extra arguments to allow for future versions to add more options
58
- */
59
- public function __construct (
60
- /** @var string $action The action of this method. */
61
- protected string $ action ,
62
- /** @var string $url The base URL of this backpack. */
63
- protected string $ url ,
64
- /** @var mixed $postparams List of parameters for this method. */
65
- protected mixed $ postparams ,
66
- /** @var bool $multiple This method returns an array of responses. */
67
- protected bool $ multiple ,
68
- /** @var string $method GET or POST method. */
69
- protected string $ method ,
70
- /** @var bool $json JSON decode the response. */
71
- protected bool $ isjson ,
72
- /** @var bool $authrequired Authentication is required for this request. */
73
- protected bool $ authrequired ,
74
- /** @var int OpenBadges version. */
75
- protected $ backpackapiversion ,
76
- protected string $ requestexporter ,
77
- protected string $ responseexporter ,
78
- mixed ...$ extra ,
79
- ) {
80
-
81
- }
82
-
83
39
/**
84
40
* Get the unique key for the token.
85
41
*
@@ -108,57 +64,6 @@ public static function get_authentication_error(): string {
108
64
return self ::$ authenticationerror ;
109
65
}
110
66
111
- /**
112
- * Parse the post parameters and insert replacements.
113
- *
114
- * @param string $email The api username.
115
- * @param string $password The api password.
116
- * @param string $param The parameter.
117
- * @return mixed
118
- */
119
- private function get_post_params ($ email , $ password , $ param ) {
120
- global $ PAGE ;
121
-
122
- if ($ this ->method == 'get ' ) {
123
- return '' ;
124
- }
125
-
126
- $ request = $ this ->postparams ;
127
- if ($ request === '[PARAM] ' ) {
128
- $ value = $ param ;
129
- foreach ($ value as $ key => $ keyvalue ) {
130
- if (gettype ($ value [$ key ]) == 'array ' ) {
131
- $ newkey = 'related_ ' . $ key ;
132
- $ value [$ newkey ] = $ value [$ key ];
133
- unset($ value [$ key ]);
134
- }
135
- }
136
- } else if (is_array ($ request )) {
137
- foreach ($ request as $ key => $ value ) {
138
- if ($ value == '[EMAIL] ' ) {
139
- $ value = $ email ;
140
- $ request [$ key ] = $ value ;
141
- } else if ($ value == '[PASSWORD] ' ) {
142
- $ value = $ password ;
143
- $ request [$ key ] = $ value ;
144
- } else if ($ value == '[PARAM] ' ) {
145
- $ request [$ key ] = is_array ($ param ) ? $ param [0 ] : $ param ;
146
- }
147
- }
148
- }
149
- $ context = context_system::instance ();
150
- $ exporter = $ this ->requestexporter ;
151
- $ output = $ PAGE ->get_renderer ('core ' , 'badges ' );
152
- if (!empty ($ exporter )) {
153
- $ exporterinstance = new $ exporter ($ value , ['context ' => $ context ]);
154
- $ request = $ exporterinstance ->export ($ output );
155
- }
156
- if ($ this ->isjson ) {
157
- return json_encode ($ request );
158
- }
159
- return $ request ;
160
- }
161
-
162
67
/**
163
68
* Get the user id from a previous user request.
164
69
*
@@ -209,28 +114,19 @@ private function oauth_token_response($response, $backpackid) {
209
114
/**
210
115
* Make an API request and parse the response.
211
116
*
212
- * @param string $apiurl Raw request URL
117
+ * @param string $url Request URL
118
+ * @param array|string|null $postdata Data to post
213
119
* @param mixed ...$extra Extra arguments to allow for specific mappings to add more options
214
120
* @return mixed TODO: Replace mixed with more specific type.
215
121
*/
216
122
public function request (
217
- string $ apiurl ,
123
+ string $ url ,
124
+ $ postdata = null ,
218
125
mixed ...$ extra ,
219
126
) {
220
127
global $ SESSION , $ PAGE ;
221
128
222
- // Extract parameters from $extra
223
- $ urlparam1 = $ extra [0 ] ?? '' ;
224
- $ urlparam2 = $ extra [1 ] ?? '' ;
225
- $ email = $ extra [2 ] ?? '' ;
226
- $ password = $ extra [3 ] ?? '' ;
227
- $ postparam = $ extra [4 ] ?? '' ;
228
- $ backpackid = $ extra [5 ] ?? '' ;
229
-
230
129
$ curl = new curl ();
231
-
232
- $ url = $ this ->get_url ($ apiurl , $ urlparam1 , $ urlparam2 );
233
-
234
130
if ($ this ->authrequired ) {
235
131
$ accesskey = $ this ->get_token_key (BADGE_ACCESS_TOKEN );
236
132
if (isset ($ SESSION ->$ accesskey )) {
@@ -244,49 +140,18 @@ public function request(
244
140
$ curl ->setHeader (array ('Accept: application/json ' , 'Expect: ' ));
245
141
$ options = $ this ->get_curl_options ();
246
142
247
- $ post = $ this ->get_post_params ($ email , $ password , $ postparam );
248
-
249
143
if ($ this ->method == 'get ' ) {
250
- $ response = $ curl ->get ($ url , $ post , $ options );
144
+ $ response = $ curl ->get ($ url , $ postdata , $ options );
251
145
} else if ($ this ->method == 'post ' ) {
252
- $ response = $ curl ->post ($ url , $ post , $ options );
146
+ $ response = $ curl ->post ($ url , $ postdata , $ options );
253
147
} else if ($ this ->method == 'put ' ) {
254
- $ response = $ curl ->put ($ url , $ post , $ options );
148
+ $ response = $ curl ->put ($ url , $ postdata , $ options );
255
149
}
256
150
$ response = json_decode ($ response );
257
151
if (isset ($ response ->result )) {
258
152
$ response = $ response ->result ;
259
153
}
260
- $ context = context_system::instance ();
261
- $ exporter = $ this ->responseexporter ;
262
- if (class_exists ($ exporter )) {
263
- $ output = $ PAGE ->get_renderer ('core ' , 'badges ' );
264
- if (!$ this ->multiple ) {
265
- if (count ($ response )) {
266
- $ response = $ response [0 ];
267
- }
268
- if (empty ($ response )) {
269
- return null ;
270
- }
271
- $ apidata = $ exporter ::map_external_data ($ response , $ this ->backpackapiversion );
272
- $ exporterinstance = new $ exporter ($ apidata , ['context ' => $ context ]);
273
- $ data = $ exporterinstance ->export ($ output );
274
- return $ data ;
275
- } else {
276
- $ multiple = [];
277
- if (empty ($ response )) {
278
- return $ multiple ;
279
- }
280
- foreach ($ response as $ data ) {
281
- $ apidata = $ exporter ::map_external_data ($ data , $ this ->backpackapiversion );
282
- $ exporterinstance = new $ exporter ($ apidata , ['context ' => $ context ]);
283
- $ multiple [] = $ exporterinstance ->export ($ output );
284
- }
285
- return $ multiple ;
286
- }
287
- } else if (method_exists ($ this , $ exporter )) {
288
- return $ this ->$ exporter ($ response , $ backpackid );
289
- }
154
+
290
155
return $ response ;
291
156
}
292
157
}
0 commit comments