Skip to content

Commit

Permalink
refactor: rename generate to fetch & move bid request into the standa…
Browse files Browse the repository at this point in the history
…lone method
  • Loading branch information
OlenaPostindustria committed May 21, 2024
1 parent 6d77144 commit 7428a1d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 89 deletions.
158 changes: 81 additions & 77 deletions PrebidMobile/PrebidMobileRendering/Prebid/PBMCore/PBMBidRequester.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,100 +59,104 @@ - (instancetype)initWithConnection:(id<PrebidServerConnectionProtocol>)connectio

- (void)requestBidsWithCompletion:(void (^)(BidResponse *, NSError *))completion {
@weakify(self);
[PBMUserAgentService.shared generateUserAgentWithCompletion:^{
[PBMUserAgentService.shared fetchUserAgentWithCompletion:^(NSString * _Nonnull userAgent) {
@strongify(self);
NSError * const setupError = [self findErrorInSettings];
if (setupError) {
completion(nil, setupError);
return;
}
[self makeRequestWithCompletion:completion];
}];
}

- (void)makeRequestWithCompletion:(void (^)(BidResponse *, NSError *))completion {
NSError * const setupError = [self findErrorInSettings];
if (setupError) {
completion(nil, setupError);
return;
}

if (self.completion) {
completion(nil, [PBMError requestInProgress]);
return;
}

self.completion = completion ?: ^(BidResponse *r, NSError *e) {};

NSString * const requestString = [self getRTBRequest];

NSError * hostURLError = nil;
NSString * const requestServerURL = [Host.shared getHostURLWithHost:self.sdkConfiguration.prebidServerHost error:&hostURLError];

if (hostURLError) {
completion(nil, hostURLError);
return;
}

const NSInteger rawTimeoutMS_onRead = self.sdkConfiguration.timeoutMillis;
NSNumber * const dynamicTimeout_onRead = self.sdkConfiguration.timeoutMillisDynamic;

const NSTimeInterval postTimeout = (dynamicTimeout_onRead ? dynamicTimeout_onRead.doubleValue : (rawTimeoutMS_onRead / 1000.0));

@weakify(self);
NSDate * const requestDate = [NSDate date];
[self.connection post:requestServerURL
data:[requestString dataUsingEncoding:NSUTF8StringEncoding]
timeout:postTimeout
callback:^(PrebidServerResponse * _Nonnull serverResponse) {
@strongify(self);
if (!self) { return; }

if (self.completion) {
completion(nil, [PBMError requestInProgress]);
void (^ const completion)(BidResponse *, NSError *) = self.completion;
self.completion = nil;

if (serverResponse.statusCode == 204) {
completion(nil, PBMError.blankResponse);
return;
}

self.completion = completion ?: ^(BidResponse *r, NSError *e) {};

NSString * const requestString = [self getRTBRequest];

NSError * hostURLError = nil;
NSString * const requestServerURL = [Host.shared getHostURLWithHost:self.sdkConfiguration.prebidServerHost error:&hostURLError];

if (hostURLError) {
completion(nil, hostURLError);
if (serverResponse.error) {
PBMLogInfo(@"Bid Request Error: %@", [serverResponse.error localizedDescription]);
completion(nil, serverResponse.error);
return;
}

const NSInteger rawTimeoutMS_onRead = self.sdkConfiguration.timeoutMillis;
NSNumber * const dynamicTimeout_onRead = self.sdkConfiguration.timeoutMillisDynamic;
PBMLogInfo(@"Bid Response: %@", [[NSString alloc] initWithData:serverResponse.rawData encoding:NSUTF8StringEncoding]);

const NSTimeInterval postTimeout = (dynamicTimeout_onRead ? dynamicTimeout_onRead.doubleValue : (rawTimeoutMS_onRead / 1000.0));
NSError *trasformationError = nil;
BidResponse * const _Nullable bidResponse = [PBMBidResponseTransformer transformResponse:serverResponse error:&trasformationError];

@weakify(self);
NSDate * const requestDate = [NSDate date];
[self.connection post:requestServerURL
data:[requestString dataUsingEncoding:NSUTF8StringEncoding]
timeout:postTimeout
callback:^(PrebidServerResponse * _Nonnull serverResponse) {
@strongify(self);
if (!self) { return; }

void (^ const completion)(BidResponse *, NSError *) = self.completion;
self.completion = nil;

if (serverResponse.statusCode == 204) {
completion(nil, PBMError.blankResponse);
return;
}

if (serverResponse.error) {
PBMLogInfo(@"Bid Request Error: %@", [serverResponse.error localizedDescription]);
completion(nil, serverResponse.error);
return;
if (bidResponse && !trasformationError) {
NSNumber * const tmaxrequest = bidResponse.tmaxrequest;
if (tmaxrequest) {
NSDate * const responseDate = [NSDate date];

const NSTimeInterval bidResponseTimeout = tmaxrequest.doubleValue / 1000.0;
const NSTimeInterval remoteTimeout = ([responseDate timeIntervalSinceDate:requestDate]
+ bidResponseTimeout
+ 0.2);
NSString * const currentServerURL = [Host.shared getHostURLWithHost:self.sdkConfiguration.prebidServerHost error:nil];
if (self.sdkConfiguration.timeoutMillisDynamic == nil && [currentServerURL isEqualToString:requestServerURL]) {
const NSInteger rawTimeoutMS_onWrite = self.sdkConfiguration.timeoutMillis;
const NSTimeInterval appTimeout = rawTimeoutMS_onWrite / 1000.0;
const NSTimeInterval updatedTimeout = MIN(remoteTimeout, appTimeout);
self.sdkConfiguration.timeoutMillisDynamic = @(updatedTimeout);
self.sdkConfiguration.timeoutUpdated = true;
};
}

PBMLogInfo(@"Bid Response: %@", [[NSString alloc] initWithData:serverResponse.rawData encoding:NSUTF8StringEncoding]);

NSError *trasformationError = nil;
BidResponse * const _Nullable bidResponse = [PBMBidResponseTransformer transformResponse:serverResponse error:&trasformationError];
PBMORTBSDKConfiguration *pbsSDKConfig = [bidResponse.ext.extPrebid.passthrough filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(PBMORTBExtPrebidPassthrough *_Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [evaluatedObject.type isEqual: @"prebidmobilesdk"];
}]].firstObject.sdkConfiguration;

if (bidResponse && !trasformationError) {
NSNumber * const tmaxrequest = bidResponse.tmaxrequest;
if (tmaxrequest) {
NSDate * const responseDate = [NSDate date];

const NSTimeInterval bidResponseTimeout = tmaxrequest.doubleValue / 1000.0;
const NSTimeInterval remoteTimeout = ([responseDate timeIntervalSinceDate:requestDate]
+ bidResponseTimeout
+ 0.2);
NSString * const currentServerURL = [Host.shared getHostURLWithHost:self.sdkConfiguration.prebidServerHost error:nil];
if (self.sdkConfiguration.timeoutMillisDynamic == nil && [currentServerURL isEqualToString:requestServerURL]) {
const NSInteger rawTimeoutMS_onWrite = self.sdkConfiguration.timeoutMillis;
const NSTimeInterval appTimeout = rawTimeoutMS_onWrite / 1000.0;
const NSTimeInterval updatedTimeout = MIN(remoteTimeout, appTimeout);
self.sdkConfiguration.timeoutMillisDynamic = @(updatedTimeout);
self.sdkConfiguration.timeoutUpdated = true;
};
if(pbsSDKConfig) {
if(pbsSDKConfig.cftBanner) {
Prebid.shared.creativeFactoryTimeout = pbsSDKConfig.cftBanner.doubleValue;
}

PBMORTBSDKConfiguration *pbsSDKConfig = [bidResponse.ext.extPrebid.passthrough filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(PBMORTBExtPrebidPassthrough *_Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [evaluatedObject.type isEqual: @"prebidmobilesdk"];
}]].firstObject.sdkConfiguration;

if(pbsSDKConfig) {
if(pbsSDKConfig.cftBanner) {
Prebid.shared.creativeFactoryTimeout = pbsSDKConfig.cftBanner.doubleValue;
}

if(pbsSDKConfig.cftPreRender) {
Prebid.shared.creativeFactoryTimeoutPreRenderContent = pbsSDKConfig.cftPreRender.doubleValue;
}
if(pbsSDKConfig.cftPreRender) {
Prebid.shared.creativeFactoryTimeoutPreRenderContent = pbsSDKConfig.cftPreRender.doubleValue;
}
}
completion(bidResponse, trasformationError);
}];
}

completion(bidResponse, trasformationError);
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,29 @@ public class UserAgentService: NSObject {
override init() {
super.init()

generateUserAgent()
fetchUserAgent()
}

public func generateUserAgent(completion: (() -> Void)? = nil) {
public func fetchUserAgent(completion: ((String) -> Void)? = nil) {
// user agent has been already generated
guard userAgent.isEmpty else {
completion?()
completion?(userAgent)
return
}

DispatchQueue.main.async {
self.webView.evaluateJavaScript("navigator.userAgent") { [weak self] result, error in
guard let self = self else { return }

if let error {
Log.error(error.localizedDescription)
}

if let result = result, self?.userAgent.isEmpty == true {
self?.userAgent = "\(result)"
if let result = result, self.userAgent.isEmpty {
self.userAgent = "\(result)"
}

completion?()
completion?(self.userAgent)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class UserAgentServiceTest: XCTestCase {

let userAgentService = UserAgentService.shared

userAgentService.generateUserAgent { [weak self] in
//Should start with a useragent from the Web View
let userAgentString = userAgentService.userAgent
userAgentService.fetchUserAgent { [weak self] userAgentString in
XCTAssert(userAgentService.userAgent == userAgentString)

XCTAssert(userAgentString.PBMdoesMatch("^Mozilla"))

Expand Down Expand Up @@ -68,15 +67,15 @@ class UserAgentServiceTest: XCTestCase {
expectationUserAgentExecuted = expectation(description: "expectationUserAgentExecuted")
expectationUserAgentExecuted?.expectedFulfillmentCount = 3

userAgentService.generateUserAgent { [weak self] in
userAgentService.fetchUserAgent { [weak self] _ in
self?.expectationUserAgentExecuted?.fulfill()
}

userAgentService.generateUserAgent { [weak self] in
userAgentService.fetchUserAgent { [weak self] _ in
self?.expectationUserAgentExecuted?.fulfill()
}

userAgentService.generateUserAgent { [weak self] in
userAgentService.fetchUserAgent { [weak self] _ in
self?.expectationUserAgentExecuted?.fulfill()
}

Expand Down

0 comments on commit 7428a1d

Please sign in to comment.