From 51382f649d1e8c11fbf69ede9c0b8ba96267c1c2 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Sat, 18 Oct 2014 19:42:32 -0500 Subject: [PATCH 1/6] Add Socrata namespace --- README.md | 1 + composer.json | 4 +++- public/index.php | 17 ++++++++++++++++- public/socrata.php => src/Socrata.php | 13 ++----------- 4 files changed, 22 insertions(+), 13 deletions(-) rename public/socrata.php => src/Socrata.php (95%) diff --git a/README.md b/README.md index 5af51ec..5affcf5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ This library provides a simple wrapper for accessing some of the features of the The library is very simple. To access the Socrata API, you first instantiate a "Socrata" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls: ```php +use Socrata\Socrata; $socrata = new Socrata("http://data.medicare.gov"); $response = $socrata->get("/resource/abcd-2345.json"); ``` diff --git a/composer.json b/composer.json index 1114b62..e6f3976 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,8 @@ "ext-curl": "*" }, "autoload": { - "files": ["public/socrata.php"] + "psr-4": { + "Socrata\\": "src" + } } } diff --git a/public/index.php b/public/index.php index 45e5b43..2fc142f 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,20 @@ " . print_r($var) . ""; +} $view_uid = "ykw4-j3aj"; $root_url = "https://data.austintexas.gov"; diff --git a/public/socrata.php b/src/Socrata.php similarity index 95% rename from public/socrata.php rename to src/Socrata.php index 8f67cb1..d2c2b9a 100644 --- a/public/socrata.php +++ b/src/Socrata.php @@ -1,5 +1,7 @@ " . print_r($var) . ""; -} -?> From 206e0ebfdfe9846e0c2fd417cb10666a3414f4e2 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Sat, 18 Oct 2014 19:58:21 -0500 Subject: [PATCH 2/6] PSR-2 formatting --- src/Socrata.php | 335 ++++++++++++++++++++++++++++-------------------- 1 file changed, 193 insertions(+), 142 deletions(-) diff --git a/src/Socrata.php b/src/Socrata.php index d2c2b9a..20a3ade 100644 --- a/src/Socrata.php +++ b/src/Socrata.php @@ -2,157 +2,208 @@ namespace Socrata; -class Socrata { - // The base URL for this Socrata API, ex: http://data.medicare.gov or http://www.socrata.com - private $root_url = "http://opendata.socrata.com"; - - // App Token - private $app_token = ""; - - // Username and password, used for authenticated requests - private $user_name = ""; - private $password = ""; - - // Basic constructor - public function __construct($root_url = "", $app_token = "", $user_name = "", $password = "") { - $this->root_url = $root_url; - $this->app_token = $app_token; - $this->user_name = $user_name; - $this->password = $password; - return true; - } - - // create query URL based on the root URL, path, and parameters - public function create_query_url($path, $params = array()) { - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - // Build up our array of parameters - $parameters = array(); - foreach($params as $key => $value) { - array_push($parameters, urlencode($key) . "=" . urlencode($value)); +class Socrata +{ + /** + * The base URL for this Socrata API, ex: http://data.medicare.gov or http://www.socrata.com + * + * @var string + */ + private $root_url = "http://opendata.socrata.com"; + + /** + * App Token + * + * @var string + */ + private $app_token = ""; + + /** + * Username, used for authenticated requests + * + * @var string + */ + private $user_name = ""; + + /** + * Password, used for authenticated requests + * + * @var string + */ + private $password = ""; + + /** + * Basic constructor + * + * @param string $root_url + * @param string $app_token + * @param string $user_name + * @param string $password + */ + public function __construct($root_url, $app_token = "", $user_name = "", $password = "") { + $this->root_url = $root_url; + $this->app_token = $app_token; + $this->user_name = $user_name; + $this->password = $password; } - if(count($parameters) > 0) { - $full_url .= "?" . implode("&", $parameters); - } - - return $full_url; - } - - // create cURL handle, which can then be submitted via get - public function create_curl_handle($path, $params = array()) { - - // The full URL for this resource is the root + the path - $full_url = $this->create_query_url($path, $params); - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - // Set up request, and auth, if configured - if($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + /** + * create query URL based on the root URL, path, and parameters + * + * @param string $path + * @param array $params + * @return string + */ + private function create_query_url($path, array $params = array()) + { + // The full URL for this resource is the root + the path + $full_url = $this->root_url . $path; + + // Build up our array of parameters + $parameters = array(); + foreach ($params as $key => $value) { + array_push($parameters, urlencode($key) . "=" . urlencode($value)); + } + if (count($parameters) > 0) { + $full_url .= "?" . implode("&", $parameters); + } + + return $full_url; } - return $handle; - } - - // Convenience function for GET calls - public function get($path, $params = array()) { - - $handle = $this->create_curl_handle($path, $params); - - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if($code != "200") { - echo "Error \"$code\" from server: $response"; - die(); - } - - return json_decode($response, true); - } - - // Convenience function for Posts - public function post($path, $json_filter) { - - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($handle, CURLOPT_POST, true); - curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); - curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); - - // Set up request, and auth, if configured - if($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + /** + * create cURL handle, which can then be submitted via get + * + * @param string $path + * @param array $params + * @return resource + */ + private function create_curl_handle($path, array $params = array()) + { + // The full URL for this resource is the root + the path + $full_url = $this->create_query_url($path, $params); + + // Build up the headers we'll need to pass + $headers = array( + 'Accept: application/json', + 'Content-type: application/json', + "X-App-Token: " . $this->app_token + ); + + // Time for some cURL magic... + $handle = curl_init(); + curl_setopt($handle, CURLOPT_URL, $full_url); + curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + + // Set up request, and auth, if configured + if ($this->user_name != "" && $this->password != "") { + curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + } + + return $handle; } - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if($code != "200") { - echo "Error \"$code\" from server: $response"; - die(); + /** + * Convenience function for GET calls + * + * @param string $path + * @param array $params + * @return mixed + */ + public function get($path, array $params = array()) + { + $handle = $this->create_curl_handle($path, $params); + + $response = curl_exec($handle); + $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); + if ($code != "200") { + die("Error \"$code\" from server: $response"); + } + + return json_decode($response, true); } - return json_decode($response, true); - } - - // Convenience function for Puts - public function put($path, $json_filter) { - - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); - curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT"); - - // Set up request, and auth, if configured - if($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + /** + * Convenience function for Posts + * + * @param string $path + * @param bool $json_filter + * @return mixed + */ + public function post($path, $json_filter) + { + // The full URL for this resource is the root + the path + $full_url = $this->root_url . $path; + + // Build up the headers we'll need to pass + $headers = array( + 'Accept: application/json', + 'Content-type: application/json', + "X-App-Token: " . $this->app_token + ); + + // Time for some cURL magic... + $handle = curl_init(); + curl_setopt($handle, CURLOPT_URL, $full_url); + curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($handle, CURLOPT_POST, true); + curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); + curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); + + // Set up request, and auth, if configured + if ($this->user_name != "" && $this->password != "") { + curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + } + + $response = curl_exec($handle); + $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); + if ($code != "200") { + die("Error \"$code\" from server: $response"); + } + + return json_decode($response, true); } - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if($code != "200") { - echo "Error \"$code\" from server: $response"; - die(); + /** + * Convenience function for Puts + * + * @param string $path + * @param bool $json_filter + * @return mixed + */ + public function put($path, $json_filter) + { + // The full URL for this resource is the root + the path + $full_url = $this->root_url . $path; + + // Build up the headers we'll need to pass + $headers = array( + 'Accept: application/json', + 'Content-type: application/json', + "X-App-Token: " . $this->app_token + ); + + // Time for some cURL magic... + $handle = curl_init(); + curl_setopt($handle, CURLOPT_URL, $full_url); + curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); + curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT"); + + // Set up request, and auth, if configured + if ($this->user_name != "" && $this->password != "") { + curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + } + + $response = curl_exec($handle); + $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); + if ($code != "200") { + die("Error \"$code\" from server: $response"); + } + + return json_decode($response, true); } - - return json_decode($response, true); - } } From 6ac2263c3f41a88e287922d294f17796ec34398a Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Sun, 19 Oct 2014 21:21:17 -0500 Subject: [PATCH 3/6] Add PHPUnit and config --- .gitignore | 5 ++++- README.md | 37 +++++++++++++++++++++++++++++++++++++ composer.json | 11 ++++++++++- phpunit.xml.dist | 32 ++++++++++++++++++++++++++++++++ test/config/bootstrap.php | 5 +++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 test/config/bootstrap.php diff --git a/.gitignore b/.gitignore index 64c5136..4b8b859 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ .DS_Store -/vendor/ +/vendor +/composer.lock +/phpunit.xml +/build # Ignore eclipse project files .settings diff --git a/README.md b/README.md index 5affcf5..0ae4846 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,21 @@ Updated 2014-07-22, Chris Metcalf, chris.metcalf (at) socrata.com This library provides a simple wrapper for accessing some of the features of the Socrata Open Data API from PHP. Currently it supports HTTP GET, POST, and PUT operations. +## Installation + +Add the following to your project's `composer.json`: + +```json +"require": { + "socrata/soda-php": "*" +} +``` + +If not using composer, simply require the `Socrata` class: +```php +require '{install_dir}/src/Socrata.php'; +``` + The library is very simple. To access the Socrata API, you first instantiate a "Socrata" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls: ```php @@ -38,3 +53,25 @@ $response = $socrata->put("/resource/abcd-2345.json", $data_as_json); ``` The library also includes a simple example application, which retrieves rows from a dataset and dumps them in a simple table. + +## Development and Testing + +Unit testing uses the standard `PHPUnit` library. Please add a failing test before making any modifications. Run tests with the following command: + +```bash +$ vendor/bin/phpunit +``` + +# Configuration + +Copy the provided `phpunit.xml.dist` file to `phpunit.xml` and add your credentials as constants in the `php` section. You should sign up at a providing site and register your application for proper access. + +```xml + + + + + + + +``` diff --git a/composer.json b/composer.json index e6f3976..0a582f1 100644 --- a/composer.json +++ b/composer.json @@ -5,11 +5,20 @@ "homepage": "http://dev.socrata.com", "require": { "php": "~5.3", - "ext-curl": "*" + "ext-curl": "*", + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": "@stable" }, "autoload": { "psr-4": { "Socrata\\": "src" } + }, + "autoload-dev": { + "psr-4": { + "Socrata\\Test\\": "test" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..692f4df --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,32 @@ + + + + + + ./test + + + + + + + + + + + ./src + + ./vendor + ./test + + + + + + + + + diff --git a/test/config/bootstrap.php b/test/config/bootstrap.php new file mode 100644 index 0000000..75e426e --- /dev/null +++ b/test/config/bootstrap.php @@ -0,0 +1,5 @@ + Date: Wed, 22 Oct 2014 23:41:15 -0500 Subject: [PATCH 4/6] Update namespace, add test, and refactoring --- README.md | 38 +++----- composer.json | 4 +- public/index.php | 73 ++++++---------- src/Client.php | 177 +++++++++++++++++++++++++++++++++++++ src/Socrata.php | 209 -------------------------------------------- test/ClientTest.php | 52 +++++++++++ 6 files changed, 273 insertions(+), 280 deletions(-) create mode 100644 src/Client.php delete mode 100644 src/Socrata.php create mode 100644 test/ClientTest.php diff --git a/README.md b/README.md index 0ae4846..ad34362 100644 --- a/README.md +++ b/README.md @@ -13,17 +13,17 @@ Add the following to your project's `composer.json`: } ``` -If not using composer, simply require the `Socrata` class: +If not using composer, simply require the `socrata\soda\Client` class: ```php -require '{install_dir}/src/Socrata.php'; +require '{install_dir}/src/Client.php'; ``` -The library is very simple. To access the Socrata API, you first instantiate a "Socrata" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls: +The library is very simple. To access the Socrata API, you first instantiate a "socrata\soda\Client" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls: ```php -use Socrata\Socrata; -$socrata = new Socrata("http://data.medicare.gov"); -$response = $socrata->get("/resource/abcd-2345.json"); +use socrata\soda\Client; +$sodaClient = new Client("http://data.medicare.gov"); +$response = $sodaClient->get("/resource/abcd-2345.json"); ``` ## Querying @@ -31,11 +31,12 @@ $response = $socrata->get("/resource/abcd-2345.json"); [Simple filters](http://dev.socrata.com/docs/filtering.html) and [SoQL Queries](http://dev.socrata.com/docs/queries.html) can be passed as a parameter to the `get` function: ```php -$socrata = new Socrata("https://data.austintexas.gov", $app_token); +use socrata\soda\Client; +$sodaClient = new Client("https://data.austintexas.gov", $app_token); $params = array("\$where" => "within_circle(location, $latitude, $longitude, $range)"); -$response = $socrata->get("/resource/$view_uid.json", $params); +$response = $sodaClient->get("/resource/$view_uid.json", $params); ``` ## Publishing @@ -43,13 +44,14 @@ $response = $socrata->get("/resource/$view_uid.json", $params); To use the library to publish data you can use the PUT (replace) or POST (upsert) methods: ```php -$socrata = new Socrata("https://data.medicare.gov", $app_token, $user_name, $password); +use socrata\soda\Client; +$sodaClient = new Client("https://data.medicare.gov", $app_token, $user_name, $password); // Publish data via 'upsert' -$response = $socrata->post("/resource/abcd-2345.json", $data_as_json); +$response = $sodaClient->post("/resource/abcd-2345.json", $data_as_json); // Publish data via 'replace' -$response = $socrata->put("/resource/abcd-2345.json", $data_as_json); +$response = $sodaClient->put("/resource/abcd-2345.json", $data_as_json); ``` The library also includes a simple example application, which retrieves rows from a dataset and dumps them in a simple table. @@ -61,17 +63,3 @@ Unit testing uses the standard `PHPUnit` library. Please add a failing test bef ```bash $ vendor/bin/phpunit ``` - -# Configuration - -Copy the provided `phpunit.xml.dist` file to `phpunit.xml` and add your credentials as constants in the `php` section. You should sign up at a providing site and register your application for proper access. - -```xml - - - - - - - -``` diff --git a/composer.json b/composer.json index 0a582f1..ce829eb 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ }, "autoload": { "psr-4": { - "Socrata\\": "src" + "socrata\\soda\\": "src" } }, "autoload-dev": { "psr-4": { - "Socrata\\Test\\": "test" + "socrata\\soda\\test\\": "test" } } } diff --git a/public/index.php b/public/index.php index 2fc142f..0640d1f 100644 --- a/public/index.php +++ b/public/index.php @@ -2,10 +2,10 @@ if (file_exists("../vendor/autoload.php")) { require_once "../vendor/autoload.php"; } else { - require_once "../src/Socrata.php"; + require_once "../src/Client.php"; } -use Socrata\Socrata; +use socrata\soda\Client; // Convenience functions function array_get($needle, array $haystack, $default = NULL) { @@ -16,67 +16,52 @@ function pre_dump($var) { echo "
" . print_r($var) . "
"; } - $view_uid = "ykw4-j3aj"; - $root_url = "https://data.austintexas.gov"; - $app_token = "B0ixMbJj4LuQVfYnz95Hfp3Ni"; - $response = NULL; +$view_uid = "y9us-9xdf"; +$root_url = "https://data.medicare.gov"; +$footnote = array_get("footnote", $_POST); - $latitude = array_get("latitude", $_POST); - $longitude = array_get("longitude", $_POST); - $range = array_get("range", $_POST); +// Create a new unauthenticated client +$sodaClient = new Client($root_url); +$params = array(); - if($latitude != NULL && $longitude != NULL && $range != NULL) { - // Create a new unauthenticated client - $socrata = new Socrata($root_url, $app_token); - - $params = array("\$where" => "within_circle(location, $latitude, $longitude, $range)"); +if (!empty($footnote)) { + $params['$where'] = "footnote=$footnote"; +} - $response = $socrata->get("/resource/$view_uid.json", $params); - } +$response = $sodaClient->get("/resource/{$view_uid}.json", $params); ?> - Austin Dangerous Dogs + Medicare Footnote Crosswalk -

