From 4fb9d1a8e5978b25ea4bb2d452e0f2d21bd4956f Mon Sep 17 00:00:00 2001 From: Jakub Oleksy Date: Wed, 3 Dec 2014 12:20:15 -0700 Subject: [PATCH] More progress on PHP to service interaction. Moved to composer for managing dependencies, including adding a dependency on GuzzleHttp. --- .gitignore | 5 +++ Application.Insights.phpproj | 5 +++ Application.Insights.sln | 8 ----- ApplicationInsights/ApplicationInsights.php | 10 +----- .../Channel/Contracts/Data.php | 8 +++++ .../Channel/Contracts/Device.php | 7 ++++ .../Channel/Contracts/Envelope.php | 11 +++++++ .../Channel/Contracts/EventData.php | 2 ++ .../Channel/TelemetryChannel.php | 1 + .../Channel/TelemetrySender.php | 33 +++++-------------- .../Tests/TelemetryClientTest.php | 7 ---- .../Tests/TelemetrySenderTest.php | 8 +---- phpunit.xml | 2 +- 13 files changed, 51 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 1bc915c..d2a04a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +## Ignore Composer +vendor +*.lock +*.phar + ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/Application.Insights.phpproj b/Application.Insights.phpproj index 1b1684c..dbc0bba 100644 --- a/Application.Insights.phpproj +++ b/Application.Insights.phpproj @@ -37,6 +37,11 @@ + + + + + \ No newline at end of file diff --git a/Application.Insights.sln b/Application.Insights.sln index d32baeb..15c16dd 100644 --- a/Application.Insights.sln +++ b/Application.Insights.sln @@ -5,14 +5,6 @@ VisualStudioVersion = 12.0.30723.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{A0786B88-2ADB-4C21-ABE8-AA2D79766269}") = "Application.Insights", "Application.Insights.phpproj", "{6C54EAC1-21B0-4DBE-A7C8-59E4A4F81316}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BD4B101A-09FF-434E-800B-A9F17513944B}" - ProjectSection(SolutionItems) = preProject - ..\.gitattributes = ..\.gitattributes - ..\.gitignore = ..\.gitignore - ..\LICENSE = ..\LICENSE - ..\README.md = ..\README.md - EndProjectSection -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/ApplicationInsights/ApplicationInsights.php b/ApplicationInsights/ApplicationInsights.php index 52dac92..4419d0f 100644 --- a/ApplicationInsights/ApplicationInsights.php +++ b/ApplicationInsights/ApplicationInsights.php @@ -1,13 +1,5 @@ \ No newline at end of file diff --git a/ApplicationInsights/Channel/Contracts/Data.php b/ApplicationInsights/Channel/Contracts/Data.php index 8c9d17b..7561a35 100644 --- a/ApplicationInsights/Channel/Contracts/Data.php +++ b/ApplicationInsights/Channel/Contracts/Data.php @@ -11,6 +11,14 @@ class Data implements \JsonSerializable */ private $_data; + /** + * Creates a new Data. + */ + function __construct() + { + $this->_data['baseData'] = NULL; + } + /** * Gets the baseType field. */ diff --git a/ApplicationInsights/Channel/Contracts/Device.php b/ApplicationInsights/Channel/Contracts/Device.php index 978db9c..f5afc21 100644 --- a/ApplicationInsights/Channel/Contracts/Device.php +++ b/ApplicationInsights/Channel/Contracts/Device.php @@ -11,6 +11,13 @@ class Device implements \JsonSerializable */ private $_data; + /** + * Creates a new Device. + */ + function __construct() + { + } + /** * Gets the id field. */ diff --git a/ApplicationInsights/Channel/Contracts/Envelope.php b/ApplicationInsights/Channel/Contracts/Envelope.php index 233b3f9..4d714eb 100644 --- a/ApplicationInsights/Channel/Contracts/Envelope.php +++ b/ApplicationInsights/Channel/Contracts/Envelope.php @@ -11,6 +11,17 @@ class Envelope implements \JsonSerializable */ private $_data; + /** + * Creates a new Envelope. + */ + function __construct() + { + $this->_data['ver'] = 1; + $this->_data['name'] = NULL; + $this->_data['time'] = NULL; + $this->_data['sampleRate'] = 100.0; + } + /** * Gets the ver field. */ diff --git a/ApplicationInsights/Channel/Contracts/EventData.php b/ApplicationInsights/Channel/Contracts/EventData.php index a54210e..03d050d 100644 --- a/ApplicationInsights/Channel/Contracts/EventData.php +++ b/ApplicationInsights/Channel/Contracts/EventData.php @@ -28,6 +28,8 @@ class EventData implements \JsonSerializable { $this->_envelope_type_name = 'Microsoft.ApplicationInsights.Event'; $this->_data_type_name = 'EventData'; + $this->_data['ver'] = 2; + $this->_data['name'] = NULL; } /** diff --git a/ApplicationInsights/Channel/TelemetryChannel.php b/ApplicationInsights/Channel/TelemetryChannel.php index 5fe4430..2a55699 100644 --- a/ApplicationInsights/Channel/TelemetryChannel.php +++ b/ApplicationInsights/Channel/TelemetryChannel.php @@ -33,6 +33,7 @@ class TelemetryChannel } $envelope = new Contracts\Envelope(); + $envelope->set_ver(2); $envelope->set_name($data->get_envelope_type_name()); $envelope->set_time(gmdate('c') . 'Z'); $envelope->set_ikey('f22d426f-57e2-47c3-9668-c58013a26eb4'); diff --git a/ApplicationInsights/Channel/TelemetrySender.php b/ApplicationInsights/Channel/TelemetrySender.php index 6241206..cb4a225 100644 --- a/ApplicationInsights/Channel/TelemetrySender.php +++ b/ApplicationInsights/Channel/TelemetrySender.php @@ -18,12 +18,6 @@ class TelemetrySender */ private $_queue; - /** - * When set, instead of sending the data to the endpoint, the class will send the data it would have sent to this callback. - * @var callable - */ - private $_verificationCallback; - /** * Initializes a new TelemetrySender. * @param string $endpointURL Optional. Allows caller to override which endpoint to send data to. @@ -32,7 +26,6 @@ class TelemetrySender function __construct($endpointURL = 'http://dc.services.visualstudio.com/v2/track') { $this->_endpointURL = $endpointURL; - $this->_verificationCallback = NULL; $this->_queue = []; } @@ -54,23 +47,6 @@ class TelemetrySender $this->_endpointURL = $endpointURL; } - /** - * If set, returns the callback to use for verification; otherwise NULL. - * @return callable - */ - public function getVerificationCallback() - { - return $this->_verificationCallback; - } - - /** - * Sets the callback to use for verification. - * @param string $verificationCallback - */ - public function setVerificationCallback($verificationCallback) - { - return $this->_verificationCallback = $verificationCallback; - } /** * Returns the current queue. @@ -88,6 +64,15 @@ class TelemetrySender public function send($telemetryItem) { $serializedTelemetryItem = json_encode($telemetryItem); + + $client = new \GuzzleHttp\Client(); + + $response = $client->post($this->_endpointURL, [ + 'headers' => ['Accept' => 'application/json', + 'Content-Type' => 'application/json; charset=utf-8'], + 'body' => utf8_encode($serializedTelemetryItem), + 'proxy' => '127.0.0.1:8888' + ]); } } diff --git a/ApplicationInsights/Tests/TelemetryClientTest.php b/ApplicationInsights/Tests/TelemetryClientTest.php index 7d931d2..8a4ad53 100644 --- a/ApplicationInsights/Tests/TelemetryClientTest.php +++ b/ApplicationInsights/Tests/TelemetryClientTest.php @@ -7,13 +7,6 @@ use ApplicationInsights\TelemetryClient; */ class TelemetryClientTest extends \PHPUnit_Framework_TestCase { - /** - * Verifies the object is constructed properly. - */ - public function testConstructor() - { - } - public function testCurrent() { $telemetryClient = new TelemetryClient(); diff --git a/ApplicationInsights/Tests/TelemetrySenderTest.php b/ApplicationInsights/Tests/TelemetrySenderTest.php index fc4b6d7..d34fa9d 100644 --- a/ApplicationInsights/Tests/TelemetrySenderTest.php +++ b/ApplicationInsights/Tests/TelemetrySenderTest.php @@ -11,7 +11,7 @@ class TelemetrySenderTest extends \PHPUnit_Framework_TestCase /** * Verifies the object is constructed properly. */ - public function testConstructor() + public function test_constructor() { $telemetrySender = new TelemetrySender(); $this->assertEquals($telemetrySender->getEndpointURL(), 'http://dc.services.visualstudio.com/v2/track', 'Default Endpoint URL is incorrect.'); @@ -19,17 +19,11 @@ class TelemetrySenderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($telemetrySender->getVerificationCallback(), NULL, 'Callback should be NULL by default.'); } - public function telemetrySenderCallback($encodedItem) - { - echo $encodedItem; - } - public function testCurrent() { $envelope = new Envelope(); $envelope->setIKey('f22d426f-57e2-47c3-9668-c58013a26eb4'); $telemetrySender = new TelemetrySender(); - $telemetrySender->setVerificationCallback(array($this, 'telemetrySenderCallback')); $telemetrySender->send($envelope); } } diff --git a/phpunit.xml b/phpunit.xml index 15e409d..5175240 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -20,5 +20,5 @@ timeoutForMediumTests="10" timeoutForLargeTests="60" strict="false" - verbose="false" + verbose="true" \ No newline at end of file