-
Notifications
You must be signed in to change notification settings - Fork 113
Add support for AppSync events. #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6073c58
Add support for AppSync events.
2e6ca2b
AppSync.Request => AppSync.Event
DwayneCoussement 1923494
Update Sources/AWSLambdaEvents/AppSync.swift
DwayneCoussement 0d5d61e
Swiftformat + review comments
c28333c
Improve parsing of identity
DwayneCoussement 32aaff1
Merge branch 'main' into appsync
DwayneCoussement File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public struct AppSync { | ||
public struct Request: Codable { | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public let arguments: [String: ArgumentValue] | ||
|
||
public enum ArgumentValue: Codable { | ||
case string(String) | ||
case dictionary([String: String]) | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
if let strValue = try? container.decode(String.self) { | ||
self = .string(strValue) | ||
} else if let dictionaryValue = try? container.decode([String: String].self) { | ||
self = .dictionary(dictionaryValue) | ||
} else { | ||
throw DecodingError.dataCorruptedError(in: container, debugDescription: """ | ||
Unexpected AppSync argument. | ||
Expected a String or a Dictionary. | ||
""") | ||
} | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
switch self { | ||
case .dictionary(let array): | ||
try container.encode(array) | ||
case .string(let str): | ||
try container.encode(str) | ||
} | ||
} | ||
} | ||
|
||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public let info: Info | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public struct Info: Codable { | ||
public var selectionSetList: [String] | ||
public var selectionSetGraphQL: String | ||
public var parentTypeName: String | ||
public var fieldName: String | ||
public var variables: [String: String] | ||
} | ||
public let identity: Identity | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public struct Identity: Codable { | ||
public struct Claims { | ||
let sub: String | ||
let emailVerified: Bool | ||
let iss: String | ||
let phoneNumberVerified: Bool | ||
let cognitoUsername: String | ||
let aud: String | ||
let eventId: String | ||
let tokenUse: String | ||
let authTime: Int | ||
let phoneNumber: String? | ||
let exp: Int | ||
let iat: Int | ||
let email: String? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case sub, emailVerified = "email_verified", iss, phoneNumberVerified = "phone_number_verified", cognitoUsername = "cognito:username", aud, eventId = "event_id", tokenUse = "token_use", authTime = "auth_time", phoneNumber = "phone_number", exp, iat, email | ||
} | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public let defaultAuthStrategy: String | ||
public let issuer: String | ||
public let sourceIp: [String] | ||
public let sub: String | ||
public let userName: String? | ||
} | ||
} | ||
} | ||
|
||
extension AppSync { | ||
public enum Response<ResultType: Codable>: Encodable { | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
switch self { | ||
case .array(let array): | ||
try container.encode(array) | ||
case .object(let object): | ||
try container.encode(object) | ||
case .dictionary(let dictionary): | ||
try container.encode(dictionary) | ||
} | ||
} | ||
|
||
case object(ResultType) | ||
case array([ResultType]) | ||
case dictionary([String: ResultType]) | ||
} | ||
|
||
public typealias JSONStringResponse = Response<String> | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import AWSLambdaEvents | ||
import XCTest | ||
|
||
class AppSyncTests: XCTestCase { | ||
static let exampleEventBody = """ | ||
{ | ||
"arguments": { | ||
"id": "my identifier" | ||
}, | ||
"identity": { | ||
"claims": { | ||
"sub": "192879fc-a240-4bf1-ab5a-d6a00f3063f9", | ||
"email_verified": true, | ||
"iss": "https://cognito-idp.us-west-2.amazonaws.com/us-west-xxxxxxxxxxx", | ||
"phone_number_verified": false, | ||
"cognito:username": "jdoe", | ||
"aud": "7471s60os7h0uu77i1tk27sp9n", | ||
"event_id": "bc334ed8-a938-4474-b644-9547e304e606", | ||
"token_use": "id", | ||
"auth_time": 1599154213, | ||
"phone_number": "+19999999999", | ||
"exp": 1599157813, | ||
"iat": 1599154213, | ||
"email": "jdoe@email.com" | ||
}, | ||
"defaultAuthStrategy": "ALLOW", | ||
"groups": null, | ||
"issuer": "https://cognito-idp.us-west-2.amazonaws.com/us-west-xxxxxxxxxxx", | ||
"sourceIp": [ | ||
"1.1.1.1" | ||
], | ||
"sub": "192879fc-a240-4bf1-ab5a-d6a00f3063f9", | ||
"username": "jdoe" | ||
}, | ||
"source": null, | ||
"request": { | ||
"headers": { | ||
"x-forwarded-for": "1.1.1.1, 2.2.2.2", | ||
"cloudfront-viewer-country": "US", | ||
"cloudfront-is-tablet-viewer": "false", | ||
"via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)", | ||
"cloudfront-forwarded-proto": "https", | ||
"origin": "https://us-west-1.console.aws.amazon.com", | ||
"content-length": "217", | ||
"accept-language": "en-US,en;q=0.9", | ||
"host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com", | ||
"x-forwarded-proto": "https", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36", | ||
"accept": "*/*", | ||
"cloudfront-is-mobile-viewer": "false", | ||
"cloudfront-is-smarttv-viewer": "false", | ||
"accept-encoding": "gzip, deflate, br", | ||
"referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1", | ||
"content-type": "application/json", | ||
"sec-fetch-mode": "cors", | ||
"x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==", | ||
"x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714", | ||
"authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...", | ||
"sec-fetch-dest": "empty", | ||
"x-amz-user-agent": "AWS-Console-AppSync/", | ||
"cloudfront-is-desktop-viewer": "true", | ||
"sec-fetch-site": "cross-site", | ||
"x-forwarded-port": "443" | ||
} | ||
}, | ||
"prev": null, | ||
"info": { | ||
"selectionSetList": [ | ||
"id", | ||
"field1", | ||
"field2" | ||
], | ||
"selectionSetGraphQL": "{ id }", | ||
"parentTypeName": "Mutation", | ||
"fieldName": "createSomething", | ||
"variables": {} | ||
}, | ||
"stash": {} | ||
} | ||
""" | ||
|
||
// MARK: Decoding | ||
func testRequestDecodingExampleEvent() { | ||
let data = AppSyncTests.exampleEventBody.data(using: .utf8)! | ||
var req: AppSync.Request? | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertNoThrow(req = try JSONDecoder().decode(AppSync.Request.self, from: data)) | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
XCTAssertNotNil(req?.arguments) | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertEqual(req?.arguments["id"], .string("my identifier")) | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertEqual(req?.info.fieldName, "createSomething") | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertEqual(req?.info.parentTypeName, "Mutation") | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertEqual(req?.info.selectionSetList, ["id", "field1", "field2"]) | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
extension AppSync.Request.ArgumentValue: Equatable { | ||
DwayneCoussement marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static func == (lhs: Self, rhs: Self) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.string(let lhsString), .string(let rhsString)): | ||
return lhsString == rhsString | ||
case (.dictionary(let lhsDictionary), .dictionary(let rhsDictionary)): | ||
return lhsDictionary == rhsDictionary | ||
default: | ||
return false | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.