Skip to content

Commit

Permalink
Enable return ServiceContext
Browse files Browse the repository at this point in the history
Enable set Log location, MinorVersion and disable Log
Fix Report service bug
Fix Exception class not found
  • Loading branch information
hlu2 committed Apr 20, 2017
1 parent 41ab489 commit 8cac984
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Core/Configuration/LocalConfigReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function ReadConfigurationFromFile($filePath, $OAuthOption = CoreC

}

public static function ReadConfigurationFromParameters($OAuthConfig, $baseUrl, $defaultLoggingLocation = CoreConstatnts::DEFAULT_LOGGINGLOCATION, $minorVersion = 3){
public static function ReadConfigurationFromParameters($OAuthConfig, $baseUrl, $defaultLoggingLocation = CoreConstants::DEFAULT_LOGGINGLOCATION, $minorVersion = 3){
$ippConfig = new IppConfiguration();
try {
//Set OAuth
Expand Down
6 changes: 3 additions & 3 deletions src/Core/HttpClients/SyncRestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function GetResponseSyncRest($requestParameters, $requestBody, $oauthRequ
// });
}
}
catch (Exception $webException)
catch (\Exception $webException)
{
// System.Net.HttpWebRequest.Abort() was previously called.-or- The time-out
// period for the request expired.-or- An error occurred while processing the request.
Expand Down Expand Up @@ -169,7 +169,7 @@ private function GetOAuthResponseHeaders($oauth)
}
}
}
catch(Exception $e)
catch(\Exception $e)
{
throw new \Exception("Error on GetOAuthResponseHeaders.");
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public function GetResponse($requestParameters, $requestBody, $oauthRequestUri)

//When call OAuth, check if the user has OAuth installed.
if (!extension_loaded('OAuth')) {
throw new Exception('The PHP exention OAuth 1.2.3 must be installed to use this library.');
throw new \Exception('The PHP exention OAuth 1.2.3 must be installed to use this library.');
}


Expand Down
84 changes: 68 additions & 16 deletions src/Core/ServiceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,27 @@ public static function ConfigureFromPassedArray(array $settings){
}

if (!isset($settings['consumerKey'])) {
throw new Exception("'consumerKey' must be provided");
throw new \Exception("'consumerKey' must be provided");
}

if (!isset($settings['consumerSecret'])) {
throw new Exception("'consumerSecret' must be provided");
throw new \Exception("'consumerSecret' must be provided");
}

if (!isset($settings['accessTokenKey'])) {
throw new Exception("'accessTokenKey' must be provided");
throw new \Exception("'accessTokenKey' must be provided");
}

if (!isset($settings['accessTokenSecret'])) {
throw new Exception("'accessTokenSecret' must be provided");
throw new \Exception("'accessTokenSecret' must be provided");
}

if (!isset($settings['QBORealmID'])) {
throw new Exception("'QBORealmID' must be provided");
throw new \Exception("'QBORealmID' must be provided");
}

if (!isset($settings['baseUrl'])) {
throw new Exception("'baseUrl' must be provided");
throw new \Exception("'baseUrl' must be provided");
}


Expand Down Expand Up @@ -225,20 +225,72 @@ public function disableSSlCheck() {
* Currently the Object serailziation and deserilization only supports XML format.
*/
public function useXml(){
$this->$ippConfig->Message->Request->CompressionFormat = CompressionFormat::None;
$this->$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
$this->$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Xml;
$this->$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Xml;
$this->IppConfiguration->Message->Request->CompressionFormat = CompressionFormat::None;
$this->IppConfiguration->Message->Response->CompressionFormat = CompressionFormat::None;
$this->IppConfiguration->Message->Request->SerializationFormat = SerializationFormat::Xml;
$this->IppConfiguration->Message->Response->SerializationFormat = SerializationFormat::Xml;
}

/**
* Currently the Object serailziation and deserilization only supports XML format.
* To be Supported @Hao
* To be Supported, Currently it is only a place holder @Hao
*/
public function useJson(){
$this->$ippConfig->Message->Request->CompressionFormat = CompressionFormat::None;
$this->$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
$this->$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Json;
$this->$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Json;
private function useJson(){
$this->IppConfiguration->Message->Request->CompressionFormat = CompressionFormat::None;
$this->IppConfiguration->Message->Response->CompressionFormat = CompressionFormat::None;
$this->IppConfiguration->Message->Request->SerializationFormat = SerializationFormat::Json;
$this->IppConfiguration->Message->Response->SerializationFormat = SerializationFormat::Json;
}

/**
* Disable Log the request and response to disk
*/
public function disableLog(){
try
{
$_ippConfigInstance = $this->getIppConfig();
LocalConfigReader::setupLogger($_ippConfigInstance, CoreConstants::DEFAULT_LOGGINGLOCATION, "FALSE");
}catch (\Exception $e){
throw new \Excpetion("Error in disable Log.");
}
}

/**
* Set a new Location for Log instead of the default Location
* @param $new_log_location
* The new log directory path. It is a directory, not a file
*/
public function setLogLocation($new_log_location){
try
{
$_ippConfigInstance = $this->getIppConfig();
LocalConfigReader::setupLogger($_ippConfigInstance, $new_log_location, "TRUE");
}catch (\Exception $e){
throw new \Excpetion("Error in setting up new Log Configuration: " . $new_log_location);
}
}

public function setMinorVersion($new_minor_version){
try
{
$_ippConfigInstance = $this->getIppConfig();
$_ippConfigInstance->minorVersion = $new_minor_version;
}catch (\Exception $e){
throw new \Excpetion("Error in setting up minor version.");
}
}

/**
* Return the Ipp Configuration for the ServiceContext for changing the settings
* @return IppConfiguration
* The IppConfiguration for current settings
*/
private function getIppConfig(){
$_ippConfiguration = $this->IppConfiguration;
if(isset($_ippConfiguration)){
return $_ippConfiguration;
}else{
throw new \Excpetion("Return Null or Empty IppConfiguration.");
}
}
}
68 changes: 65 additions & 3 deletions src/DataService/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,78 @@ private function setupServiceContext($serviceContext){
$this->serviceContext = $serviceContext;
}

/**
* Return the ServiceContext of this DataService
*
* @return ServiceContext
*/
public function getServiceContext(){
$_ServiceContext = $this->serviceContext;
if(isset($_ServiceContext)){
return $_ServiceContext;
}else{
throw new \Exception("Trying to Return an Empty Service Context.");
}
}

private function setupRestHandler($serviceContext){
$this->restHandler = new SyncRestHandler($serviceContext);
}

/**
* PHP SDK currently only support XML for Object Serialization and Deserialization
* PHP SDK currently only support XML for Object Serialization and Deserialization, except for Report Service
*/
public function useXml(){
$_ServiceContext = $this->getServiceContext();
$_ServiceContext->useXml();
$this->updateServiceContextSettingsForOthers($_ServiceContext);
}

/**
* PHP SDK currently only support XML for Object Serialization and Deserialization, except for Report Service
*/
public function useJson(){
$_ServiceContext = $this->getServiceContext();
$_ServiceContext->useJson();
$this->updateServiceContextSettingsForOthers($_ServiceContext);
}

/**
* Set a new directory for request and response log
*/
public function setLogLocation($new_log_location){
$_ServiceContext = $this->getServiceContext();
$_ServiceContext->setLogLocation($new_log_location);
$this->updateServiceContextSettingsForOthers($_ServiceContext);
}

/**
* Set a new Minor Version
* @param $new_minor_version
* The new minor version that passed
*/
public function setMinorVersion($newMinorVersion){
$_ServiceContext = $this->getServiceContext();
$_ServiceContext->setMinorVersion($newMinorVersion);
$this->updateServiceContextSettingsForOthers($_ServiceContext);
}

/**
* Disable the logging function
*
*/
public function disableLog(){
$_ServiceContext = $this->getServiceContext();
$_ServiceContext->disableLog();
$this->updateServiceContextSettingsForOthers($_ServiceContext);
}

public function updateServiceContextSettingsForOthers($_ServiceContext){
$this->setupSerializers();
$this->useMinorVersion();
$this->setupRestHandler($_ServiceContext);
}

/**
* New Static function for static Reading from Config or Passing Array
Expand All @@ -141,14 +203,14 @@ public static function Configure($settings){
if(is_array($settings)){
$ServiceContextFromPassedArray = ServiceContext::ConfigureFromPassedArray($settings);
if (!isset($ServiceContextFromPassedArray)) {
throw new Exception('Construct ServiceContext from OAuthSettigs failed.');
throw new \Exception('Construct ServiceContext from OAuthSettigs failed.');
}
$DataServiceInstance = new DataService($ServiceContextFromPassedArray);
return $DataServiceInstance;
}else if(is_string($settings)){
$ServiceContextFromFile = ServiceContext::ConfigureFromLocalFile($settings);
if (!isset($ServiceContextFromFile)) {
throw new Exception('Construct ServiceContext from File failed.');
throw new \Exception('Construct ServiceContext from File failed.');
}
$DataServiceInstance = new DataService($ServiceContextFromFile);
return $DataServiceInstance;
Expand Down
10 changes: 8 additions & 2 deletions src/ReportService/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use QuickBooksOnline\API\Core\Configuration\OperationControlList;
use QuickBooksOnline\API\DataService\Batch;
use QuickBooksOnline\API\DataService\IntuitCDCResponse;
use QuickBooksOnline\API\Core\Http\Serialization\SerializationFormat;
use QuickBooksOnline\API\Core\HttpClients\SyncRestHandler;
use QuickBooksOnline\API\Core\ServiceContext;
use QuickbooksOnline\API\Core\CoreConstants;
use QuickBooksOnline\API\Core\HttpClients\RequestParameters;

class ReportService
{
Expand Down Expand Up @@ -825,7 +830,7 @@ public function __construct($serviceContext) {
$this->restHandler = new SyncRestHandler($serviceContext);

// Set the Service type to either QBO or QBD by calling a method.
$this->serviceContext->UseDataServices();
//$this->serviceContext->UseDataServices();
}

/**
Expand Down Expand Up @@ -1037,7 +1042,8 @@ public function executeReport($reportName){
CoreHelper::CheckNullResponseAndThrowException($responseBody);

try {
$responseBody = $this->modifyReportResponse($responseBody);
//The modification is no longer necessary
//$responseBody = $this->modifyReportResponse($responseBody);
$parsedResponseBody = $this->getResponseSerializer()->Deserialize($responseBody, TRUE);

} catch (Exception $e) {
Expand Down
22 changes: 17 additions & 5 deletions src/Utility/MetadataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use RuntimeException;
use UnexpectedValueException;
use InvalidArgumentException;
use QuickBooksOnline\API\Utility\Serialization\SimpleEntity;
use QuickBooksOnline\API\Utility\Serialization\ObjectEntity;
use QuickBooksOnline\API\Utility\Serialization\UnknownEntity;
use QuickBooksOnline\API\Utility\Serialization\AbstractEntity;
use QuickBooksOnline\API\Core\CoreConstants;
use QuickBooksOnline\API\Core\Http\Serialization\ObjectEntity;
use QuickBooksOnline\API\Core\Http\Serialization\SimpleEntity;
use QuickBooksOnline\API\Core\Http\Serialization\UnknownEntity;
use QuickBooksOnline\API\Core\Http\Serialization\AbstractEntity;


/**
* Extracts metadata for properties and decides which type is associated with this property
Expand Down Expand Up @@ -88,6 +90,7 @@ private function verifyVariableType($value)
// generate names
// try it
foreach ($this->generateObjectNames($value) as $name) {
$name = $this->addNameSpaceToPotentialClassName($name);
if(class_exists($name)) {
return new ObjectEntity($name);
}
Expand All @@ -96,6 +99,14 @@ private function verifyVariableType($value)
return new UnknownEntity($value);
}

private function addNameSpaceToPotentialClassName($name){
$name = trim($name);
$lists = explode('\\', $name);
$ippEntityName = end($lists);
$ippEntityName = CoreConstants::NAMEPSACE_DATA_PREFIX . $ippEntityName;
return $ippEntityName;
}

/**
* Returns order list of possible valid names of object type
* @param type $value
Expand Down Expand Up @@ -149,7 +160,8 @@ private function getClassNameFromPackagePath($string)
*/
private function getIntuitName($string)
{
return \PHP_CLASS_PREFIX.$string;
$string = trim($string);
return $string;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/_Samples/CustomerCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
'baseUrl' => "https://qbonline-e2e.api.intuit.com/"
));

// Prep Data Services
$serviceContext = $dataService->getServiceContext();

$dataService->setLogLocation("/Users/hlu2/Desktop/newFolderForLog");
$dataService->setMinorVersion("4");
$dataService->useXml();
$dataService->disableLog();
var_dump($serviceContext);

if (!$dataService)
exit("Problem while initializing DataService.\n");
Expand Down

0 comments on commit 8cac984

Please sign in to comment.