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 GuzzleHttp\Client; |
14 | use GuzzleHttp\ClientInterface; |
15 | use MediaWiki\Http\Telemetry; |
16 | use MediaWiki\Utils\GitInfo; |
17 | use Psr\Http\Message\ResponseInterface; |
18 | use Wikimedia\ObjectCache\BagOStuff; |
19 | |
20 | /** |
21 | * @codeCoverageIgnore |
22 | */ |
23 | class OrchestratorRequest { |
24 | |
25 | protected Client $guzzleClient; |
26 | protected string $userAgentString; |
27 | protected BagOStuff $objectCache; |
28 | |
29 | /** |
30 | * @param ClientInterface $client GuzzleHttp Client used for requests |
31 | */ |
32 | public function __construct( ClientInterface $client ) { |
33 | $this->guzzleClient = $client; |
34 | |
35 | $this->userAgentString = 'wikifunctions-request/' . MW_VERSION; |
36 | $gitInfo = new GitInfo( MW_INSTALL_PATH . '/extensions/WikiLambda' ); |
37 | $gitHash = $gitInfo->getHeadSHA1(); |
38 | if ( $gitHash !== false ) { |
39 | $this->userAgentString .= '-WL' . substr( $gitHash, 0, 8 ); |
40 | } |
41 | |
42 | $this->objectCache = WikiLambdaServices::getZObjectStash(); |
43 | } |
44 | |
45 | /** |
46 | * @param \stdClass|array $query |
47 | * @param bool $bypassCache Whether to bypass the function call cache; this is only |
48 | * to be used for special circumstances, as it's potentially expensive. |
49 | * @return string response object returned by orchestrator, down-cast to a string |
50 | */ |
51 | public function orchestrate( $query, $bypassCache = false ): string { |
52 | // TODO (T338242): Use postAsync here. |
53 | $guzzleClient = $this->guzzleClient; |
54 | $userAgentString = $this->userAgentString; |
55 | |
56 | // (T365053) Propagate request tracing headers |
57 | $telemetry = Telemetry::getInstance(); |
58 | $requestHeaders = $telemetry->getRequestHeaders(); |
59 | $requestHeaders['User-Agent'] = $userAgentString; |
60 | if ( $bypassCache ) { |
61 | return $this->guzzleClient->post( '/1/v1/evaluate/', [ |
62 | 'json' => $query, |
63 | 'headers' => $requestHeaders, |
64 | ] )->getBody()->getContents(); |
65 | } |
66 | |
67 | return $this->objectCache->getWithSetCallback( |
68 | $this->objectCache->makeKey( 'WikiLambdaFunctionCall', ZObjectUtils::makeCacheKeyFromZObject( $query ) ), |
69 | // TODO (T338243): Is this the right timeout? Maybe TTL_DAY or TTL_MONTH instead? |
70 | $this->objectCache::TTL_WEEK, |
71 | static function () use ( $query, $guzzleClient, $requestHeaders ) { |
72 | return $guzzleClient->post( '/1/v1/evaluate/', [ |
73 | 'json' => $query, |
74 | 'headers' => $requestHeaders, |
75 | ] )->getBody()->getContents(); |
76 | } |
77 | ); |
78 | } |
79 | |
80 | /** |
81 | * @return ResponseInterface response object returned by orchestrator |
82 | */ |
83 | public function getSupportedProgrammingLanguages(): ResponseInterface { |
84 | // TODO (T338242): Use getAsync here. |
85 | return $this->guzzleClient->get( '/1/v1/supported-programming-languages/' ); |
86 | } |
87 | } |