Skip to content

Commit

Permalink
Merge pull request #85 from caseysoftware/library-updates
Browse files Browse the repository at this point in the history
Library updates
  • Loading branch information
Joe Wegner authored Jul 7, 2016
2 parents c19aa60 + 9054b95 commit cf8fa16
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 7 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: php
php:
- "5.5"
- "5.4"
- "5.3"
- "7.0"
- "5.6"
before_script:
- composer self-update
- composer install --prefer-source
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ that matches that version. That Service Description defines the operations avail

Currently the Keen IO Webservice Client only supports - and automatically defaults - to the current version (`3.0`) of the API.

##### More details about the client

Since the Keen client extends the [Guzzle](http://guzzlephp.org/) client, you get all the power and flexibility of that behind the scenes. If you need more complex logging, backoff/retry handling, or asynchronous requests, check out their [Plugins](http://guzzle3.readthedocs.io/docs.html#plugins).

###### Example
```php
use KeenIO\Client\KeenIOClient;
Expand Down Expand Up @@ -176,6 +180,44 @@ $allowedOperations = ['read'];
$scopedKey = $client->createScopedKey($filters, $allowedOperations);
```

#### Using saved queries

[Saved Queries](https://keen.io/docs/api/?php#saved-queries) allow you to perform with exactly the same parameters every time with minimal effort. It's effectively a bookmark or macro to analysis that you can jump to or share without configuring each time. While you can create and access them via the Dashboard, the PHP library gives you the same ability.

######Example: Creating a Saved Query
```php
$client = KeenIOClient::factory([
'projectId' => $project_id,
'masterKey' => $master_key
]);

$query = [
"analysis_type" => "count",
"event_collection" => "api_requests",
"filters" =>
[
[
"property_name" => "user_agent",
"operator" => "ne",
"property_value"=> "Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)"
]
],
"timeframe" => "this_2_weeks"
];

$results = $client->createSavedQuery(['query_name' => 'total-API-requests', 'query' => $query]);
```

######Example: Retrieving a Saved Query
```php
$client = KeenIOClient::factory([
'projectId' => $project_id,
'masterKey' => $master_key
]);

$results = $client->getSavedQuery(['query_name' => 'total-API-requests']);
```

Questions & Support
-------------------
If you have any questions, bugs, or suggestions, please
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"chat": "https://www.hipchat.com/gIdidQscL"
},
"require": {
"php": ">=5.3.3",
"php": ">=5.6.0",
"ext-mcrypt": "*",
"guzzle/guzzle": "~3.7"
},
"require-dev": {
"phpunit/phpunit": "~3.7.0"
"phpunit/phpunit": "5.3.*"
},
"homepage": "http://keen.io",
"autoload": {
Expand Down
39 changes: 37 additions & 2 deletions src/Client/KeenIOClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* @method array getProjects(array $args = array()) {@command KeenIO getProjects}
* @method array getProject(array $args = array()) {@command KeenIO getProject}
* @method array getProperty(array $args = array()) {@command KeenIO getProperty}
* @method array getSavedQueries(array $args = array()) {@command KeenIO getProperty}
* @method array getSavedQuery(array $args = array()) {@command KeenIO getProperty}
* @method array createSavedQuery(array $args = array()) {@command KeenIO getProperty}
* @method array deleteSavedQuery(array $args = array()) {@command KeenIO getProperty}
* @method array getSavedQueryResults(array $args = array()) {@command KeenIO getProperty}
* @method array getEventSchemas(array $args = array()) {@command KeenIO getEventSchemas}
* @method array deleteEvents(array $args = array()) {@command KeenIO deleteEvents}
* @method array deleteEventProperties(array $args = array()) {@command KeenIO deleteEventProperties}
Expand Down Expand Up @@ -49,7 +54,9 @@ public static function factory($config = array())
'masterKey' => null,
'writeKey' => null,
'readKey' => null,
'projectId' => null
'projectId' => null,
'organizationKey' => null,
'organizationId' => null
);

// Create client configuration
Expand All @@ -59,7 +66,7 @@ public static function factory($config = array())
// Because each API Resource uses a separate type of API Key, we need to expose them all in
// `commands.params`. Doing it this way allows the Service Definitions to set what API Key is used.
$parameters = array();
foreach (array('masterKey', 'writeKey', 'readKey') as $key) {
foreach (array('masterKey', 'writeKey', 'readKey', 'organizationKey') as $key) {
$parameters[$key] = $config->get($key);
}

Expand Down Expand Up @@ -148,6 +155,26 @@ public function getProjectId()
return $this->getConfig('projectId');
}

/**
* Sets the Organization Id used by the Keen IO Client
*
* @param string $organizationId
*/
public function setOrganizationId($organizationId)
{
$this->getConfig()->set('organizationId', $organizationId);
}

/**
* Gets the Organization Id being used by the Keen IO Client
*
* @return string|null Value of the OrganizationId or NULL
*/
public function getOrganizationId()
{
return $this->getConfig('organizationId');
}

/**
* Sets the API Write Key used by the Keen IO Client
*
Expand Down Expand Up @@ -390,4 +417,12 @@ protected static function parseConfig($config, $default)

return $config;
}

public static function cleanQueryName($raw)
{
$filtered = str_replace(' ', '-', $raw);
$filtered = preg_replace("/[^A-Za-z0-9_\-]/", "", $filtered);

return $filtered;
}
}
154 changes: 154 additions & 0 deletions src/Client/Resources/keen-io-3_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
),
),

'createProject' => array(
'uri' => 'organizations/{organizationId}/projects',
'description' => 'Creates a project for the specified organization and returns the project id for later usage.',
'httpMethod' => 'POST',
'parameters' => array(
'organizationKey' => array(
'location' => 'header',
'description' => 'The Organization Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'project_data' => array(
'location' => 'body',
'type' => 'array',
'filters' => array('json_encode'),
),
),
),

'getProjects' => array(
'uri' => 'projects',
'description' => 'Returns the projects accessible to the API user, as well as links to project sub-resources for discovery.',
Expand Down Expand Up @@ -52,6 +73,139 @@
),
),

'getSavedQueries' => array(
'uri' => 'projects/{projectId}/queries/saved',
'description' => 'Returns the saved queries accessible to the API user on the specified project.',
'httpMethod' => 'GET',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
),
),

'getSavedQuery' => array(
'uri' => 'projects/{projectId}/queries/saved/{query_name}',
'description' => 'Returns the detailed information about the specified query, as well as links to retrieve results.',
'httpMethod' => 'GET',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'query_name' => array(
'location' => 'uri',
'description' => 'The saved query.',
'required' => true,
),
),
),

'createSavedQuery' => array(
'uri' => 'projects/{projectId}/queries/saved/{query_name}',
'description' => 'Creates the described query.',
'httpMethod' => 'PUT',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'query_name' => array(
'location' => 'uri',
'description' => 'The desired name of the query.',
'filters' => array(["method" => '\KeenIO\Client\KeenIOClient::cleanQueryName', "args" => ["@value"]]),
'required' => true,
),
'query' => array(
'location' => 'body',
'type' => 'array',
'filters' => array('json_encode'),
),
),
),

'updateSavedQuery' => array(
'uri' => 'projects/{projectId}/queries/saved/{query_name}',
'description' => 'Creates the described query.',
'httpMethod' => 'PUT',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'query_name' => array(
'location' => 'uri',
'description' => 'The desired name of the query.',
'filters' => array(["method" => '\KeenIO\Client\KeenIOClient::cleanQueryName', "args" => ["@value"]]),
'required' => true,
),
'query' => array(
'location' => 'body',
'type' => 'array',
'filters' => array('json_encode'),
),
),
),

'deleteSavedQuery' => array(
'uri' => 'projects/{projectId}/queries/saved/{query_name}',
'description' => 'Deletes the specified query.',
'httpMethod' => 'DELETE',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'query_name' => array(
'location' => 'uri',
'description' => 'The saved query.',
'required' => true,
),
),
),

'getSavedQueryResults' => array(
'uri' => 'projects/{projectId}/queries/saved/{query_name}/result',
'description' => 'Returns the results of executing the specified query.',
'httpMethod' => 'GET',
'parameters' => array(
'masterKey' => array(
'location' => 'header',
'description' => 'The Master API Key.',
'sentAs' => 'Authorization',
'pattern' => '/^([[:alnum:]])+$/',
'type' => 'string',
'required' => true,
),
'query_name' => array(
'location' => 'uri',
'description' => 'The saved query.',
'required' => true,
),
),
),

'getCollections' => array(
'uri' => 'projects/{projectId}/events',
'description' => 'GET returns schema information for all the event collections in this project, including properties and their type. It also returns links to sub-resources.',
Expand Down

0 comments on commit cf8fa16

Please sign in to comment.