Skip to content

Commit b4348df

Browse files
committed
fixup! MDL-83906 core_badges: Organise backpack API classes
1 parent 220e322 commit b4348df

File tree

7 files changed

+419
-536
lines changed

7 files changed

+419
-536
lines changed

badges/classes/local/backpack/mapping/mapping_base.php

+4-42
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,16 @@ abstract class mapping_base {
3434
*
3535
* @param string $action The action of this method.
3636
* @param string $url The base url of this backpack.
37-
* @param mixed $postparams List of parameters for this method.
38-
* @param bool $multiple This method returns an array of responses.
3937
* @param string $method get or post methods.
4038
* @param bool $isjson json decode the response.
4139
* @param bool $authrequired Authentication is required for this request.
4240
* @param int $backpackapiversion OpenBadges version.
43-
* @param mixed ...$extra Extra arguments to allow for future versions to add more options
4441
*/
4542
public function __construct(
4643
/** @var string $action The action of this method. */
4744
protected string $action,
4845
/** @var string $url The base URL of this backpack. */
49-
protected string $url,
50-
/** @var mixed $postparams List of parameters for this method. */
51-
protected mixed $postparams,
52-
/** @var bool $multiple This method returns an array of responses. */
53-
protected bool $multiple,
46+
public string $url,
5447
/** @var string $method GET or POST method. */
5548
protected string $method,
5649
/** @var bool $json JSON decode the response. */
@@ -59,7 +52,6 @@ public function __construct(
5952
protected bool $authrequired,
6053
/** @var int OpenBadges version. */
6154
protected $backpackapiversion,
62-
mixed ...$extra,
6355
) {
6456

6557
}
@@ -68,46 +60,16 @@ public function __construct(
6860
* Make an API request and parse the response.
6961
*
7062
* @param string $apiurl Raw request URL
63+
* @param array|string|null $postdata Data to post
7164
* @param mixed ...$extra Extra arguments to allow for specific mappings to add more options
7265
* @return mixed
7366
*/
74-
abstract public function request(
67+
abstract public function curl_request(
7568
string $apiurl,
69+
$postdata = null,
7670
mixed ...$extra,
7771
);
7872

79-
/**
80-
* Does the action match this mapping?
81-
*
82-
* @param string $action The action.
83-
* @return bool
84-
*/
85-
public function is_match($action): bool {
86-
return $this->action == $action;
87-
}
88-
89-
/**
90-
* Parse the method URL and insert parameters.
91-
*
92-
* @param string $apiurl The raw API URL.
93-
* @param string ...$params Optional parameters.
94-
* @return string
95-
*/
96-
protected function get_url(string $apiurl, string ...$params): string {
97-
$urlscheme = parse_url($apiurl, PHP_URL_SCHEME);
98-
$urlhost = parse_url($apiurl, PHP_URL_HOST);
99-
100-
$url = $this->url;
101-
$url = str_replace('[SCHEME]', $urlscheme, $url);
102-
$url = str_replace('[HOST]', $urlhost, $url);
103-
$url = str_replace('[URL]', $apiurl, $url);
104-
foreach ($params as $index => $param) {
105-
$url = str_replace("[PARAM" . ($index + 1) . "]", $param, $url);
106-
}
107-
108-
return $url;
109-
}
110-
11173
/**
11274
* Standard options used for all curl requests.
11375
*

badges/classes/local/backpack/mapping/mapping_session.php

+11-146
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,16 @@
3333
*/
3434
class mapping_session extends mapping_base {
3535

36-
3736
/** @var string Error string from authentication request. */
3837
private static $authenticationerror = '';
3938

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-
8339
/**
8440
* Get the unique key for the token.
8541
*
8642
* @param string $type The type of token.
8743
* @return string
8844
*/
89-
private function get_token_key($type): string {
45+
public static function get_token_key($type): string {
9046
return 'badges_backpack_' . $type . '_token';
9147
}
9248

@@ -108,57 +64,6 @@ public static function get_authentication_error(): string {
10864
return self::$authenticationerror;
10965
}
11066

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-
16267
/**
16368
* Get the user id from a previous user request.
16469
*
@@ -177,7 +82,7 @@ private function get_auth_user_id(): int {
17782
* @param integer $backpackid The id of the backpack.
17883
* @return mixed
17984
*/
180-
private function oauth_token_response($response, $backpackid) {
85+
public function oauth_token_response($response, $backpackid) {
18186
global $SESSION;
18287

18388
if (isset($response->access_token) && isset($response->refresh_token)) {
@@ -209,28 +114,19 @@ private function oauth_token_response($response, $backpackid) {
209114
/**
210115
* Make an API request and parse the response.
211116
*
212-
* @param string $apiurl Raw request URL
117+
* @param string $url Request URL
118+
* @param array|string|null $postdata Data to post
213119
* @param mixed ...$extra Extra arguments to allow for specific mappings to add more options
214120
* @return mixed TODO: Replace mixed with more specific type.
215121
*/
216-
public function request(
217-
string $apiurl,
122+
public function curl_request(
123+
string $url,
124+
$postdata = null,
218125
mixed ...$extra,
219126
) {
220127
global $SESSION, $PAGE;
221128

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-
230129
$curl = new curl();
231-
232-
$url = $this->get_url($apiurl, $urlparam1, $urlparam2);
233-
234130
if ($this->authrequired) {
235131
$accesskey = $this->get_token_key(BADGE_ACCESS_TOKEN);
236132
if (isset($SESSION->$accesskey)) {
@@ -244,49 +140,18 @@ public function request(
244140
$curl->setHeader(array('Accept: application/json', 'Expect:'));
245141
$options = $this->get_curl_options();
246142

247-
$post = $this->get_post_params($email, $password, $postparam);
248-
249143
if ($this->method == 'get') {
250-
$response = $curl->get($url, $post, $options);
144+
$response = $curl->get($url, $postdata, $options);
251145
} else if ($this->method == 'post') {
252-
$response = $curl->post($url, $post, $options);
146+
$response = $curl->post($url, $postdata, $options);
253147
} else if ($this->method == 'put') {
254-
$response = $curl->put($url, $post, $options);
148+
$response = $curl->put($url, $postdata, $options);
255149
}
256150
$response = json_decode($response);
257151
if (isset($response->result)) {
258152
$response = $response->result;
259153
}
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+
290155
return $response;
291156
}
292157
}

badges/classes/local/backpack/mapping/mapping_token.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,37 @@ class mapping_token extends mapping_base {
3535
/**
3636
* Make an API request and parse the response.
3737
*
38-
* @param string $apiurl Raw request URL
38+
* @param string $apiurl Request URL
39+
* @param array|string|null $postdata Data to post
3940
* @param mixed ...$extra Extra arguments to allow for specific mappings to add more options
4041
* @return mixed TODO: Replace mixed with more specific type.
4142
*/
42-
public function request(
43-
string $apiurl,
43+
public function curl_request(
44+
string $url,
45+
$postdata = null,
4446
mixed ...$extra,
4547
) {
4648
// Extract parameters from $extra
4749
$tokenkey = $extra[0] ?? '';
48-
$post = $extra[1] ?? [];
4950

5051
$curl = new curl();
51-
$url = $this->get_url($apiurl);
5252
if ($tokenkey) {
5353
$curl->setHeader('Authorization: Bearer ' . $tokenkey);
5454
}
5555

5656
if ($this->isjson) {
5757
$curl->setHeader(array('Content-type: application/json'));
5858
if ($this->method == 'post') {
59-
$post = json_encode($post);
59+
$postdata = json_encode($postdata);
6060
}
6161
}
6262

6363
$curl->setHeader(array('Accept: application/json', 'Expect:'));
6464
$options = $this->get_curl_options();
6565
if ($this->method == 'get') {
66-
$response = $curl->get($url, $post, $options);
66+
$response = $curl->get($url, $postdata, $options);
6767
} else if ($this->method == 'post') {
68-
$response = $curl->post($url, $post, $options);
68+
$response = $curl->post($url, $postdata, $options);
6969
}
7070
$response = json_decode($response);
7171
if (isset($response->result)) {

0 commit comments

Comments
 (0)