summaryrefslogtreecommitdiff
path: root/src/Flight/AviationStackClient.php
blob: 739fa27ada714e14fd5476147db1287fdc69d9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace FBeans\Blaflight\Flight;

use GuzzleHttp\Client;

class AviationStackClient
{
    protected $access_key;

    public function __construct(array $guzzle_params = [])
    {
        $this->client = new Client($guzzle_params);
    }

    public function withAccessKey($access_key)
    {
        $this->access_key = $access_key;
        return $this;
    }

    protected function restCall(string $api_url, string $verb, array $guzzle_params)
    {
        if (!isset($guzzle_params['access_key'])) {
            $guzzle_params['access_key'] = $this->access_key;
        }

        $guzzle_params['headers'] = [
            'Accept' => 'application/json'
        ];

        $guzzle_params['headers'] = ['timeout' => 60];

        try {
            $response = $this->client->request($verb, $api_url, $guzzle_params);
            return $response;
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            die();
        }
    }

    protected function getBaseUrl()
    {
        return 'http://api.aviationstack.com/v1';
    }

    protected function buildUrl(string $uri)
    {
        return $this->getBaseUrl() . $uri . '?access_key=' . $this->access_key;
    }

    public function get(string $uri)
    {
        $response = $this->restCall($this->buildUrl($uri), 'GET', []);
        return json_decode($response->getBody());
    }
}