|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +// https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html |
| 16 | +public enum AppSync { |
| 17 | + public struct Event: Decodable { |
| 18 | + public let arguments: [String: ArgumentValue] |
| 19 | + |
| 20 | + public enum ArgumentValue: Codable { |
| 21 | + case string(String) |
| 22 | + case dictionary([String: String]) |
| 23 | + |
| 24 | + public init(from decoder: Decoder) throws { |
| 25 | + let container = try decoder.singleValueContainer() |
| 26 | + if let strValue = try? container.decode(String.self) { |
| 27 | + self = .string(strValue) |
| 28 | + } else if let dictionaryValue = try? container.decode([String: String].self) { |
| 29 | + self = .dictionary(dictionaryValue) |
| 30 | + } else { |
| 31 | + throw DecodingError.dataCorruptedError(in: container, debugDescription: """ |
| 32 | + Unexpected AppSync argument. |
| 33 | + Expected a String or a Dictionary. |
| 34 | + """) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public func encode(to encoder: Encoder) throws { |
| 39 | + var container = encoder.singleValueContainer() |
| 40 | + switch self { |
| 41 | + case .dictionary(let array): |
| 42 | + try container.encode(array) |
| 43 | + case .string(let str): |
| 44 | + try container.encode(str) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public let request: Request |
| 50 | + public struct Request: Decodable { |
| 51 | + let headers: HTTPHeaders |
| 52 | + } |
| 53 | + |
| 54 | + public let source: [String: String]? |
| 55 | + public let stash: [String: String]? |
| 56 | + |
| 57 | + public let info: Info |
| 58 | + public struct Info: Codable { |
| 59 | + public var selectionSetList: [String] |
| 60 | + public var selectionSetGraphQL: String |
| 61 | + public var parentTypeName: String |
| 62 | + public var fieldName: String |
| 63 | + public var variables: [String: String] |
| 64 | + } |
| 65 | + |
| 66 | + public let identity: Identity? |
| 67 | + public enum Identity: Codable { |
| 68 | + case iam(IAMIdentity) |
| 69 | + case cognitoUserPools(CognitoUserPoolIdentity) |
| 70 | + |
| 71 | + public struct IAMIdentity: Codable { |
| 72 | + public let accountId: String |
| 73 | + public let cognitoIdentityPoolId: String |
| 74 | + public let cognitoIdentityId: String |
| 75 | + public let sourceIp: [String] |
| 76 | + public let username: String? |
| 77 | + public let userArn: String |
| 78 | + public let cognitoIdentityAuthType: String |
| 79 | + public let cognitoIdentityAuthProvider: String |
| 80 | + } |
| 81 | + |
| 82 | + public struct CognitoUserPoolIdentity: Codable { |
| 83 | + public let defaultAuthStrategy: String |
| 84 | + public let issuer: String |
| 85 | + public let sourceIp: [String] |
| 86 | + public let sub: String |
| 87 | + public let username: String? |
| 88 | + |
| 89 | + public struct Claims { |
| 90 | + let sub: String |
| 91 | + let emailVerified: Bool |
| 92 | + let iss: String |
| 93 | + let phoneNumberVerified: Bool |
| 94 | + let cognitoUsername: String |
| 95 | + let aud: String |
| 96 | + let eventId: String |
| 97 | + let tokenUse: String |
| 98 | + let authTime: Int |
| 99 | + let phoneNumber: String? |
| 100 | + let exp: Int |
| 101 | + let iat: Int |
| 102 | + let email: String? |
| 103 | + |
| 104 | + enum CodingKeys: String, CodingKey { |
| 105 | + case sub |
| 106 | + case emailVerified = "email_verified" |
| 107 | + case iss |
| 108 | + case phoneNumberVerified = "phone_number_verified" |
| 109 | + case cognitoUsername = "cognito:username" |
| 110 | + case aud |
| 111 | + case eventId = "event_id" |
| 112 | + case tokenUse = "token_use" |
| 113 | + case authTime = "auth_time" |
| 114 | + case phoneNumber = "phone_number" |
| 115 | + case exp |
| 116 | + case iat |
| 117 | + case email |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public init(from decoder: Decoder) throws { |
| 123 | + let container = try decoder.singleValueContainer() |
| 124 | + if let iamIdentity = try? container.decode(IAMIdentity.self) { |
| 125 | + self = .iam(iamIdentity) |
| 126 | + } else if let cognitoIdentity = try? container.decode(CognitoUserPoolIdentity.self) { |
| 127 | + self = .cognitoUserPools(cognitoIdentity) |
| 128 | + } else { |
| 129 | + throw DecodingError.dataCorruptedError(in: container, debugDescription: """ |
| 130 | + Unexpected Identity argument. |
| 131 | + Expected a IAM Identity or a Cognito User Pool Identity. |
| 132 | + """) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public func encode(to encoder: Encoder) throws { |
| 137 | + var container = encoder.singleValueContainer() |
| 138 | + switch self { |
| 139 | + case .iam(let iamIdentity): |
| 140 | + try container.encode(iamIdentity) |
| 141 | + case .cognitoUserPools(let cognitoUserPool): |
| 142 | + try container.encode(cognitoUserPool) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +public extension AppSync { |
| 150 | + enum Response<ResultType: Encodable>: Encodable { |
| 151 | + public func encode(to encoder: Encoder) throws { |
| 152 | + var container = encoder.singleValueContainer() |
| 153 | + switch self { |
| 154 | + case .array(let array): |
| 155 | + try container.encode(array) |
| 156 | + case .object(let object): |
| 157 | + try container.encode(object) |
| 158 | + case .dictionary(let dictionary): |
| 159 | + try container.encode(dictionary) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + case object(ResultType) |
| 164 | + case array([ResultType]) |
| 165 | + case dictionary([String: ResultType]) |
| 166 | + } |
| 167 | + |
| 168 | + typealias JSONStringResponse = Response<String> |
| 169 | +} |
0 commit comments