Skip to content

Commit

Permalink
Merge pull request #22 from SurveyGizmoOfficial/multi-region
Browse files Browse the repository at this point in the history
Support region selection
  • Loading branch information
SurveyGizmoOfficial authored Jun 26, 2018
2 parents 7cb1df1 + 9adce71 commit dbad47a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,27 @@ The library is intended to make integrating with SurveyGizmo easier and quicker

## Code Examples

#### Auto Loading & Authenticating
#### Auto Loading
If you are not using composer, you can use our AutoLoader directly as follows:
```php
require_once "<LIBRARY_PATH>/SurveyGizmoAutoLoader.php";
```

#### Configuring Requests for International Data Centers
If you are not using our US data center, you will need to choose the appropriate region (US, CA, or EU):
```php
try {
\SurveyGizmo\SurveyGizmoAPI::setRegion('EU');
} catch (Exception $e) {
die('Region not available');
}
```
The datacenter must be configured correctly prior to authentication and any other calls.

If you are not sure if you are using the US, EU, or CA API, see: https://apihelp.surveygizmo.com/help/us-eu-or-ca-api

#### Authenticating
```php
try {
\SurveyGizmo\SurveyGizmoAPI::auth("<YOUR API_KEY>", "<YOUR API_SECRET>");
} catch (Exception $e) {
Expand All @@ -64,7 +80,7 @@ try {
```

#### Rate limiting
For info on API request limiting see https://apihelp.surveygizmo.com/help/api-request-limits.
For info on API request limiting see: https://apihelp.surveygizmo.com/help/api-request-limits
```php
//set max retries of requests to 10, when request is rate limited it will be retried after 5 seconds.
\SurveyGizmo\ApiRequest::setRepeatRateLimitedRequest(10);
Expand Down Expand Up @@ -209,7 +225,15 @@ Imagination, Determination and Common Sense!
```php
require_once "<LIBRARY_PATH>/SurveyGizmoAutoLoader.php";
```
3. Authenticate using your SurveyGizmo [API Key and Secret](https://apihelp.surveygizmo.com/help/article/link/authentication).
3. Configure your SurveyGizmo API data center (Defaults to US data center if not called)
```php
try {
\SurveyGizmo\SurveyGizmoAPI::setRegion('EU');
} catch (Exception $e) {
die('Region not available');
}
```
4. Authenticate using your SurveyGizmo [API Key and Secret](https://apihelp.surveygizmo.com/help/article/link/authentication).
```php

try {
Expand Down
25 changes: 25 additions & 0 deletions SurveyGizmo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use SurveyGizmo\Helpers\SurveyGizmoException;
use SurveyGizmo\Resources\Account;
use SurveyGizmo\ApiRequest;

/**
* Simple class to store auth credentials for SG API and authorize API use.
Expand Down Expand Up @@ -37,6 +38,30 @@ public static function auth($api_key, $api_secret, $bypass_test = false)
}
}

/**
* Changes hostname to point API calls to international data centers
* @access public
* @param string $region what region your account resides on (US/CA/EU)
* @return void
*/
public static function setRegion($region = 'US') {
switch (strtoupper($region)) {
case 'US':
$region_base_url = 'restapi.surveygizmo.com/v5';
break;
case 'EU':
$region_base_url = 'restapi.surveygizmo.eu/v5';
break;
case 'CA':
$region_base_url = 'restapica.surveygizmo.com/v5';
break;
default:
throw new SurveyGizmoException('Invalid region supplied: ' . $region, SurveyGizmoException::NOT_SUPPORTED);
}

ApiRequest::setBaseURI($region_base_url);
}

/**
* Get authorization credentials for subsequent api calls
* @access public
Expand Down
32 changes: 15 additions & 17 deletions Tests/ApiRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public function testBuildPayloadForeignCharacters()
public function testbuildURI()
{

//$expected = "trunk.qa.devo.boulder.sgizmo.com/services/rest/v4this is a string.json?api_token=testing&api_token_secret=sauce&_method=GET";

$test = new ApiRequest();
$test->data = new \stdClass();
$test->path = 'this is a string';
Expand All @@ -91,20 +89,20 @@ public function testbuildURI()
}

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, array($parameters));
}
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, array($parameters));
}

}

0 comments on commit dbad47a

Please sign in to comment.