Skip to content

Commit

Permalink
[ABW-3259] NPS survey upload using body params (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
danvleju-rdx committed Jul 5, 2024
1 parent 93513e5 commit d2194db
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
8 changes: 7 additions & 1 deletion RadixWallet/Clients/HTTPClient/HTTPClient+Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ public struct HTTPClient: Sendable, DependencyKey {

// MARK: HTTPClient.ExecuteRequest
extension HTTPClient {
public typealias ExecuteRequest = @Sendable (URLRequest) async throws -> Data
public typealias ExecuteRequest = @Sendable (_ request: URLRequest, _ acceptedStatusCodes: [HTTPStatusCode]) async throws -> Data
}

extension HTTPClient {
func executeRequest(_ request: URLRequest) async throws -> Data {
try await executeRequest(request, [.ok])
}
}
4 changes: 2 additions & 2 deletions RadixWallet/Clients/HTTPClient/HTTPClient+Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ extension HTTPClient {
let session = URLSession.shared

return .init(
executeRequest: { request in
executeRequest: { request, acceptedStatusCodes in
let (data, urlResponse) = try await session.data(for: request)

guard let httpURLResponse = urlResponse as? HTTPURLResponse else {
throw ExpectedHTTPURLResponse()
}

guard httpURLResponse.statusCode == BadHTTPResponseCode.expected else {
guard let statusCode = httpURLResponse.status, acceptedStatusCodes.contains(statusCode) else {
#if DEBUG
loggerGlobal.error("Request with URL: \(request.url!.absoluteString) failed with status code: \(httpURLResponse.statusCode), data: \(data.prettyPrintedJSONString ?? "<NOT_JSON>")")
#endif
Expand Down
2 changes: 1 addition & 1 deletion RadixWallet/Clients/HTTPClient/HTTPClient+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension HTTPClient: TestDependencyKey {

private static func noop() -> Self {
.init(
executeRequest: { _ in Data() }
executeRequest: { _, _ in Data() }
)
}
}
Expand Down
56 changes: 21 additions & 35 deletions RadixWallet/Clients/NPSSurveyClient/NPSSurveyClient+Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ extension NPSSurveyClient {
extension NPSSurveyClient {
#if DEBUG
static let rootURL = "https://dev-wallet-net-promoter-score.radixdlt.com/v1/responses"
static let formUUID = "281622a0-dc6b-11ee-8fd1-23c96056fbd2"
#else
static let rootURL = "https://wallet-net-promoter-score.radixdlt.com/v1/responses"
static let formUUID = "3432b6e0-dfad-11ee-a53c-95167f067d9c"
#endif

enum QueryItem: String {
enum BodyParam: String {
case id
case formUuid = "form_uuid"
case nps
Expand All @@ -101,51 +103,35 @@ extension NPSSurveyClient {
}
}()

var urlComponents = URLComponents(string: Self.rootURL)!
urlComponents.queryItems = [.userId(userId), .formUUID] + (feedback.map(\.queryItems) ?? [])
let urlComponents = URLComponents(string: Self.rootURL)!

guard let url = urlComponents.url else {
return
}

var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.allHTTPHeaderFields = [
"accept": "application/json",
"Content-Type": "application/json",
]

var feedbackParams: [String: Any] = [
BodyParam.id.rawValue: userId.uuidString,
BodyParam.formUuid.rawValue: formUUID,
]
if let feedback {
feedbackParams[BodyParam.nps.rawValue] = feedback.npsScore
}
if let reason = feedback?.reason {
feedbackParams[BodyParam.feedbackReason.rawValue] = reason
}
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: feedbackParams)

do {
_ = try await httpClient.executeRequest(urlRequest)
_ = try await httpClient.executeRequest(urlRequest, [.ok, .accepted])
} catch {
loggerGlobal.info("Failed to submit nps survey feedback \(error)")
}
}
}

private extension URLQueryItem {
static func userId(_ id: UUID) -> Self {
.init(name: "id", value: id.uuidString)
}

#if DEBUG
static let formUUID: Self = .init(name: "form_uuid", value: "281622a0-dc6b-11ee-8fd1-23c96056fbd2")
#else
static let formUUID: Self = .init(name: "form_uuid", value: "3432b6e0-dfad-11ee-a53c-95167f067d9c")
#endif

static func npsScore(_ score: Int) -> Self {
.init(name: NPSSurveyClient.QueryItem.nps.rawValue, value: "\(score)")
}

static func npsFeedbackReason(_ reason: String) -> Self {
.init(name: NPSSurveyClient.QueryItem.feedbackReason.rawValue, value: reason)
}
}

extension NPSSurveyClient.UserFeedback {
var queryItems: [URLQueryItem] {
var queryItems: [URLQueryItem] = []
queryItems.append(.npsScore(npsScore))
if let reason {
queryItems.append(.npsFeedbackReason(reason))
}
return queryItems
}
}
2 changes: 1 addition & 1 deletion RadixWallet/Clients/ROLAClient/ROLAClient+Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension ROLAClient {
throw ExpectedHTTPURLResponse()
}

guard httpURLResponse.statusCode == BadHTTPResponseCode.expected else {
guard httpURLResponse.status == .ok else {
throw BadHTTPResponseCode(got: httpURLResponse.statusCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ public struct ExpectedHTTPURLResponse: Swift.Error {
// MARK: - BadHTTPResponseCode
public struct BadHTTPResponseCode: LocalizedError {
public let got: Int
public let butExpected = Self.expected
public static let expected = 200

public init(got: Int) {
self.got = got
Expand All @@ -33,3 +31,18 @@ public struct ResponseDecodingError: Swift.Error {
self.error = error
}
}

// MARK: - HTTPStatusCode
public enum HTTPStatusCode: Int, Error {
/// - ok: Standard response for successful HTTP requests.
case ok = 200

/// - accepted: The request has been accepted for processing, but the processing has not been completed.
case accepted = 202
}

extension HTTPURLResponse {
var status: HTTPStatusCode? {
.init(rawValue: statusCode)
}
}

0 comments on commit d2194db

Please sign in to comment.