update to new json/url options

This commit is contained in:
Emil Lerch 2021-06-23 18:20:45 -07:00
parent 77caa626f0
commit 6f38ecd893
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -79,21 +79,10 @@ pub const Aws = struct {
var buffer = std.ArrayList(u8).init(self.allocator); var buffer = std.ArrayList(u8).init(self.allocator);
defer buffer.deinit(); defer buffer.deinit();
const writer = buffer.writer(); const writer = buffer.writer();
// TODO: transformation function should be refactored for operation try url.encode(request, writer, .{
// with a Writer passed in so we don't have to allocate .field_name_transformer = &queryFieldTransformer,
const transformer = struct { .allocator = self.allocator,
allocator: *std.mem.Allocator, });
const This = @This();
pub fn transform(this: This, name: []const u8) ![]const u8 {
return try case.snakeToPascal(this.allocator, name);
}
pub fn transform_deinit(this: This, name: []const u8) void {
this.allocator.free(name);
}
}{ .allocator = self.allocator };
try url.encode(request, writer, .{ .field_name_transformer = transformer });
const continuation = if (buffer.items.len > 0) "&" else ""; const continuation = if (buffer.items.len > 0) "&" else "";
const body = try std.fmt.allocPrint(self.allocator, "Action={s}&Version={s}{s}{s}\n", .{ action.action_name, service.version, continuation, buffer.items }); const body = try std.fmt.allocPrint(self.allocator, "Action={s}&Version={s}{s}{s}\n", .{ action.action_name, service.version, continuation, buffer.items });
@ -123,6 +112,7 @@ pub const Aws = struct {
.allow_camel_case_conversion = true, // new option .allow_camel_case_conversion = true, // new option
.allow_snake_case_conversion = true, // new option .allow_snake_case_conversion = true, // new option
.allow_unknown_fields = true, // new option. Cannot yet handle non-struct fields though .allow_unknown_fields = true, // new option. Cannot yet handle non-struct fields though
.allow_missing_fields = false, // new option. Cannot yet handle non-struct fields though
}; };
const SResponse = ServerResponse(request); const SResponse = ServerResponse(request);
const parsed_response = try json.parse(SResponse, &stream, parser_options); const parsed_response = try json.parse(SResponse, &stream, parser_options);
@ -215,3 +205,29 @@ fn FullResponse(comptime request: anytype) type {
fn Response(comptime request: anytype) type { fn Response(comptime request: anytype) type {
return request.metaInfo().action.Response; return request.metaInfo().action.Response;
} }
fn queryFieldTransformer(field_name: []const u8, encoding_options: url.EncodingOptions) anyerror![]const u8 {
return try case.snakeToPascal(encoding_options.allocator.?, field_name);
}
// Use for debugging json responses of specific requests
// test "dummy request" {
// const allocator = std.testing.allocator;
// const svs = Services(.{.sts}){};
// const request = svs.sts.get_session_token.Request{
// .duration_seconds = 900,
// };
// const FullR = FullResponse(request);
// const response =
// var stream = json.TokenStream.init(response);
//
// const parser_options = json.ParseOptions{
// .allocator = allocator,
// .allow_camel_case_conversion = true, // new option
// .allow_snake_case_conversion = true, // new option
// .allow_unknown_fields = true, // new option. Cannot yet handle non-struct fields though
// .allow_missing_fields = false, // new option. Cannot yet handle non-struct fields though
// };
// const SResponse = ServerResponse(request);
// const r = try json.parse(SResponse, &stream, parser_options);
// json.parseFree(SResponse, r, parser_options);
// }