This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
PHP SDK: Load collections and entities #2
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ed4d24e
Add base Endpoint, Collection, and Entity classes. Add initial collec…
davidfstr 110aa9a
Collections list contents. Entities load their content.
davidfstr 065758d
Extract XML utilities to XmlUtil class.
davidfstr 90a75a9
Support login with a token.
davidfstr d0a0989
Alter Http::request() to return its response as an object instead of …
davidfstr 73c9597
Alter unit tests to be run from root directory. This allows the tests…
davidfstr b912ce3
Atom parser can handle lists. Add unit tests for the parser.
davidfstr 0476e6e
Load entities by name. Quote entity names in endpoint paths correctly.
davidfstr c91a791
Pull up common loading logic from {Collection, Entity} to Endpoint.
davidfstr 0a7cdc3
Wordsmithing documentation.
davidfstr d2012db
Document AtomFeed class.
davidfstr 25f8911
Document how to generate a code coverage report.
davidfstr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,8 @@ | |
# Generated documentation | ||
docs/ | ||
|
||
# Generated code coverage report | ||
coverage/ | ||
|
||
# Local settings | ||
*.local.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Copyright 2012 Splunk, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* @package Splunk | ||
*/ | ||
class Splunk_AmbiguousKeyException extends RuntimeException {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright 2012 Splunk, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Contains utilities for parsing Atom feeds received from the Splunk REST API. | ||
* | ||
* @package Splunk | ||
*/ | ||
class Splunk_AtomFeed | ||
{ | ||
/** Name of the 's' namespace in Splunk Atom feeds. */ | ||
const NS_S = 'http://dev.splunk.com/ns/rest'; | ||
|
||
/** | ||
* Parses and returns the value inside the specified XML element. | ||
* | ||
* @param SimpleXMLElement $containerXml | ||
* @returns mixed | ||
*/ | ||
public static function parseValueInside($containerXml) | ||
{ | ||
$dictValue = $containerXml->children(Splunk_AtomFeed::NS_S)->dict; | ||
$listValue = $containerXml->children(Splunk_AtomFeed::NS_S)->list; | ||
|
||
if (Splunk_XmlUtil::elementExists($dictValue)) | ||
{ | ||
return Splunk_AtomFeed::parseDict($dictValue); | ||
} | ||
else if (Splunk_XmlUtil::elementExists($listValue)) | ||
{ | ||
return Splunk_AtomFeed::parseList($listValue); | ||
} | ||
else // value is scalar | ||
{ | ||
return Splunk_XmlUtil::getTextContent($containerXml); | ||
} | ||
} | ||
|
||
private static function parseDict($dictXml) | ||
{ | ||
$dict = array(); | ||
foreach ($dictXml->children(Splunk_AtomFeed::NS_S)->key as $keyXml) | ||
{ | ||
$key = Splunk_XmlUtil::getAttributeValue($keyXml, 'name'); | ||
$value = Splunk_AtomFeed::parseValueInside($keyXml); | ||
|
||
$dict[$key] = $value; | ||
} | ||
return $dict; | ||
} | ||
|
||
private static function parseList($listXml) | ||
{ | ||
$list = array(); | ||
foreach ($listXml->children(Splunk_AtomFeed::NS_S)->item as $itemXml) | ||
$list[] = Splunk_AtomFeed::parseValueInside($itemXml); | ||
return $list; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Copyright 2012 Splunk, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Represents a collection of entities accessible through Splunk's REST API. | ||
* | ||
* @package Splunk | ||
*/ | ||
class Splunk_Collection extends Splunk_Endpoint | ||
{ | ||
private $entries = NULL; | ||
|
||
// === Load === | ||
|
||
protected function load() | ||
{ | ||
$response = $this->service->get($this->path); | ||
$xml = new SimpleXMLElement($response->body); | ||
|
||
$entries = array(); | ||
foreach ($xml->entry as $entryData) | ||
{ | ||
$entries[] = $this->loadEntry($entryData); | ||
} | ||
|
||
$this->entries = $entries; | ||
$this->loaded = TRUE; | ||
} | ||
|
||
private function loadEntry($entryData) | ||
{ | ||
return new Splunk_Entity( | ||
$this->service, | ||
"{$this->path}/" . urlencode($entryData->title), | ||
$entryData); | ||
} | ||
|
||
// === Children === | ||
|
||
/** | ||
* Returns the unique entity with the specified name in this collection. | ||
* | ||
* @param string $name | ||
* @return Splunk_Entity | ||
* @throws Splunk_NoSuchKeyException | ||
* @throws Splunk_AmbiguousKeyException | ||
*/ | ||
public function get($name) | ||
{ | ||
$results = array(); | ||
foreach ($this->validate()->entries as $entry) | ||
{ | ||
if ($entry->getName() == $name) | ||
{ | ||
$results[] = $entry; | ||
} | ||
} | ||
|
||
if (count($results) == 0) | ||
{ | ||
throw new Splunk_NoSuchKeyException( | ||
"No value exists with key '{$name}'."); | ||
} | ||
else if (count($results) == 1) | ||
{ | ||
return $results[0]; | ||
} | ||
else | ||
{ | ||
throw new Splunk_AmbiguousKeyException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can the user specify a namespace? Should get take a second, optional argument? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I should. Namespace support is forthcoming. |
||
"Multiple values exist with key '{$name}'. " . | ||
"Specify a namespace to disambiguate."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright 2012 Splunk, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Represents a single endpoint in the Splunk REST API. | ||
* | ||
* @package Splunk | ||
*/ | ||
abstract class Splunk_Endpoint | ||
{ | ||
protected $service; | ||
protected $path; | ||
|
||
protected $loaded = FALSE; | ||
|
||
public function __construct($service, $path) | ||
{ | ||
$this->service = $service; | ||
$this->path = $path; | ||
} | ||
|
||
// === Load === | ||
|
||
/** Loads this resource if not already done. Returns self. */ | ||
protected function validate() | ||
{ | ||
if (!$this->loaded) | ||
{ | ||
$this->load(); | ||
assert($this->loaded); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Loads this resource. | ||
* | ||
* Implementations must set $this->loaded to TRUE before returning. | ||
*/ | ||
protected abstract function load(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does assigning to [] do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appends to the end of an array variable.
If the array variable does not exist, it creates it. My code always explicitly creates the array, so you shouldn't need to know that behavior.