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

Add ability to bulk upload traits #24

Merged
merged 1 commit into from
Feb 21, 2023
Merged
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
10 changes: 7 additions & 3 deletions FlagsmithClient/Classes/Internal/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ enum Router {
case getFlags
case getIdentity(identity: String)
case postTrait(trait: Trait, identity: String)
case postTraits(identity: String, traits: [Trait])
case postAnalytics(events: [String:Int])

private var method: HTTPMethod {
switch self {
case .getFlags, .getIdentity:
return .get
case .postTrait, .postAnalytics:
case .postTrait, .postTraits, .postAnalytics:
return .post
}
}
Expand All @@ -34,7 +35,7 @@ enum Router {
switch self {
case .getFlags:
return "flags/"
case .getIdentity:
case .getIdentity, .postTraits:
return "identities/"
case .postTrait:
return "traits/"
Expand All @@ -45,7 +46,7 @@ enum Router {

private var parameters: [URLQueryItem]? {
switch self {
case .getIdentity(let identity):
case .getIdentity(let identity), .postTraits(let identity, _):
return [URLQueryItem(name: "identifier", value: identity)]
default:
return nil
Expand All @@ -59,6 +60,9 @@ enum Router {
case .postTrait(let trait, let identifier):
let traitWithIdentity = Trait(trait: trait, identifier: identifier)
return try encoder.encode(traitWithIdentity)
case .postTraits(let identifier, let traits):
let traitsWithIdentity = Traits(traits: traits, identifier: identifier)
return try encoder.encode(traitsWithIdentity)
case .postAnalytics(let events):
return try encoder.encode(events)
}
Expand Down
16 changes: 16 additions & 0 deletions FlagsmithClient/Classes/Traits.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Router.swift
// FlagsmithClient
//
// Created by Rob Valdes on 07/02/23.
//

import Foundation

/**
A Traits object represent a collection of different `Trait`s stored against the same Identity (user) on the server.
*/
public struct Traits: Codable {
public let traits: [Trait]
public let identifier: String?
}
29 changes: 29 additions & 0 deletions FlagsmithClient/Tests/ComparableJson.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ComparableJson.swift
// FlagsmithClientTests
//
// Created by Rob Valdes on 07/02/23.
//

import XCTest

extension String {
func json(using encoding: String.Encoding) throws -> NSDictionary {
return try self.data(using: encoding).json()
}
}

extension Optional where Wrapped == Data {
func json() throws -> NSDictionary {
let data = try XCTUnwrap(self)
return try data.json()
}
}

extension Data {
func json() throws -> NSDictionary {
let json = try JSONSerialization.jsonObject(with: self)
let dict = json as! [String : Any]
return NSDictionary(dictionary: dict)
}
}
7 changes: 1 addition & 6 deletions FlagsmithClient/Tests/FlagsmithClientTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import XCTest

class FlagsmithClientTestCase: XCTestCase {

let encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
return encoder
}()

let encoder: JSONEncoder = .init()
let decoder: JSONDecoder = .init()
}
46 changes: 36 additions & 10 deletions FlagsmithClient/Tests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,54 @@ final class RouterTests: FlagsmithClientTestCase {
XCTAssertNil(request.httpBody)
}

func testPostTraitsRequest() throws {
func testPostTraitRequest() throws {
let trait = Trait(key: "meaning_of_life", value: 42)
let url = try XCTUnwrap(baseUrl)
let route = Router.postTrait(trait: trait, identity: "CFF8D9CA")
let request = try route.request(baseUrl: url, apiKey: apiKey, using: encoder)
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(request.url?.absoluteString, "https://edge.api.flagsmith.com/api/v1/traits/")

let json = """
let json = try """
{
"identity" : {
"identifier" : "CFF8D9CA"
},
"trait_key" : "meaning_of_life",
"trait_value" : 42
}
"""
let data = try XCTUnwrap(json.data(using: .utf8))
""".json(using: .utf8)
let body = try request.httpBody.json()

XCTAssertEqual(request.httpBody, data)
XCTAssertEqual(body, json)
}

func testPostTraitsRequest() throws {
let questionTrait = Trait(key: "question_meaning_of_life", value: "6 x 9")
let meaningTrait = Trait(key: "meaning_of_life", value: 42)
let url = try XCTUnwrap(baseUrl)
let route = Router.postTraits(identity: "A1B2C3D4", traits: [questionTrait, meaningTrait])
let request = try route.request(baseUrl: url, apiKey: apiKey, using: encoder)
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(request.url?.absoluteString, "https://edge.api.flagsmith.com/api/v1/identities/?identifier=A1B2C3D4")

let expectedJson = try """
{
"traits" : [
{
"trait_key" : "question_meaning_of_life",
"trait_value" : "6 x 9"
},
{
"trait_key" : "meaning_of_life",
"trait_value" : 42
}
],
"identifier" : "A1B2C3D4"
}
""".json(using: .utf8)
let body = try request.httpBody.json()
XCTAssertEqual(body, expectedJson)
}

func testPostAnalyticsRequest() throws {
Expand All @@ -68,14 +96,12 @@ final class RouterTests: FlagsmithClientTestCase {
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(request.url?.absoluteString, "https://edge.api.flagsmith.com/api/v1/analytics/flags/")

let json = """
let json = try """
{
"one" : 1,
"two" : 2
}
"""
let data = try XCTUnwrap(json.data(using: .utf8))

XCTAssertEqual(request.httpBody, data)
""".json(using: .utf8)
XCTAssertEqual(try request.httpBody.json(), json)
}
}
6 changes: 3 additions & 3 deletions FlagsmithClient/Tests/TraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ final class TraitTests: FlagsmithClientTestCase {
let wrappedTrait = Trait(key: "dark_mode", value: .bool(true))
let trait = Trait(trait: wrappedTrait, identifier: "theme_settings")
let data = try encoder.encode(trait)
let json = try XCTUnwrap(String(data: data, encoding: .utf8))
XCTAssertEqual(json, """
let json = try """
{
"identity" : {
"identifier" : "theme_settings"
},
"trait_key" : "dark_mode",
"trait_value" : true
}
""")
""".json(using: .utf8)
XCTAssertEqual(try data.json(), json)
}
}