Austin Dangerous Dogs

- - -
-

Try 30.27898, -97.68351 with a range of 1000 meters

- - -
- - -
- - -
- - -
- +

Medicare Footnote Crosswalk

+

+
+ + +
+

Results

- - + + - + - - + + - +
DescriptionAddressFootnoteFootnote Text
+ -

Raw Response

-
- +

Raw Response

+
diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..5af9116 --- /dev/null +++ b/src/Client.php @@ -0,0 +1,177 @@ +root_url = $root_url; + $this->app_token = $app_token; + $this->user_name = $user_name; + $this->password = $password; + } + + /** + * Convenience function for GET calls + * + * @param string $path + * @param array $params + * @return mixed + */ + public function get($path, array $params = array()) + { + $full_url = $this->buildQueryUrl($path, $params); + $handle = $this->buildHandle($full_url); + + return $this->executeAndDecode($handle); + } + + /** + * Convenience function for Posts + * + * @param string $path + * @param mixed $data + * @return mixed + */ + public function post($path, $data) + { + $full_url = $this->root_url . $path; + $handle = $this->buildHandle($full_url); + + curl_setopt($handle, CURLOPT_POST, 1); + curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data)); + + return $this->executeAndDecode($handle); + } + + /** + * Convenience function for Puts + * + * @param string $path + * @param mixed $data + * @return mixed + */ + public function put($path, $data) + { + $full_url = $this->root_url . $path; + $handle = $this->buildHandle($full_url); + + curl_setopt($handle, CURLOPT_PUT, 1); + curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data)); + + return $this->executeAndDecode($handle); + } + + /** + * create query URL based on the root URL, path, and parameters + * + * @param string $path + * @param array $params + * @return string + */ + private function buildQueryUrl($path, array $params = array()) + { + $full_url = $this->root_url . $path; + + // Build up our array of parameters + $parameters = array(); + foreach ($params as $key => $value) { + array_push($parameters, urlencode($key) . "=" . urlencode($value)); + } + if (count($parameters) > 0) { + $full_url .= "?" . implode("&", $parameters); + } + + return $full_url; + } + + /** + * create cURL handle, which can then be submitted via get + * + * @param string $path + * @param array $params + * @return resource + */ + private function buildHandle($full_url) + { + // Build up the headers we'll need to pass + $headers = array( + 'Accept: application/json', + 'Content-type: application/json', + "X-App-Token: " . $this->app_token + ); + + // Time for some cURL magic... + $handle = curl_init(); + curl_setopt($handle, CURLOPT_URL, $full_url); + curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); + + // Set up request, and auth, if configured + if ($this->user_name != "" && $this->password != "") { + curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); + } + + return $handle; + } + + /** + * Execute a CURL resource and json_decode the result + * + * @param resource $handle + * @return mixed + * @throws RuntimeException if the request was not successful + */ + private function executeAndDecode($handle) + { + $response = curl_exec($handle); + curl_close($handle); + + return json_decode($response, true); + } +} diff --git a/src/Socrata.php b/src/Socrata.php deleted file mode 100644 index 20a3ade..0000000 --- a/src/Socrata.php +++ /dev/null @@ -1,209 +0,0 @@ -root_url = $root_url; - $this->app_token = $app_token; - $this->user_name = $user_name; - $this->password = $password; - } - - /** - * create query URL based on the root URL, path, and parameters - * - * @param string $path - * @param array $params - * @return string - */ - private function create_query_url($path, array $params = array()) - { - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - // Build up our array of parameters - $parameters = array(); - foreach ($params as $key => $value) { - array_push($parameters, urlencode($key) . "=" . urlencode($value)); - } - if (count($parameters) > 0) { - $full_url .= "?" . implode("&", $parameters); - } - - return $full_url; - } - - /** - * create cURL handle, which can then be submitted via get - * - * @param string $path - * @param array $params - * @return resource - */ - private function create_curl_handle($path, array $params = array()) - { - // The full URL for this resource is the root + the path - $full_url = $this->create_query_url($path, $params); - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - - // Set up request, and auth, if configured - if ($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); - } - - return $handle; - } - - /** - * Convenience function for GET calls - * - * @param string $path - * @param array $params - * @return mixed - */ - public function get($path, array $params = array()) - { - $handle = $this->create_curl_handle($path, $params); - - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if ($code != "200") { - die("Error \"$code\" from server: $response"); - } - - return json_decode($response, true); - } - - /** - * Convenience function for Posts - * - * @param string $path - * @param bool $json_filter - * @return mixed - */ - public function post($path, $json_filter) - { - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($handle, CURLOPT_POST, true); - curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); - curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); - - // Set up request, and auth, if configured - if ($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); - } - - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if ($code != "200") { - die("Error \"$code\" from server: $response"); - } - - return json_decode($response, true); - } - - /** - * Convenience function for Puts - * - * @param string $path - * @param bool $json_filter - * @return mixed - */ - public function put($path, $json_filter) - { - // The full URL for this resource is the root + the path - $full_url = $this->root_url . $path; - - // Build up the headers we'll need to pass - $headers = array( - 'Accept: application/json', - 'Content-type: application/json', - "X-App-Token: " . $this->app_token - ); - - // Time for some cURL magic... - $handle = curl_init(); - curl_setopt($handle, CURLOPT_URL, $full_url); - curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter); - curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT"); - - // Set up request, and auth, if configured - if ($this->user_name != "" && $this->password != "") { - curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password); - } - - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - if ($code != "200") { - die("Error \"$code\" from server: $response"); - } - - return json_decode($response, true); - } -} diff --git a/test/ClientTest.php b/test/ClientTest.php new file mode 100644 index 0000000..e7779d6 --- /dev/null +++ b/test/ClientTest.php @@ -0,0 +1,52 @@ +path = '/resource/' . self::SODA_VIEW . '.json'; + } + + public function testGetReturnsArray() + { + $client = new Client(self::SODA_HOST); + $result = $client->get($this->path, array('$limit' => '10')); + $this->assertInternalType('array', $result); + $this->assertCount(10, $result); + } + + public function testPostResultIsError() + { + $client = new Client(self::SODA_HOST, self::SODA_TOKEN, self::SODA_USER, self::SODA_PASS); + $result = $client->post($this->path, array( + 'footnote' => '99', + 'footnote_text' => 'soda-php sdk test - disregard' + )); + $this->assertArrayHasKey('error', $result); + $this->assertTrue($result['error']); + } + + public function testPutResultIsError() + { + $client = new Client(self::SODA_HOST, self::SODA_TOKEN); + $result = $client->put($this->path, array( + 'footnote' => '99', + 'footnote_text' => 'remove me' + )); + $this->assertArrayHasKey('error', $result); + $this->assertTrue($result['error']); + } +} From bdd016ef7451552f5717761c254921df8a7972e1 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Wed, 22 Oct 2014 23:51:56 -0500 Subject: [PATCH 5/6] Update composer keywords --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ce829eb..99ba4a5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "socrata/soda-php", "description": "A simple library to make it easier to access SODA services from PHP", - "keywords": ["SODA", "socrata"], + "keywords": ["SODA", "socrata", "open data", "sdk"], "homepage": "http://dev.socrata.com", "require": { "php": "~5.3", From 83dde81823a5be6644ca23b128c1aa10778b9ce2 Mon Sep 17 00:00:00 2001 From: George Cooksey Date: Thu, 23 Oct 2014 00:03:30 -0500 Subject: [PATCH 6/6] Add license and archive to composer config --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index 99ba4a5..21ffc03 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "A simple library to make it easier to access SODA services from PHP", "keywords": ["SODA", "socrata", "open data", "sdk"], "homepage": "http://dev.socrata.com", + "license": "Apache-2.0", "require": { "php": "~5.3", "ext-curl": "*", @@ -20,5 +21,8 @@ "psr-4": { "socrata\\soda\\test\\": "test" } + }, + "archive": { + "exclude": ["public", "test"] } }