diff options
author | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-12-15 13:36:09 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-12-15 13:36:09 -0800 |
commit | 7f69f89d92cd54db26b4f420da1f46ed456a1668 (patch) | |
tree | c3ae1aea8668751755ed1a876bba003f1e0e7ae9 | |
parent | ebf68af9d4158450b6d673bf6d31ff5297057882 (diff) | |
parent | 2e7d3fcffd757c5ae1ef313fa2a8c3aa31ec29a0 (diff) | |
download | sendgrid-php-4.0.3.zip sendgrid-php-4.0.3.tar.gz sendgrid-php-4.0.3.tar.bz2 |
Merge pull request #183 from sendgrid/statsv4.0.3
Add stats [GET] endpoint
-rwxr-xr-x | example_v3.php | 25 | ||||
-rw-r--r-- | lib/Client.php | 2 | ||||
-rw-r--r-- | lib/resources/global_stats.php | 40 | ||||
-rw-r--r-- | test/unit/resources/global_statsTest.php | 82 |
4 files changed, 149 insertions, 0 deletions
diff --git a/example_v3.php b/example_v3.php index 33322db..47a0e2b 100755 --- a/example_v3.php +++ b/example_v3.php @@ -9,6 +9,31 @@ $sendgrid = new Client($sendgrid_apikey); /* +$start_date = "2015-12-01"; +$response = $sendgrid->global_stats->get($start_date); +print("Status Code: " . $response->getStatusCode() . "\n"); +print("Body: " . $response->getBody() . "\n\n"); + +$end_date = "2015-12-14"; +$response = $sendgrid->global_stats->get($start_date, $end_date); +print("Status Code: " . $response->getStatusCode() . "\n"); +print("Body: " . $response->getBody() . "\n\n"); + +$aggregated_by = "day"; +$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by); +print("Status Code: " . $response->getStatusCode() . "\n"); +print("Body: " . $response->getBody() . "\n\n"); + +$aggregated_by = "week"; +$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by); +print("Status Code: " . $response->getStatusCode() . "\n"); +print("Body: " . $response->getBody() . "\n\n"); + +$aggregated_by = "month"; +$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by); +print("Status Code: " . $response->getStatusCode() . "\n"); +print("Body: " . $response->getBody() . "\n\n"); + $group_id = 70; $email = 'example@example.com'; $response = $sendgrid->asm_suppressions->delete($group_id, $email); diff --git a/lib/Client.php b/lib/Client.php index a25ce15..da71d5d 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -3,6 +3,7 @@ require 'resources/api_keys.php'; require 'resources/asm_groups.php'; require 'resources/asm_suppressions.php'; +require 'resources/global_stats.php'; class Client { @@ -48,6 +49,7 @@ class Client $this->api_keys = new APIKeys($this); $this->asm_groups = new ASMGroups($this); $this->asm_suppressions = new ASMSuppressions($this); + $this->global_stats = new GlobalStats($this); } /** diff --git a/lib/resources/global_stats.php b/lib/resources/global_stats.php new file mode 100644 index 0000000..b139767 --- /dev/null +++ b/lib/resources/global_stats.php @@ -0,0 +1,40 @@ +<?php + +class GlobalStats +{ + protected + $base_endpoint, + $endpoint, + $client; + + public function __construct($client, $options=NULL) + { + $this->name = NULL; + $this->base_endpoint = "/v3/stats"; + $this->endpoint = "/v3/stats"; + $this->client = $client; + } + + public function getBaseEndpoint(){ + return $this->base_endpoint; + } + + public function getEndpoint(){ + return $this->endpoint; + } + + public function setEndpoint($endpoint){ + $this->endpoint = $endpoint; + } + + public function get($start_date, $end_date=Null, $aggregated_by=Null){ + $this->endpoint = $this->base_endpoint . "?start_date=" . $start_date; + if($end_date != Null){ + $this->endpoint = $this->endpoint . "&end_date=" . $end_date; + } + if($aggregated_by != Null){ + $this->endpoint = $this->endpoint . "&aggregated_by=" . $aggregated_by; + } + return $this->client->getRequest($this); + } +}
\ No newline at end of file diff --git a/test/unit/resources/global_statsTest.php b/test/unit/resources/global_statsTest.php new file mode 100644 index 0000000..49f5a3c --- /dev/null +++ b/test/unit/resources/global_statsTest.php @@ -0,0 +1,82 @@ +<?php +require_once __DIR__.'/baseTest.php'; + +class SendGridTest_GlobalStats extends baseTest +{ + public function testGET() + { + $code = 200; + $headers = array('Content-Type' => 'application/json'); + $body = '[ + { + "date": "2015-01-01", + "stats": [ + { + "metrics": { + "blocks": 1, + "bounce_drops": 0, + "bounces": 0, + "clicks": 0, + "deferred": 1, + "delivered": 1, + "invalid_emails": 1, + "opens": 1, + "processed": 2, + "requests": 3, + "spam_report_drops": 0, + "spam_reports": 0, + "unique_clicks": 0, + "unique_opens": 1, + "unsubscribe_drops": 0, + "unsubscribes": 0 + } + } + ] + }, + { + "date": "2015-01-02", + "stats": [ + { + "metrics": { + "blocks": 0, + "bounce_drops": 0, + "bounces": 0, + "clicks": 0, + "deferred": 0, + "delivered": 0, + "invalid_emails": 0, + "opens": 0, + "processed": 0, + "requests": 0, + "spam_report_drops": 0, + "spam_reports": 0, + "unique_clicks": 0, + "unique_opens": 0, + "unsubscribe_drops": 0, + "unsubscribes": 0 + } + } + ] + } + ]'; + + $sendgrid = $this->buildClient($code, $headers, $body); + $start_date = "2015-01-01"; + $response = $sendgrid->global_stats->get($start_date); + $this->assertEquals($code, $response->getStatusCode()); + $this->assertEquals($body, $response->getBody()); + + $sendgrid = $this->buildClient($code, $headers, $body); + $end_date = "2015-01-02"; + $response = $sendgrid->global_stats->get($start_date, $end_date); + $this->assertEquals($code, $response->getStatusCode()); + $this->assertEquals($body, $response->getBody()); + + $sendgrid = $this->buildClient($code, $headers, $body); + $aggregated_by = "day"; + $response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by); + $this->assertEquals($code, $response->getStatusCode()); + $this->assertEquals($body, $response->getBody()); + } + +}
\ No newline at end of file |