Address Validation International C# Rest Code Snippet

//Set the primary and backup URLs as necessary
AVIResponse.AddressInfoResponse wsresponse = httpGet(primaryurl);
  
//checks if a response was returned from the service, uses the backup url if response is null or a fatal error occured.
if (wsresponse == null || (wsresponse.Error != null && wsresponse.Error.TypeCode == "3"));
{
    wsresponse = httpGet(backupurl);
}     
if (wsresponse.Error != null)
{
    ProcessErrorResponse(wsresponse.Error);
}
else
{
    ProcessSuccessfulResponse(wsresponse);
}

Address Validation International Java Rest Code Snippet

JSONObject results = RestClient(mainURL); 
if (ErrorMessages != null || (results.getJSONObject("AddressInfoResponse").has("Error") && results.getJSONObject("AddressInfoResponse").getJSONObject("Error").get("TypeCode") == "3")) {
    // BACKUP
    results = RestClient(backupURL);
}
return results;

Address Validation International PHP Rest Code Snippets

$URL = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=".urlencode($Address1)."&Address2=".urlencode($Address2)."&Address3=".urldecode($Address3)."&Address4=".urldecode($Address4)."&Address5=".urlencode($Address5)."&Locality=".urlencode($Locality)."&AdministrativeArea=".urldecode($AdministrativeArea)."&PostalCode=".urldecode($PostalCode)."&Country=".urlencode($Country)."&OutputLanguage=".urldecode($OutputLanguage)."&LicenseKey=".urlencode($LicenseKey)."&Format=xml";
//use backup url once given purchased license key
$backupURL = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=".urlencode($Address1)."&Address2=".urlencode($Address2)."&Address3=".urldecode($Address3)."&Address4=".urldecode($Address4)."&Address5=".urlencode($Address5)."&Locality=".urlencode($Locality)."&AdministrativeArea=".urldecode($AdministrativeArea)."&PostalCode=".urldecode($PostalCode)."&Country=".urlencode($Country)."&OutputLanguage=".urldecode($OutputLanguage)."&LicenseKey=".urlencode($LicenseKey)."&Format=xml";
try{
    // Get cURL resource
    $curl = curl_init();
    curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $URL, CURLOPT_USERAGENT => 'Service Objects Geocoder Canada'));
    curl_setopt($curl, CURLOPT_TIMEOUT, 50); //timeout in seconds
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
      
    // Close request to clear up some resources
    if($resp == false)
    {
        echo "IN back up block";
        curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $backupURL, CURLOPT_USERAGENT => 'Service Objects Geocoder Canada'));
        curl_setopt($curl, CURLOPT_TIMEOUT, 50); //timeout in seconds
        // Send the request & save response to $resp
        $resp = curl_exec($curl);
        if($resp == false)
        {
            echo "<b> Both rest calls failed </b>";
            curl_close($curl);
            return;
        }
      
      
    }
curl_close($curl);
$outputs = new SimpleXMLElement($resp);

Address Validation International RoR Rest Code Snippets

#Set Primary and Backup URLs as needed. This method encodes and standardizes the URI to pass to the REST service.
primaryURL = URI.encode("https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=" + address1 + "&Address2=" + address2 + "&Address3=" + address3 + "&Address4=" + address4 + "&Address5=" + address5 + "&Locality=" + locality + "&AdministrativeArea=" + administrativearea + "&PostalCode=" + postalcode + "&Country=" + country + "&OutputLanguage=" + outputlanguage + "&LicenseKey=" + licensekey + "&format=" + format)
backupURL = URI.encode("https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=" + address1 + "&Address2=" + address2 + "&Address3=" + address3 + "&Address4=" + address4 + "&Address5=" + address5 + "&Locality=" + locality + "&AdministrativeArea=" + administrativearea + "&PostalCode=" + postalcode + "&Country=" + country + "&OutputLanguage=" + outputlanguage + "&LicenseKey=" + licensekey + "&format=" + format)
#These are set to access the hash that is returned
@aviresponse ="AddressInfoResponse"
@aviinfo = "AddressInfo"
@avierror = "Error"
  #Begins the call the RESTful web service
begin
  response = HTTParty.get(primaryURL, timeout: default_timeout)
  #processes the response to display to the screen
    
  #Passes the response returned from HTTParty and processes them depending on the results
  processresults(response)
    
 rescue StandardError => e
        begin
        #uses the backupURl in the event that the service encountered an error
        response = HTTParty.get(backupURL, timeout: default_timeout)
      
    #processes the response returned from using the backupURL
        processresults(response)
    #If the backup url railed this will raise an error and display the
    #error message returned from the HTTParty gem.
        rescue StandardError => error
            @status = response
            @displaydata = {"Error" => "A Big Error Occured"}
        end
