Api.php 3.03 KB
Newer Older
1 2
<?php

3 4
use GuzzleHttp\Client;

5 6 7 8
/*
 * Static class for REST API communication
 */
class Api {
9
    // Generic get function for REST API
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();
16 17
    }

18 19
    // Generic post function for REST API
    static function post($url, $data) {
20 21 22 23 24 25 26 27 28 29 30 31 32
        try {
            $client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
            $url = API_BASE . $url . "/?format=json";
            $result = $client->request('POST', $url, ['body' => json_encode($data), "headers" => ['Content-Type' => 'application/json']]);
            return $result->getBody();
        } catch (Exception $e) {
            echo $e;
            if ( $e->hasResponse()) {
                echo $e->getResponse()->getBody();
            } else {
                echo $e;
            }
        }
33 34 35
    }

    // Asking for campaign informations
36
    static function get_campaign() {
37
        $json = Api::get("campaigns/" . CAMPAIGN_ID);
38 39 40 41 42
        return json_decode($json, true);
    }

    // Asking for complete list of contacts
    static function get_contacts() {
43
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/contacts");
44 45 46
        return json_decode($json, true);
    }

47 48
    // Asking for a single contact
    static function get_contact($id) {
okhin's avatar
okhin committed
49 50
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/contacts/" . $id);
        return json_decode($json, true);
51 52
    }

53 54
    // Asking for complete list of groups
    static function get_groups() {
55
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/groups");
56 57 58
        return json_decode($json, true);
    }

okhin's avatar
okhin committed
59 60 61 62 63 64
    // //Asking for complete list of group types
    static function get_group_types() {
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/grouptypes");
        return json_decode($json, true);
    }

65 66
    // Asking for complete list of feedback categories
    static function get_feedback_categories() {
okhin's avatar
okhin committed
67
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/categories");
68 69 70 71 72
        return json_decode($json, true);
    }

    // Asking for arguments by language
    static function get_arguments($language='en') {
73
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/arguments");
74
        return json_decode($json, true);
okhin's avatar
okhin committed
75 76 77
    }

    // Asking for organization details
okhin's avatar
okhin committed
78
    static function get_organization() {
okhin's avatar
okhin committed
79 80
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/organization");
        return json_decode($json, true);
81 82 83 84 85
    }

    // Posting a feedback
    static function post_feedback($contact_id, $comment, $feedback_category='') {
        $data = array(
86
            "callee"=>$contact_id,
87
            "comment"=>$comment,
88
            "category"=>$feedback_category
89
        );
okhin's avatar
okhin committed
90
        $json = Api::post("campaigns/" . CAMPAIGN_ID . "/feedbacks", $data);
91 92
        return json_decode($json, true);
    }
93 94
};