MCPcopy Create free account
hub / github.com/Rello/analytics / readData

Method readData

lib/Datasource/ExternalJson.php:79–122  ·  view source on GitHub ↗

* Read the Data * @param $option * @return array available options of the data source */

($option)

Source from the content-addressed store, hash-verified

77 * @return array available options of the data source
78 */
79 public function readData($option): array
80 {
81 $cache = $this->getCacheMetadata($option);
82 if ($cache['notModified'] === true) {
83 return [
84 'header' => [],
85 'dimensions' => [],
86 'data' => [],
87 'rawdata' => null,
88 'error' => 0,
89 'cache' => $cache,
90 ];
91 }
92
93 $url = htmlspecialchars_decode($option['url'], ENT_NOQUOTES);
94 $auth = (string)($option['auth'] ?? '');
95 $method = ($option['method'] ?? 'GET') === 'POST' ? 'POST' : 'GET';
96 $contentType = !empty($option['content-type']) ? (string)$option['content-type'] : 'application/json';
97 $data = array();
98 $http_code = '';
99 $headers = $this->parseHeaders((string)($option['customHeaders'] ?? ''));
100 $headers['OCS-APIRequest'] = 'true';
101 $headers['Content-Type'] = $contentType;
102 $response = $this->httpClient->request(
103 $url,
104 $method,
105 $headers,
106 isset($option['body']) && $option['body'] !== '' ? (string)$option['body'] : null,
107 (string)$auth,
108 );
109 $rawResult = $response['body'];
110 $http_code = $response['status'];
111 if ($response['error'] !== null) {
112 return ['header' => [], 'dimensions' => [], 'data' => [], 'error' => $response['error'], 'cache' => $cache];
113 }
114
115 $return = $this->jsonParser($option, $rawResult);
116 $return['rawdata'] = $rawResult;
117 $return['URL'] = $url;
118 $return['error'] = ($http_code >= 200 && $http_code < 300) ? 0 : 'HTTP response code: ' . $http_code;
119 $return['cache'] = $cache;
120
121 return $return;
122 }
123
124 /** @return array<string, string> */
125 private function parseHeaders(string $customHeaders): array {

Callers

nothing calls this directly

Calls 4

getCacheMetadataMethod · 0.95
parseHeadersMethod · 0.95
jsonParserMethod · 0.95
requestMethod · 0.45

Tested by

no test coverage detected