end
  
def processresults(response)  
  
        #Checks to see if a fatal error was returned by the service.
        if !response[@aviresponse][@avierror].nil? && response[@aviresponse][@avierror]["TypeCode"]  == "3"
                @status = "A Fatal Error Has Occured"
                @displaydata = response[@aviresponse][@avierror]
  
        #Processes Error Response from soap Client    
        elsif !response[@aviresponse][@avierror].nil?
            @status = "An Error Has Occured"
            @displaydata = response[@aviresponse][@avierror]
  
        #Processes Valid response from soap client
        else
            @status = "Below are the Results"
            @infoComponents= response[@aviresponse][@aviinfo]["InformationComponents"]["InformationComponent"]
            @displaydata = response[@aviresponse][@aviinfo].except!("InformationComponents")
  
        end
end

Address Validation International Python Code Snippet

#The Requests package allows the user to format the path parameters like so instead of having to manually insert them into the URL
inputs = {'Address1': mAddress1, 'Address2': mAddress2, 'Address3': mAddress3, 'Address4': mAddress4, 'Address5': mAddress5, 'Locality': mLocality, 'AdministrativeArea': mAdministrativeArea, 'PostalCode': mPostalCode, 'Country': mCountry, 'OutputLanguage': mOutputLanguage, 'LicenseKey': mLicenseKey, 'format': "XML"}
try:
    result = requests.get(primaryURL, params=inputs)
    #Parses the XML response from the service into a python dictionary type
    outputs = xmltodict.parse(result.content)
  
    #checks the output for Errors and displays the info accordingly
    if 'Error' in outputs['AddressInfoResponse']:
        #Handles Error Response
    else:
        #Handles Successful Response
#Attempts to use the backupURL if the call to the primary URL failed
except:
    try:
        result = requests.get(backupURL, params=inputs)
        #Parses the XML response from the service into a python dictionary type
        outputs = xmltodict.parse(result.content)
        #checks the output for Errors and displays the info accordingly
        if 'Error' in outputs['AddressInfoResponse']:
            #Handles Error Response
        else:
            #Handles Successful Response

Address Validation International ColdFusion Rest Snippet

<!--Makes Request to web service --->
<cfIf isDefined("form.Action") AND Action neq "" >
    <cftry>
        <cfset primaryURL = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=#Address1#&Address2=#Address2#&Address3=#Address3#&Address4=#Address4#&Address5=#Address5#&Locality=#Locality#&AdministrativeArea=#AdministrativeArea#&PostalCode=#PostalCode#&Country=#Country#&OutputLanguage=#OutputLanguage#&LicenseKey=#LicenseKey#&format=XML">
        <cfhttp url="#primaryURL#"
        method="get"
        result="response">
        <cfset outputs = XmlParse(response.FileContent)>
        <cfcatch>
            <cftry>
                <cfset backupURL = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=#Address1#&Address2=#Address2#&Locality=#Locality#&AdministrativeArea=#AdministrativeArea#&PostalCode=#PostalCode#&Country=#Country#&Language=#Language#&LicenseKey=#LicenseKey#&format=XML">
                <cfhttp url="#backupURL#"
                method="get"
                result="response">
                <cfset outputs = XmlParse(response.FileContent)>            
                <cfcatch >
                    <cfoutput >
                        The Following Error Occured: #response.StatusCode#
                    </cfoutput>
                </cfcatch>
            </cftry>
        </cfcatch>
    </cftry>
</cfif>

Address Validation International VB Rest Code Snippet

Try
    'encodes the URLs for the get Call. Set the primary and back urls as necessary
    Dim primaryurl As String = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=" + address1 + "&Address2=" + address2 + "&Address3=" + address3 + "&Address4=" + address4 + "&Address5=" + address5 + "&Locality=" + locality + "&AdministrativeArea=" + administrativearea + "&PostalCode=" + postalcode + "&Country=" + country + "&OutputLanguage=" + outputlanguage + "&LicenseKey=" + licensekey + "&format=" + format
    Dim backupurl As String = "https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=" + address1 + "&Address2=" + address2 + "&Address3=" + address3 + "&Address4=" + address4 + "&Address5=" + address5 + "&Locality=" + locality + "&AdministrativeArea=" + administrativearea + "&PostalCode=" + postalcode + "&Country=" + country + "&OutputLanguage=" + outputlanguage + "&LicenseKey=" + licensekey + "&format=" + format
    Dim wsresponse As AVIResponse.AddressInfoResponse = httpGet(primaryurl)
  
    'checks if a response was returned from the service, uses the backup url if response is null or a fatal error occured.
    If wsresponse Is Nothing OrElse (wsresponse.[Error] IsNot Nothing AndAlso wsresponse.[Error].TypeCode = "3") Then
        wsresponse = httpGet(backupurl)
    End If
    If wsresponse.[Error] IsNot Nothing Then
        ProcessErrorResponse(wsresponse.[Error])
    Else
        ProcessSuccessfulResponse(wsresponse)
  
    End If
