Search code examples
c++visual-studiomechanicalturkaws-sdk-cpp

C++ SDK for MTurk GetAccountBalance API call


I'm a little new to C++ and haven't used SDKs at all. I'm trying to use C++ in Visual Studio 2013 recreate the sample API call provided on the developer page of the MTurk requester site that should return the account balance from the sandbox. Here's the example for python, copied off of the developer page:

    import boto3

    region_name = 'us-east-1'
    aws_access_key_id = 'YOUR_ACCESS_ID'
    aws_secret_access_key = 'YOUR_SECRET_KEY'

    endpoint_url = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'

    # Uncomment this line to use in production
    # endpoint_url = 'https://mturk-requester.us-east-1.amazonaws.com'

    client = boto3.client('mturk',
        endpoint_url = endpoint_url,
        region_name = region_name,
        aws_access_key_id = aws_access_key_id,
        aws_secret_access_key = aws_secret_access_key,
    )

    # This will return $10,000.00 in the MTurk Developer Sandbox
    print client.get_account_balance()['AvailableBalance']

So far, I've written this:

    #include <aws/core/Aws.h>
    #include <aws/core/auth/awscredentialsprovider.h>
    #include <aws/mturk-requester/MTurkClient.h>
    #include <aws/core/client/ClientConfiguration.h>
    #include <aws/mturk-requester/model/getaccountbalancerequest.h>
    #include <aws/mturk-requester/model/getaccountbalanceresult.h>
    #include <aws/core/utils/Outcome.h>
    #include <aws/core/http/HttpRequest.h>
    #include <iostream>

    using std::string;
    using std::cout; using std::endl;
    using namespace Aws::Auth;
    using namespace Aws::MTurk;
    using namespace Aws::MTurk::Model;
    using namespace Aws::Client;

    int main(){
        Aws::SDKOptions options;
        Aws::InitAPI(options);

        const char* access_key = "my key"; //I put the right keys in
        const char* secret_access_key = "my secret key";

        ClientConfiguration config;
        config.region = "us_east_1";
        config.endpointOverride = "https://mturk-requester-sandbox.us-east-1.amazonaws.com";
        //Not sure if I need this to force sandbox mode

        //Uncomment this line to use in production
        //config.endpointOverride = "https://mturk-requester.us-east-1.amazonaws.com"

        AWSCredentials creds;
        creds.SetAWSAccessKeyId(access_key);
        creds.SetAWSSecretKey(secret_access_key);
        creds.SetSessionToken("");

        MTurkClient requester = MTurkClient(creds, config);

        //not entirely sure what to do from here:
        GetAccountBalanceResult bal;
        //const GetAccountBalanceRequest& request = GetAccountBalanceRequest();
        //bal.SetAvailableBalance(); If I set it, it'll return what I set

        //This should return $10, 000.00 in the MTurk Developer Sandbox
        cout << bal.GetAvailableBalance() << endl; //outputs empty string
        cout << config.region << endl; //outputs us_east_1
        //requester.GetAccountBalance(request); I feel like this is what I should be using to get the balance?
        cout << creds.GetAWSAccessKeyId() << endl; //outputs the right key

        system("pause");

        Aws::ShutdownAPI(options);
        return 0;
    }

Like this, the code compiles and runs with the commented outputs. I've looked through AWS SDK for C++, the documentation, and also through the SDK .cpp and .h files themselves. Any help about using the SDK to send information to the website would help as well!


Solution

  • You could turn on log by something like: Aws::SDKOptions options; options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info; Also recommend put sdk related code into bracket: InitAPI { //your code here } ShutdownAPI

    And you need something like: GetAccountBalanceOutcome outcome = client->GetAccountBalance(request)

    APIs are always in the {ServiceName}Client.h. Check this S3 example https://github.com/singku/aws-sdk-cpp-test