Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Adding timeout to handle search ads
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Jan 5, 2024
1 parent 09c092d commit 5f14908
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Sources/Growth/URP/AttributionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public class AttributionManager {

@MainActor public func handleSearchAdsFeatureLinkage() async throws -> FeatureLinkageType {
do {
let attributionData = try await handleSearchAdsInstallAttribution()

let attributionData = try await urp.adCampaignLookup(isRetryEnabled: false, timeout: 30)
generateReferralCodeAndPingServer(with: attributionData)

return fetchFeatureTypes(for: attributionData.campaignId)
} catch {
throw error
Expand Down
12 changes: 8 additions & 4 deletions Sources/Growth/URP/UrpService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct UrpService {
}
}

@MainActor func adCampaignTokenLookupQueue(adAttributionToken: String, isRetryEnabled: Bool = true) async throws -> AdAttributionData {
@MainActor func adCampaignTokenLookupQueue(adAttributionToken: String, isRetryEnabled: Bool = true, timeout: TimeInterval) async throws -> AdAttributionData {
guard let endPoint = URL(string: adServicesURL) else {
Logger.module.error("AdServicesURLString can not be resolved: \(adServicesURL)")
throw URLError(.badURL)
Expand All @@ -89,7 +89,11 @@ struct UrpService {
let attributionDataToken = adAttributionToken.data(using: .utf8)

do {
let (result, _) = try await sessionManager.adServicesAttributionApiRequest(endPoint: endPoint, rawData: attributionDataToken, isRetryEnabled: isRetryEnabled)
let (result, _) = try await sessionManager.adServicesAttributionApiRequest(
endPoint: endPoint,
rawData: attributionDataToken,
isRetryEnabled: isRetryEnabled,
timeout: timeout)
UrpLog.log("Ad Attribution response: \(result)")

if let resultData = result as? Data {
Expand Down Expand Up @@ -167,7 +171,7 @@ extension URLSession {
}

// Apple ad service attricution request requires plain text encoding with post method and passing token as rawdata
func adServicesAttributionApiRequest(endPoint: URL, rawData: Data?, isRetryEnabled: Bool) async throws -> (Any, URLResponse) {
func adServicesAttributionApiRequest(endPoint: URL, rawData: Data?, isRetryEnabled: Bool, timeout: TimeInterval) async throws -> (Any, URLResponse) {
// Re-try logic will not be enabled while onboarding happening on first launch
if isRetryEnabled {
// According to attributiontoken API docs
Expand All @@ -177,7 +181,7 @@ extension URLSession {
return try await self.request(endPoint, method: .post, rawData: rawData, encoding: .textPlain)
}.value
} else {
return try await self.request(endPoint, method: .post, rawData: rawData, encoding: .textPlain)
return try await self.request(endPoint, method: .post, rawData: rawData, encoding: .textPlain, timeout: timeout)
}
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/Growth/URP/UserReferralProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@ public class UserReferralProgram {
service.referralCodeLookup(refCode: refCode, completion: referralBlock)
}

@MainActor public func adCampaignLookup(isRetryEnabled: Bool = true) async throws -> AdAttributionData {
@MainActor public func adCampaignLookup(isRetryEnabled: Bool = true, timeout: TimeInterval = 60) async throws -> AdAttributionData {
// Fetching ad attibution token
do {
let adAttributionToken = try AAAttribution.attributionToken()

do {
return try await service.adCampaignTokenLookupQueue(adAttributionToken: adAttributionToken)

return try await service.adCampaignTokenLookupQueue(
adAttributionToken: adAttributionToken,
isRetryEnabled: isRetryEnabled,
timeout: timeout)
} catch {
Logger.module.info("Could not retrieve ad campaign attibution from ad services")
throw error
Expand Down

0 comments on commit 5f14908

Please sign in to comment.