Api.php 2.14 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
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
22
23
        $client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
        $url = API_BASE . $url . "/?format=json";
	$result = $client->request('POST', $url, array(), $data);
        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
38
39
        return json_decode($json, true);
    }

    // Asking for complete list of groups
    static function get_groups() {
40
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/groups");
41
42
43
44
45
        return json_decode($json, true);
    }

    // Asking for complete list of feedback categories
    static function get_feedback_categories() {
46
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/feedback/categories");
47
48
49
50
51
        return json_decode($json, true);
    }

    // Asking for arguments by language
    static function get_arguments($language='en') {
52
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/arguments");
53
54
55
56
57
58
59
60
61
62
        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
        );
63
        $json = Api::post("campaigns/" . CAMPAIGN_ID . "/feedback/add", $data);
64
65
66
        return json_decode($json, true);
    }

Mindiell's avatar
Mindiell committed
67
68
};