Catch ex As Exception
    'Displays the relevant error mesasge if both backup and primary urls failed.
    StatusLabel.Text = ex.Message
    StatusLabel.Visible = True
End Try

Address Validation International TSQL Rest Code Snippet

--If a production key is purchased, this will execute the failover
IF @isLiveKey = 1
BEGIN
    SET @sUrl = 'https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=' + @address1 + '&Address2=' + @address2 + '&Address3=' + @address3 + '&Address4=' + @address4 + '&Address5=' + @address5 + '&Locality=' + @locality + '&AdministrativeArea=' + @administrativearea + '&PostalCode=' + @postalcode + '&Country=' + @country + '&OutputLanguage=' + @outputlanguage + '&LicenseKey=' + @key
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
    EXEC sp_OAMethod @obj, 'Open', NULL, 'Get', @sUrl, false
    EXEC sp_OAMethod @obj, 'send'
    EXEC sp_OAGetProperty @obj, 'responseText', @response OUT
              
    --Checks the Response for a fatal error or if null.
    IF @response IS NULL
    BEGIN
        SET @sBackupUrl = 'https://trial.serviceobjects.com/avi/api.svc/GetAddressInfo?Address1=' + @address1 + '&Address2=' + @address2 + '&Address3=' + @address3 + '&Address4=' + @address4 + '&Address5=' + @address5 + '&Locality=' + @locality + '&AdministrativeArea=' + @administrativearea + '&PostalCode=' + @postalcode + '&Country=' + @country + '&OutputLanguage=' + @outputlanguage + '&LicenseKey=' + @key
        EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
        EXEC sp_OAMethod @obj, 'Open', NULL, 'Get', @sBackupUrl, false
        EXEC sp_OAMethod @obj, 'send'
        EXEC sp_OAGetProperty @obj, 'responseText', @response OUT
    END
END

Address Validation International NodeJS REST Code Snippet

//Set backup and primary URL as necessary
var primaryUrl = 'https://trial.serviceobjects.com/avi/api.svc/xml/GetAddressInfo?Address1=' + Address1 + '&Address2='+ Address2 + '&Address3=' + Address3 + '&Address4=' + Address4 + '&Address5=' + Address5 + '&Locality=' + Locality +'&AdministrativeArea=' + AdministrativeArea +'&PostalCode=' + PostalCode + '&Country=' + Country + '&OutputLanguage=' + OutputLanguage +'&LicenseKey=' + LicenseKey;
var backupUrl = 'https://trial.serviceobjects.com/avi/api.svc/xml/GetAddressInfo?Address1=' + Address1 + '&Address2='+ Address2 + '&Address3=' + Address3 + '&Address4=' + Address4 + '&Address5=' + Address5 + '&Locality=' + Locality +'&AdministrativeArea=' + AdministrativeArea +'&PostalCode=' + PostalCode + '&Country=' + Country + '&OutputLanguage=' + OutputLanguage +'&LicenseKey=' + LicenseKey;
  
  
var req = http.get(primaryUrl, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (results) {
  
        var parser = require('xml2js').Parser({explicitArray: false,ignoreAttrs: true});
        parser.parseString(results, function (err, outputs) {
            if (outputs.AddressInfoResponse.Error !=  null)
            {
                //Indicates a Fatal error has occured. If this happens, the logic will then failover to the backup url
                if (outputs.AddressInfoResponse.Error.TypeCode == "3")
                {
                    var backupReq = http.get(backupUrl, function(backupRes) {
                        backupRes.setEncoding('utf8');
                        backupRes.on('data', function (results) {
  
                                var parser = require('xml2js').Parser({explicitArray: false,ignoreAttrs: true});
                                parser.parseString(results, function (err, outputs) {
  
                                    console.log("Backup Call Was Used.");
                                    response.end(JSON.stringify(outputs , null, 3));
                                });
                            });
                        });
                }
                else
                {
                    //Will Display the JSON Formatted Error Response here
                    response.end(JSON.stringify(outputs, null, 3));
                    return;
                }
            }
            else
            {
                //Will Display the JSON Formatted Valid Response here
                response.end(JSON.stringify(outputs, null, 3));
                return;
            }
        });
    });
});