Chilkat Online Tools

GetSampledRequests Objective-C Example

AWS WAFV2

#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <NSString.h>

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
authAws.AccessKey = @"AWS_ACCESS_KEY";
authAws.SecretKey = @"AWS_SECRET_KEY";

// Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
authAws.Region = @"us-west-2";
authAws.ServiceName = @"wafv2";
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
[rest SetAuthAws: authAws];

// URL: https://wafv2.us-west-2.amazonaws.com/
// Use the same region as specified above.
success = [rest Connect: @"wafv2.us-west-2.amazonaws.com" port: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

// The following code creates the JSON request body.
// The JSON created by this code is shown below.

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateInt: @"MaxItems" value: [NSNumber numberWithInt: 123]];
[json UpdateString: @"RuleMetricName" value: @"string"];
[json UpdateString: @"Scope" value: @"string"];
[json UpdateInt: @"TimeWindow.EndTime" value: [NSNumber numberWithInt: 123]];
[json UpdateInt: @"TimeWindow.StartTime" value: [NSNumber numberWithInt: 123]];
[json UpdateString: @"WebAclArn" value: @"string"];

// The JSON request body created by the above code:

// {
//   "MaxItems": number,
//   "RuleMetricName": "string",
//   "Scope": "string",
//   "TimeWindow": {
//     "EndTime": number,
//     "StartTime": number
//   },
//   "WebAclArn": "string"
// }

[rest AddHeader: @"Content-Type" value: @"application/x-amz-json-1.1"];
[rest AddHeader: @"X-Amz-Target" value: @"AWSWAF_20190729.GetSampledRequests"];

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/" requestBody: sbRequestBody responseBody: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];
NSLog(@"%@%d",@"response status code = ",respStatusCode);
if (respStatusCode != 200) {
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",rest.ResponseHeader);
    NSLog(@"%@",@"Response Body:");
    NSLog(@"%@",[sbResponseBody GetAsString]);
    return;
}

CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];

// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.

// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

NSString *Action = 0;
NSString *FailureReason = 0;
int ResponseCode;
int SolveTimestamp;
NSString *ClientIP = 0;
NSString *Country = 0;
NSString *HTTPVersion = 0;
NSString *Method = 0;
NSString *URI = 0;
int ResponseCodeSent;
NSString *RuleNameWithinRuleGroup = 0;
int Timestamp;
int Weight;
int j;
int count_j;
NSString *Name = 0;
NSString *Value = 0;

int PopulationSize = [[jResp IntOf: @"PopulationSize"] intValue];
int EndTime = [[jResp IntOf: @"TimeWindow.EndTime"] intValue];
int StartTime = [[jResp IntOf: @"TimeWindow.StartTime"] intValue];
int i = 0;
int count_i = [[jResp SizeOfArray: @"SampledRequests"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    Action = [jResp StringOf: @"SampledRequests[i].Action"];
    FailureReason = [jResp StringOf: @"SampledRequests[i].CaptchaResponse.FailureReason"];
    ResponseCode = [[jResp IntOf: @"SampledRequests[i].CaptchaResponse.ResponseCode"] intValue];
    SolveTimestamp = [[jResp IntOf: @"SampledRequests[i].CaptchaResponse.SolveTimestamp"] intValue];
    ClientIP = [jResp StringOf: @"SampledRequests[i].Request.ClientIP"];
    Country = [jResp StringOf: @"SampledRequests[i].Request.Country"];
    HTTPVersion = [jResp StringOf: @"SampledRequests[i].Request.HTTPVersion"];
    Method = [jResp StringOf: @"SampledRequests[i].Request.Method"];
    URI = [jResp StringOf: @"SampledRequests[i].Request.URI"];
    ResponseCodeSent = [[jResp IntOf: @"SampledRequests[i].ResponseCodeSent"] intValue];
    RuleNameWithinRuleGroup = [jResp StringOf: @"SampledRequests[i].RuleNameWithinRuleGroup"];
    Timestamp = [[jResp IntOf: @"SampledRequests[i].Timestamp"] intValue];
    Weight = [[jResp IntOf: @"SampledRequests[i].Weight"] intValue];
    j = 0;
    count_j = [[jResp SizeOfArray: @"SampledRequests[i].Labels"] intValue];
    while (j < count_j) {
        jResp.J = [NSNumber numberWithInt: j];
        Name = [jResp StringOf: @"SampledRequests[i].Labels[j].Name"];
        j = j + 1;
    }

    j = 0;
    count_j = [[jResp SizeOfArray: @"SampledRequests[i].Request.Headers"] intValue];
    while (j < count_j) {
        jResp.J = [NSNumber numberWithInt: j];
        Name = [jResp StringOf: @"SampledRequests[i].Request.Headers[j].Name"];
        Value = [jResp StringOf: @"SampledRequests[i].Request.Headers[j].Value"];
        j = j + 1;
    }

    j = 0;
    count_j = [[jResp SizeOfArray: @"SampledRequests[i].RequestHeadersInserted"] intValue];
    while (j < count_j) {
        jResp.J = [NSNumber numberWithInt: j];
        Name = [jResp StringOf: @"SampledRequests[i].RequestHeadersInserted[j].Name"];
        Value = [jResp StringOf: @"SampledRequests[i].RequestHeadersInserted[j].Value"];
        j = j + 1;
    }

    i = i + 1;
}

// A sample JSON response body parsed by the above code:

// {
//   "PopulationSize": number,
//   "SampledRequests": [
//     {
//       "Action": "string",
//       "CaptchaResponse": {
//         "FailureReason": "string",
//         "ResponseCode": number,
//         "SolveTimestamp": number
//       },
//       "Labels": [
//         {
//           "Name": "string"
//         }
//       ],
//       "Request": {
//         "ClientIP": "string",
//         "Country": "string",
//         "Headers": [
//           {
//             "Name": "string",
//             "Value": "string"
//           }
//         ],
//         "HTTPVersion": "string",
//         "Method": "string",
//         "URI": "string"
//       },
//       "RequestHeadersInserted": [
//         {
//           "Name": "string",
//           "Value": "string"
//         }
//       ],
//       "ResponseCodeSent": number,
//       "RuleNameWithinRuleGroup": "string",
//       "Timestamp": number,
//       "Weight": number
//     }
//   ],
//   "TimeWindow": {
//     "EndTime": number,
//     "StartTime": number
//   }
// }