Api.php 3.13 KB
Newer Older
Mindiell's avatar
Mindiell committed
1 2
<?php

3 4
use GuzzleHttp\Client;

Mindiell's avatar
Mindiell committed
5 6 7 8
/*
 * Static class for REST API communication
 */
class Api {
9
    // Generic get function for REST API
Mindiell's avatar
Mindiell committed
10
    static function get($url) {
11
        $client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
12
        $url = API_BASE . $url . "/?format=json";
13

okhin's avatar
okhin committed
14 15
        $result = $client->request('GET', $url);
        return $result->getBody();
Mindiell's avatar
Mindiell committed
16 17
    }

18 19
    // Generic post function for REST API
    static function post($url, $data) {
20 21
        $client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
        $url = API_BASE . $url . "/?format=json";
okhin's avatar
okhin committed
22
        $result = $client->request('POST', $url, array(), $data);
23
        return $result->getBody();
24 25 26
    }

    // Asking for campaign informations
Mindiell's avatar
Mindiell committed
27
    static function get_campaign() {
28
        $json = Api::get("campaigns/" . CAMPAIGN_ID);
29 30 31 32 33
        return json_decode($json, true);
    }

    // Asking for complete list of contacts
    static function get_contacts() {
34
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/contacts");
35 36 37
        return json_decode($json, true);
    }

38 39
    // Asking for a single contact
    static function get_contact($id) {
okhin's avatar
okhin committed
40 41
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/contacts/" . $id);
        return json_decode($json, true);
42 43
    }

44 45
    // Asking for complete list of groups
    static function get_groups() {
46
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/groups");
47 48 49
        return json_decode($json, true);
    }

okhin's avatar
okhin committed
50 51 52 53 54 55
    // //Asking for complete list of group types
    static function get_group_types() {
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/grouptypes");
        return json_decode($json, true);
    }

56 57
    // Asking for complete list of feedback categories
    static function get_feedback_categories() {
okhin's avatar
okhin committed
58
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/categories");
59 60 61 62 63
        return json_decode($json, true);
    }

    // Asking for arguments by language
    static function get_arguments($language='en') {
64
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/arguments");
65 66 67 68 69 70 71 72 73 74
        return json_decode($json, true);
    }

    // Posting a feedback
    static function post_feedback($contact_id, $comment, $feedback_category='') {
        $data = array(
            "contact_id"=>$contact_id,
            "comment"=>$comment,
            "feedback_category"=>$feedback_category
        );
75
        $json = Api::post("campaigns/" . CAMPAIGN_ID . "/feedback/add", $data);
76 77 78
        return json_decode($json, true);
    }

okhin's avatar
okhin committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // Getting a call from the SIP_API
    static function get_sip_call($call_id, $token) {
        $data = array('api' => JWT_TOKEN,
            'token' => $token
        );
        $client = new GuzzleHttp\Client(['base_uri' => SIP_ARI]);
        try {
            $res = $client->get("calls/$call_id", ['query' => $data]);
            return (GuzzleHttp\Psr7\copy_to_string($res->getBody()));
        } catch (RequestException $e) {
            return Psr7\str($e->getRequest());
            if ($e->hasResponse()) {
                echo Psr7\str($e->getResponse());
            }
        }
    }

Mindiell's avatar
Mindiell committed
96 97
};