|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift OpenFeature open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the Swift OpenFeature project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +import OpenFeature |
| 15 | + |
| 16 | +extension OpenFeatureResolution<Bool> { |
| 17 | + package init(_ response: Operations.PostOfrepV1EvaluateFlagsKey.Output, defaultValue: Bool) { |
| 18 | + switch response { |
| 19 | + case .ok(let ok): |
| 20 | + switch ok.body { |
| 21 | + case .json(let responsePayload): |
| 22 | + self = OpenFeatureResolution(responsePayload, defaultValue: defaultValue) |
| 23 | + } |
| 24 | + case .badRequest(let badRequest): |
| 25 | + switch badRequest.body { |
| 26 | + case .json(let responsePayload): |
| 27 | + self = OpenFeatureResolution( |
| 28 | + value: defaultValue, |
| 29 | + error: .init( |
| 30 | + code: .init(rawValue: responsePayload.errorCode.rawValue), |
| 31 | + message: responsePayload.errorDetails |
| 32 | + ), |
| 33 | + reason: .error |
| 34 | + ) |
| 35 | + } |
| 36 | + case .notFound(let notFound): |
| 37 | + switch notFound.body { |
| 38 | + case .json(let responsePayload): |
| 39 | + self = OpenFeatureResolution( |
| 40 | + value: defaultValue, |
| 41 | + error: .init( |
| 42 | + code: .init(rawValue: responsePayload.errorCode.rawValue), |
| 43 | + message: responsePayload.errorDetails |
| 44 | + ), |
| 45 | + reason: .error |
| 46 | + ) |
| 47 | + } |
| 48 | + case .unauthorized: |
| 49 | + self = OpenFeatureResolution( |
| 50 | + value: defaultValue, |
| 51 | + error: OpenFeatureResolutionError(code: .general, message: "Unauthorized."), |
| 52 | + reason: .error |
| 53 | + ) |
| 54 | + case .forbidden: |
| 55 | + self = OpenFeatureResolution( |
| 56 | + value: defaultValue, |
| 57 | + error: OpenFeatureResolutionError(code: .general, message: "Forbidden."), |
| 58 | + reason: .error |
| 59 | + ) |
| 60 | + case .tooManyRequests(let responsePayload): |
| 61 | + let message: String |
| 62 | + if let retryAfter = responsePayload.headers.retryAfter { |
| 63 | + let dateString = retryAfter.ISO8601Format(.iso8601WithTimeZone()) |
| 64 | + message = #"Too many requests. Retry after "\#(dateString)"."# |
| 65 | + } else { |
| 66 | + message = "Too many requests." |
| 67 | + } |
| 68 | + self = OpenFeatureResolution( |
| 69 | + value: defaultValue, |
| 70 | + error: OpenFeatureResolutionError(code: .general, message: message), |
| 71 | + reason: .error |
| 72 | + ) |
| 73 | + case .internalServerError(let internalServerError): |
| 74 | + switch internalServerError.body { |
| 75 | + case .json(let responsePayload): |
| 76 | + self = OpenFeatureResolution( |
| 77 | + value: defaultValue, |
| 78 | + error: OpenFeatureResolutionError(code: .general, message: responsePayload.errorDetails), |
| 79 | + reason: .error |
| 80 | + ) |
| 81 | + } |
| 82 | + case .undocumented(let statusCode, _): |
| 83 | + self = OpenFeatureResolution( |
| 84 | + value: defaultValue, |
| 85 | + error: OpenFeatureResolutionError( |
| 86 | + code: .general, |
| 87 | + message: #"Received unexpected response status code "\#(statusCode)"."# |
| 88 | + ), |
| 89 | + reason: .error |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +extension OpenFeatureResolution<Bool> { |
| 96 | + package init( |
| 97 | + _ response: Components.Schemas.ServerEvaluationSuccess, |
| 98 | + defaultValue: Bool |
| 99 | + ) { |
| 100 | + let variant = response.value1.value1.variant |
| 101 | + let flagMetadata = response.value1.value1.metadata.toFlagMetadata() |
| 102 | + |
| 103 | + switch response.value1.value2 { |
| 104 | + case .BooleanFlag(let boolContainer): |
| 105 | + self.init( |
| 106 | + value: boolContainer.value, |
| 107 | + error: nil, |
| 108 | + reason: response.value1.value1.reason.map(OpenFeatureResolutionReason.init), |
| 109 | + variant: variant, |
| 110 | + flagMetadata: flagMetadata |
| 111 | + ) |
| 112 | + default: |
| 113 | + self.init( |
| 114 | + value: defaultValue, |
| 115 | + error: OpenFeatureResolutionError( |
| 116 | + code: .typeMismatch, |
| 117 | + message: response.value1.value2.typeMismatchErrorMessage(expectedType: "\(Value.self)") |
| 118 | + ), |
| 119 | + reason: .error, |
| 120 | + variant: variant, |
| 121 | + flagMetadata: flagMetadata |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +extension Components.Schemas.EvaluationSuccess.Value1Payload.MetadataPayload? { |
| 128 | + package func toFlagMetadata() -> [String: OpenFeatureFlagMetadataValue] { |
| 129 | + self?.additionalProperties.mapValues(OpenFeatureFlagMetadataValue.init) ?? [:] |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +extension OpenFeatureFlagMetadataValue { |
| 134 | + package init( |
| 135 | + _ payload: Components.Schemas.EvaluationSuccess.Value1Payload.MetadataPayload.AdditionalPropertiesPayload |
| 136 | + ) { |
| 137 | + self = |
| 138 | + switch payload { |
| 139 | + case .case1(let value): .bool(value) |
| 140 | + case .case2(let value): .string(value) |
| 141 | + case .case3(let value): .double(value) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +extension Components.Schemas.EvaluationSuccess.Value2Payload { |
| 147 | + package var typeDescription: String { |
| 148 | + switch self { |
| 149 | + case .BooleanFlag: "Bool" |
| 150 | + case .StringFlag: "String" |
| 151 | + case .IntegerFlag: "Int" |
| 152 | + case .FloatFlag: "Double" |
| 153 | + case .ObjectFlag: "Object" |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + func typeMismatchErrorMessage(expectedType: String) -> String { |
| 158 | + #"Expected flag value of type "\#(expectedType)" but received "\#(typeDescription)"."# |
| 159 | + } |
| 160 | +} |
0 commit comments