Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
OrchestratorRequest | n/a |
0 / 0 |
n/a |
0 / 0 |
5 | n/a |
0 / 0 |
|||
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
orchestrate | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
getSupportedProgrammingLanguages | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | /** |
3 | * WikiLambda Orchestrator Interface base class |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
8 | * @license MIT |
9 | */ |
10 | |
11 | namespace MediaWiki\Extension\WikiLambda; |
12 | |
13 | use BagOStuff; |
14 | use GuzzleHttp\Client; |
15 | use GuzzleHttp\ClientInterface; |
16 | use MediaWiki\Utils\GitInfo; |
17 | use Psr\Http\Message\ResponseInterface; |
18 | |
19 | /** |
20 | * @codeCoverageIgnore |
21 | */ |
22 | class OrchestratorRequest { |
23 | |
24 | protected Client $guzzleClient; |
25 | protected string $userAgentString; |
26 | protected BagOStuff $objectCache; |
27 | |
28 | /** |
29 | * @param ClientInterface $client GuzzleHttp Client used for requests |
30 | */ |
31 | public function __construct( ClientInterface $client ) { |
32 | $this->guzzleClient = $client; |
33 | |
34 | $this->userAgentString = 'wikifunctions-request/' . MW_VERSION; |
35 | $gitInfo = new GitInfo( MW_INSTALL_PATH . '/extensions/WikiLambda' ); |
36 | $gitHash = $gitInfo->getHeadSHA1(); |
37 | if ( $gitHash !== false ) { |
38 | $this->userAgentString .= '-WL' . substr( $gitHash, 0, 8 ); |
39 | } |
40 | |
41 | $this->objectCache = WikiLambdaServices::getZObjectStash(); |
42 | } |
43 | |
44 | /** |
45 | * @param \stdClass|array $query |
46 | * @param bool $bypassCache Whether to bypass the function call cache; this is only |
47 | * to be used for special circumstances, as it's potentially expensive. |
48 | * @return string response object returned by orchestrator, down-cast to a string |
49 | */ |
50 | public function orchestrate( $query, $bypassCache = false ): string { |
51 | // TODO (T338242): Use postAsync here. |
52 | $guzzleClient = $this->guzzleClient; |
53 | $userAgentString = $this->userAgentString; |
54 | |
55 | if ( $bypassCache ) { |
56 | return $this->guzzleClient->post( '/1/v1/evaluate/', [ |
57 | 'json' => $query, |
58 | 'headers' => [ |
59 | 'User-Agent' => $this->userAgentString, |
60 | ], |
61 | ] )->getBody()->getContents(); |
62 | } |
63 | |
64 | return $this->objectCache->getWithSetCallback( |
65 | $this->objectCache->makeKey( 'WikiLambdaFunctionCall', ZObjectUtils::makeCacheKeyFromZObject( $query ) ), |
66 | // TODO (T338243): Is this the right timeout? Maybe TTL_DAY or TTL_MONTH instead? |
67 | $this->objectCache::TTL_WEEK, |
68 | static function () use ( $query, $guzzleClient, $userAgentString ) { |
69 | return $guzzleClient->post( '/1/v1/evaluate/', [ |
70 | 'json' => $query, |
71 | 'headers' => [ |
72 | 'User-Agent' => $userAgentString, |
73 | ], |
74 | ] )->getBody()->getContents(); |
75 | } |
76 | ); |
77 | } |
78 | |
79 | /** |
80 | * @return ResponseInterface response object returned by orchestrator |
81 | */ |
82 | public function getSupportedProgrammingLanguages(): ResponseInterface { |
83 | // TODO (T338242): Use getAsync here. |
84 | return $this->guzzleClient->get( '/1/v1/supported-programming-languages/' ); |
85 | } |
86 | } |