Skip to content

Commit e2c92f0

Browse files
fix: Missing content-type header for async InsightsClient.sendEvents call (#710)
* fix: move content-type header from synchronous InsightsClient.sendEvent call to Command.Insights.SendEvents
1 parent f80cecf commit e2c92f0

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Sources/AlgoliaSearchClient/Client/InsightsClient.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ public extension InsightsClient {
108108
- Returns: JSON object
109109
*/
110110
@discardableResult func sendEvents(_ events: [InsightsEvent], requestOptions: RequestOptions? = nil) throws -> Empty {
111-
var updatedRequestOptions = requestOptions ?? .init()
112-
updatedRequestOptions.setHeader("application/json", forKey: .contentType)
113-
let requestOptions = updatedRequestOptions
114111
let command = Command.Insights.SendEvents(events: events, requestOptions: requestOptions)
115112
return try execute(command)
116113
}

Sources/AlgoliaSearchClient/Command/Command+Insights.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extension Command {
2222

2323
init(events: [InsightsEvent], requestOptions: RequestOptions?) {
2424
let body = EventsWrapper(events)
25+
var requestOptions = requestOptions.unwrapOrCreate()
26+
requestOptions.setHeader("application/json", forKey: .contentType)
2527
self.requestOptions = requestOptions
2628
self.urlRequest = .init(method: .post, path: Path.eventsV1, body: body.httpBody, requestOptions: self.requestOptions)
2729
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// InsightsCommandTest.swift
3+
//
4+
//
5+
// Created by Vladislav Fitc on 21/12/2020.
6+
//
7+
8+
import Foundation
9+
import XCTest
10+
@testable import AlgoliaSearchClient
11+
12+
class InsightsCommandTest: XCTestCase, AlgoliaCommandTest {
13+
14+
func testSendEvents() throws {
15+
let event = try InsightsEvent.click(name: "Search User Clicked",
16+
indexName: "testIndex",
17+
userToken: "test_user_token",
18+
queryID: "test query id",
19+
objectIDsWithPositions: [("object1", 1)])
20+
21+
let command = Command.Insights.SendEvents(events: [event], requestOptions: test.requestOptions)
22+
check(command: command,
23+
callType: .write,
24+
method: .post,
25+
urlPath: "/1/events",
26+
queryItems: [.init(name: "testParameter", value: "testParameterValue")],
27+
body: EventsWrapper([event]).httpBody,
28+
additionalHeaders: [.contentType: "application/json"],
29+
requestOptions: test.requestOptions)
30+
31+
}
32+
}

0 commit comments

Comments
 (0)