Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Boost_API_Client
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 post
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 get_api_path
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WPCOM Boost API Client interface.
4 *
5 * @package automattic/jetpack-boost-core
6 */
7
8namespace Automattic\Jetpack\Boost_Core\Lib;
9
10use Automattic\Jetpack\Boost_Core\Contracts\Boost_API_Client;
11
12if ( ! defined( 'ABSPATH' ) ) {
13    exit( 0 );
14}
15
16/**
17 * A class that handles the Boost API client.
18 *
19 * The communication to the backend is done using this class on top of the Boost_API_Client interface.
20 */
21class WPCOM_Boost_API_Client implements Boost_API_Client {
22
23    /**
24     * Submit a POST request to boost API and return response.
25     *
26     * @param string  $path - Request path.
27     * @param mixed[] $payload - Request payload.
28     * @param mixed[] $args - Request arguments.
29     * @return mixed
30     */
31    public function post( $path, $payload = array(), $args = null ) {
32        return Utils::send_wpcom_request(
33            'POST',
34            $this->get_api_path( $path ),
35            $args,
36            $payload
37        );
38    }
39
40    /**
41     * Make a get request to boost API and return response.
42     *
43     * @param string  $path - Request path.
44     * @param mixed[] $query - Query parameters.
45     * @param mixed[] $args - Request arguments.
46     * @return mixed
47     */
48    public function get( $path, $query = array(), $args = null ) {
49        return Utils::send_wpcom_request(
50            'GET',
51            add_query_arg( $query, $this->get_api_path( $path ) ),
52            $args
53        );
54    }
55
56    /**
57     * Get the API path for the given path.
58     *
59     * @param string $path - Request path.
60     * @return string
61     */
62    private function get_api_path( $path ) {
63        $blog_id = (int) \Jetpack_Options::get_option( 'id' );
64
65        return sprintf( '/sites/%d/jetpack-boost/%s', $blog_id, $path );
66    }
67}