Processing REST and SOAP Request
Processing REST Request
The REST request processing has 3 steps:
1. Passing parameter keys and values in a URL
2. Making a call to ECS
3. Obtaining the response in XML format
SFXHTTPConnection class is used for REST connection. In the source code below, _http is an instance of SFXHTTPConnection class.
SFCError SGRNetworkDialogSet::Connect(Void) { SFCError error; // access by REST if ((error = _http.Open()) == SFERR_NO_ERROR) { if ((error = _http.SetMethod("GET")) == SFERR_NO_ERROR) // Make a call to ECS and get the response by callback function error = _http.Connect(_url, CALLBACK_FUNCTION(ConnectCallback)); } //notify if error occured if (error != SFERR_NO_ERROR) { _spp(NOTIFY_CONNECT_START, error, _reference); SGRSoftkeyToolbox::CloseStandardDialog(_dialog); _errorDialog.Open(HTTP_ERROR_TITLE, HTTP_ERROR_CODE1, DIALOG_OK, this); } return error; }
Below is the implementation of the ConnectCallback function.
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(SGRNetworkDialogSet, ConnectCallback, error) { TRACE("CALLBACK_IMPLEMENT_SFXHTTPCONNECTION"); if (error == SFERR_NO_ERROR) { // Get the stream reader error = _http.GetStreamReader(1024, &_reader); if (error == SFERR_NO_ERROR) // then get data using callback function error = _reader.Fetch(CALLBACK_FUNCTION(FetchCallback)); } // notify if error occurred if (error != SFERR_NO_ERROR) { _spp(NOTIFY_ERROR, error, _reference); SGRSoftkeyToolbox::CloseStandardDialog(_dialog); _errorDialog.Open(HTTP_ERROR_TITLE, HTTP_ERROR_CODE2, DIALOG_OK, this); } return; }
Below is the implementation of the FetchCallback function.
CALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(SGRNetworkDialogSet, FetchCallback, error) { TRACE("CALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER"); SFXBuffer _tempBuffer; if (error == SFERR_NO_ERROR) { // get a chunk of data _tempBuffer.SetSize(_reader.GetReadableSize()); _reader.Read(&_tempBuffer); // _string holds the returned text in XML format error = _receivedBuffer.Add(_tempBuffer); if (_reader.Ends() && _reader.GetReadableSize() == 0) { // if no data remains // close the connection _http.Close(); // close the dialog SGRSoftkeyToolbox::CloseStandardDialog(_dialog); // notify the application _connecting = false; //DBGPRINTF("Network connect end..."); _spp(NOTIFY_CONNECT_END, error, _reference); } else { // if there is remaining data, fetch again _reader.Fetch(CALLBACK_FUNCTION(FetchCallback)); } } else { _http.Close(); SGRSoftkeyToolbox::CloseStandardDialog(_dialog); _spp(NOTIFY_ERROR, error, _reference); _errorDialog.Open(HTTP_ERROR_TITLE, HTTP_ERROR_CODE3, DIALOG_OK, this); } return; }
Processing SOAP Request
The SFXSOAPRPC class is used for SOAP request. This class accesses to the ECS by making a remote procedure call.
The return data in XML format is then parsed by the SFXSOAPParser class.
Developers can access to the elements such as the header, the body, the fault of the parsed XML document tree through the instance of the SFXSOAPParser class.
The source code below shows how to use SFXSOAPRPC class to set parameters and get the body element of the XML document.
SFCError SGRSOAPDialogSet::SOAPConnect(Void) { SFCError error (SFERR_NO_ERROR); // set method name _rpc.SetMethodName(_params->operation); // set SOAP EncodingStyle _rpc.SetEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); // set target object URI _rpc.SetTargetObjectURI(TARGET_URI); // set AWSAccessKeyId error = _rpc.AddParameter("AWSAccessKeyId", SFXBuffer(SFXAnsiString(AMAZON_ID))); if (error != SFERR_NO_ERROR) return error; // // set method parameters // // set request parameter error = _rpc.AddParameter("Request", SFXBuffer::EmptyInstance(), SFXSOAPRPC::PARAMETER::UNKNOWN); if (error != SFERR_NO_ERROR) return error; // set SearchIndex parameter if (_params->_set_search_index) { error = _rpc.AddParameter("SearchIndex", SFXBuffer(_params->search_index)); if (error != SFERR_NO_ERROR) return error; } // set BrowseNode parameter if (_params->_set_browse_node) { error = _rpc.AddParameter("BrowseNode", SFXBuffer(_params->browse_node)); if (error != SFERR_NO_ERROR) return error; } // set ResponseGroup parameter error = _rpc.AddParameter("ResponseGroup", SFXBuffer(_params->response_group)); if (error != SFERR_NO_ERROR) return error; // set Keywords parameter if(_params->_set_keyword){ error = _rpc.AddParameter("Keywords", SFXBuffer(_params->keyword)); if (error != SFERR_NO_ERROR) return error; } // set ItemId parameter if(_params->_set_asin) { error = _rpc.AddParameter("ItemId", SFXBuffer( _params->asin)); if (error != SFERR_NO_ERROR) return error; } // make a remote procedure call // and get the body elemnet by SOAP_Notify function error = _rpc.Invoke(END_POINT_URL, SOAP_ACTION_URI, SOAP_Notify, this); return error; }
Below is the implementation of the SOAP_Notify function.
Void SGRSOAPDialogSet::SOAP_Notify(SFCError error, const SFXSOAPRPC::Params& result, const SFXSOAPRPC::FAULT& fault, SFXSOAPParserConstRef soap, VoidPtr reference) { static_cast<SGRSOAPDialogSetPtr>(reference)->On_SOAP_Notify(error, result, fault, soap); } Void SGRSOAPDialogSet::On_SOAP_Notify(SFCError error, const SFXSOAPRPC::Params& result, const SFXSOAPRPC::FAULT& fault, SFXSOAPParserConstRef soap ) { // if error occured if (error != SFERR_NO_ERROR) { _spp(NOTIFY_CONNECT_START, error, _reference); SGRSoftkeyToolbox::CloseStandardDialog(_dialog); _errorDialog.Open(SOAP_ERROR_TITLE, SOAP_ERROR_CODE, DIALOG_OK, this); } else// if success { _body = soap.GetBody(); // close dialog SGRSoftkeyToolbox::CloseStandardDialog(_dialog); // redrawing SFRApplication::GetInstance()->Invoke( SFXEvent(SREVT_RESPONDER_RENDER, SRP16_RENDER_INVOKE, false)); // notify the application when connection end _spp(NOTIFY_CONNECT_END, error, _reference); } return; }