Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Amazon Translate TextDemonstrates how to use the AWS Translate service to translate text from one language to another.
#include <C_CkRest.h> #include <C_CkAuthAws.h> #include <C_CkJsonObject.h> #include <C_CkStringBuilder.h> void ChilkatSample(void) { HCkRest rest; BOOL success; HCkAuthAws authAws; BOOL bTls; int port; BOOL bAutoReconnect; HCkJsonObject json; HCkStringBuilder sbRequestBody; HCkStringBuilder sbResponseBody; int respStatusCode; HCkJsonObject jsonResponse; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. rest = CkRest_Create(); authAws = CkAuthAws_Create(); CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY"); CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY"); CkAuthAws_putRegion(authAws,"us-west-2"); CkAuthAws_putServiceName(authAws,"translate"); CkRest_SetAuthAws(rest,authAws); // URL: https://translate.us-west-2.amazonaws.com/ bTls = TRUE; port = 443; bAutoReconnect = TRUE; success = CkRest_Connect(rest,"translate.us-west-2.amazonaws.com",port,bTls,bAutoReconnect); if (success != TRUE) { printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest)); printf("%s\n",CkRest_lastErrorText(rest)); CkRest_Dispose(rest); CkAuthAws_Dispose(authAws); return; } // Translate text from English to German json = CkJsonObject_Create(); CkJsonObject_UpdateString(json,"SourceLanguageCode","en"); CkJsonObject_UpdateString(json,"TargetLanguageCode","de"); CkJsonObject_UpdateString(json,"Text","This is the text to be translated"); CkRest_AddHeader(rest,"Content-Type","application/x-amz-json-1.1"); CkRest_AddHeader(rest,"X-Amz-Target","AWSShineFrontendService_20170701.TranslateText"); sbRequestBody = CkStringBuilder_Create(); CkJsonObject_EmitSb(json,sbRequestBody); sbResponseBody = CkStringBuilder_Create(); success = CkRest_FullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody); if (success != TRUE) { printf("%s\n",CkRest_lastErrorText(rest)); CkRest_Dispose(rest); CkAuthAws_Dispose(authAws); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbRequestBody); CkStringBuilder_Dispose(sbResponseBody); return; } respStatusCode = CkRest_getResponseStatusCode(rest); if (respStatusCode >= 400) { printf("Response Status Code = %d\n",respStatusCode); printf("Response Header:\n"); printf("%s\n",CkRest_responseHeader(rest)); printf("Response Body:\n"); printf("%s\n",CkStringBuilder_getAsString(sbResponseBody)); CkRest_Dispose(rest); CkAuthAws_Dispose(authAws); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbRequestBody); CkStringBuilder_Dispose(sbResponseBody); return; } jsonResponse = CkJsonObject_Create(); CkJsonObject_LoadSb(jsonResponse,sbResponseBody); CkJsonObject_putEmitCompact(jsonResponse,FALSE); printf("%s\n",CkJsonObject_emit(jsonResponse)); // { // "SourceLanguageCode": "en", // "TargetLanguageCode": "de", // "TranslatedText": "Dies ist der zu bersetzende Text" // } printf("%s\n",CkJsonObject_stringOf(jsonResponse,"TranslatedText")); CkRest_Dispose(rest); CkAuthAws_Dispose(authAws); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbRequestBody); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jsonResponse); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.