Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Add the following input and output parameters to the server routine:

          * Define the input fields

     Field_Map For(*INPUT) Field(#SourceText)
Field_Map For(*INPUT) Field(#TargetLanguage)
Field_Map For(*INPUT) Field(#SourceLanguage)

* Define the output fields

     Field_Map For(*OUTPUT) Field(#OK)
Field_Map For(*OUTPUT) Field(#TranslatedText)
Field_Map For(*OUTPUT) Field(#ErrorCode)
Field_Map For(*OUTPUT) Field(#ErrorMessage)

Declare the following variables at the beginning of the server routine.

     Define_Com Class(#XPRIM_HttpRequest) Name(#HttpRequest)
Define_Com Class(#XPRIM_UriBuilder) Name(#Url)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)

Populate the URL host, path, and query string.

     #Url.SetScheme( 'https' )
#Url.SetHost( 'www.googleapis.com' )
#Url.SetPath( '/language/translate/v2' )
#Url.AddQueryString( 'q' #SourceText )
#Url.AddQueryString( 'target' #TargetLanguage )
#Url.AddQueryString( 'format' 'text' )
#Url.AddQueryString( 'source' #SourceLanguage )
#Url.AddQueryString( 'key' '<your-api-key-from-previous-step>' )

Execute the request specified by the URL:

     #HttpRequest.DoGet Url(#Url)

Now you need to get the translated text from the JSON response.

Recall from the previous step (using Postman) what the JSON for positive response looks like:

  

The path for the "translated text" value is:

     data/translations/1/translatedText

Don't forget that you need to check first if your request is successful, by checking if you get any response from the server (IsSucessfulRequest property), and if you get a positive response (IsSuccessHttpStatusCode)

...

The path to the error message:

     error/message

Here is the code that extracts the translated text from the JSON response:

* Check if the server returned a response

     If (#HttpRequest.Response.IsSuccessfulRequest)

         * Feed the HTTP Response to the JsonReader object  

        #Reader.SetSourceHttpResponse Httpresponse(#HttpRequest.Response)

     * Check if we get a positive response from the server   If

       If (#HttpRequest.Response.IsSuccessHttpStatusCode)

              * Get the result (translated) text      #TranslatedText

          #TranslatedText := #Reader.ReadStringWithPath( 'data/translations/1/translatedText' )

...

          #OK := True

...

       Else

             * Get the error code & error message from the JSON response      #ErrorCode

          #ErrorCode := #Reader.ReadStringWithPath( 'error/code' )

...

          #ErrorMessage := #Reader.ReadStringWithPath( 'error/message' )

...

          #OK := False

...

      Endif
Else

   * We didn't get any response from the server
   * Get the transport error from the Response object   #OK

      #OK := False
   #ErrorCode := #HttpRequest.Response.ErrorCode

...

      #ErrorMessage := #HttpRequest.Response.ErrorMessage
Endif

Next: Creating a Webpage to Test Your Server Module