Skip to content

Commit

Permalink
feat: add client upload time (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 authored Oct 30, 2023
1 parent 3c5f244 commit a0d59f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Sources/Amplitude/Utilities/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ class HttpClient {

func getRequestData(events: String) -> Data? {
let apiKey = configuration.apiKey
let formatter = ISO8601DateFormatter()
formatter.formatOptions.insert(.withFractionalSeconds)
let clientUploadTime: String = formatter.string(from: getDate())
var requestPayload = """
{"api_key":"\(apiKey)","events":\(events)
{"api_key":"\(apiKey)","client_upload_time":"\(clientUploadTime)","events":\(events)
"""
if let minIdLength = configuration.minIdLength {
requestPayload += """
Expand All @@ -83,6 +86,10 @@ class HttpClient {
requestPayload += "}"
return requestPayload.data(using: .utf8)
}

func getDate() -> Date {
return Date()
}
}

extension HttpClient {
Expand Down
5 changes: 5 additions & 0 deletions Tests/AmplitudeTests/Supports/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class FakeHttpClient: HttpClient {
completion(Result.success(200))
return nil
}

override func getDate() -> Date {
// timestamp of 2023-10-24T18:16:24.000 in UTC time zone
return Date(timeIntervalSince1970: 1698171384)
}
}

class FakeResponseHandler: ResponseHandler {
Expand Down
13 changes: 13 additions & 0 deletions Tests/AmplitudeTests/Utilities/HttpClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ final class HttpClientTests: XCTestCase {
}
}

func testGetRequestData() {
let httpClient = FakeHttpClient(configuration: configuration)
let event = BaseEvent(userId: "unit-test user", eventType: "unit-test event")

let expectedRequestPayload = """
{"api_key":"testApiKey","client_upload_time":"2023-10-24T18:16:24.000Z","events":[\(event.toString())]}
""".data(using: .utf8)

let result = httpClient.getRequestData(events: "[\(event.toString())]")

XCTAssertEqual(result, expectedRequestPayload)
}

func testUploadWithInvalidApiKey() {
// TODO: currently this test is sending request to real Amplitude host, update to mock for better stability
let httpClient = HttpClient(configuration: configuration)
Expand Down

0 comments on commit a0d59f1

Please sign in to comment.