Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RemoteApiBackend | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
apiCall | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace Flow\Import\LiquidThreadsApi; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | class RemoteApiBackend extends ApiBackend { |
8 | /** |
9 | * @var string |
10 | */ |
11 | protected $apiUrl; |
12 | |
13 | /** |
14 | * @var string|null |
15 | */ |
16 | protected $cacheDir; |
17 | |
18 | /** |
19 | * @param string $apiUrl |
20 | * @param string|null $cacheDir |
21 | */ |
22 | public function __construct( $apiUrl, $cacheDir = null ) { |
23 | parent::__construct(); |
24 | $this->apiUrl = $apiUrl; |
25 | $this->cacheDir = $cacheDir; |
26 | } |
27 | |
28 | public function getKey() { |
29 | return $this->apiUrl; |
30 | } |
31 | |
32 | public function apiCall( array $params, $retry = 1 ) { |
33 | $params['format'] = 'json'; |
34 | $url = wfAppendQuery( $this->apiUrl, $params ); |
35 | $file = $this->cacheDir . '/' . md5( $url ) . '.cache'; |
36 | $this->logger->debug( __METHOD__ . ": $url" ); |
37 | if ( $this->cacheDir && file_exists( $file ) ) { |
38 | $result = file_get_contents( $file ); |
39 | } else { |
40 | $httpRequestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory(); |
41 | do { |
42 | $result = $httpRequestFactory->get( $url, [], __METHOD__ ); |
43 | } while ( $result === null && --$retry >= 0 ); |
44 | |
45 | if ( $this->cacheDir && file_put_contents( $file, $result ) === false ) { |
46 | $this->logger->warning( "Failed writing cached api result to $file" ); |
47 | } |
48 | } |
49 | |
50 | if ( !$result ) { |
51 | return []; |
52 | } |
53 | |
54 | return json_decode( $result, true ); |
55 | } |
56 | } |