Skip to content
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

test: [TMP] Manual testing contexts #96

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,20 @@ extension ConfidenceDemoApp {
// NOTE: Using a random UUID for each app start is not advised and can result in getting stale values.
let ctx = MutableContext(
targetingKey: UUID.init().uuidString,
structure: MutableStructure.init(attributes: ["country": .string("SE")]))
structure: MutableStructure.init(attributes: [
"my_string": .string("of_ctx"),
"my_number": .integer(3)
]))
confidence.updateContextEntry(key: "my_string", value: ConfidenceValue(string: "confidence_ctx"))
confidence.updateContextEntry(key: "my_number", value: ConfidenceValue(integer: 5))
Task {
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: ctx)
confidence.send(
definition: "test",
payload: [
"my_string": ConfidenceValue(string: "message")
]
)
}
confidence.send(
definition: "all-types",
payload: [
"my_string": ConfidenceValue(string: "hello_from_world"),
"my_timestamp": ConfidenceValue(timestamp: Date()),
"my_bool": ConfidenceValue(boolean: true),
"my_date": ConfidenceValue(date: DateComponents(year: 2024, month: 4, day: 3)),
"my_int": ConfidenceValue(integer: 2),
"my_double": ConfidenceValue(double: 3.14),
"my_list": ConfidenceValue(booleanList: [true, false]),
"my_struct": ConfidenceValue(structure: [
"my_nested_struct": ConfidenceValue(structure: [
"my_nested_nested_struct": ConfidenceValue(structure: [
"my_nested_nested_nested_int": ConfidenceValue(integer: 666)
]),
"my_nested_nested_list": ConfidenceValue(dateList: [
DateComponents(year: 2024, month: 4, day: 4),
DateComponents(year: 2024, month: 4, day: 5)
])
]),
"my_nested_string": ConfidenceValue(string: "nested_hello")
])
]
)
}
}
19 changes: 19 additions & 0 deletions Sources/Common/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ final public class NetworkClient: HttpClient {
}
}

func prettyPrintJsonString(from data: Data) -> String? {
guard let jsonString = String(data: data, encoding: .utf8) else {
return nil
}

do {
if let jsonData = jsonString.data(using: .utf8) {
let json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
let prettyData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
print(String(decoding: prettyData, as: UTF8.self))
}
} catch {
print("Error decoding JSON data: \(error)")
}
return nil
}


private func perform(
request: URLRequest,
retry: Retry
Expand All @@ -61,6 +79,7 @@ final public class NetworkClient: HttpClient {
let retryWait: TimeInterval? = retryHandler.retryIn()

do {
prettyPrintJsonString(from: request.httpBody!)
let (data, response) = try await self.session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
return RequestResult(httpResponse: nil, data: nil, error: HttpClientError.invalidResponse)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/ConfidenceValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension ConfidenceStruct {
newStruct[entry.key] = entry.value
}
// add all the rest keys
for entry in self {
for entry in self where entry.key != "open_feature" {
newStruct[entry.key] = entry.value
}
return newStruct
Expand Down