-
-
Save fdonzello/8a50e191aa8773d87a6e65d2327f1973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation BridgeManagersNetwork | |
+ (NSString *)userAgent | |
{ | |
return @"custom-user-agent-to-be-defined"; | |
} | |
- (FBLResponse *)executeWithFBLApiRequest:(FBLApiRequest *)apiRequest | |
{ | |
// implementazione del metodo GET e POST | |
if ( [[apiRequest getMethod] isEqualToString:@"GET"] || [[apiRequest getMethod] isEqualToString:@"POST"] ) | |
{ | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[apiRequest getUrl]]]; | |
[request setValue:[BridgeManagersNetwork userAgent] forHTTPHeaderField:@"User-Agent"]; | |
if ( [[apiRequest getMethod] isEqualToString:@"POST"] ) | |
{ | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; | |
if ( [apiRequest hasBody] ) | |
{ | |
[request setHTTPBody:[[apiRequest getJsonBody] dataUsingEncoding:NSUTF8StringEncoding]]; | |
} | |
} | |
if ( [apiRequest hasHeaders] ) | |
{ | |
id <JavaUtilMap>headers = [apiRequest getHeaders]; | |
for ( NSString *key in [[headers keySet] toArray] ) | |
{ | |
[request setValue:[headers getWithId:key] forHTTPHeaderField:key]; | |
} | |
} | |
#if 1 | |
// ios pre 9 | |
NSError *error = nil; | |
NSHTTPURLResponse *res = nil; | |
#pragma GCC diagnostic push | |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&error]; | |
#pragma GCC diagnostic pop | |
#else | |
// ios 9 | |
__block NSData *data = nil; | |
__block NSError *error = nil; | |
__block NSHTTPURLResponse *res = nil; | |
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); | |
NSURLSession *session = [NSURLSession sharedSession]; | |
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable dataz, NSURLResponse * _Nullable response, NSError * _Nullable errorz) { | |
data = dataz; | |
error = errorz; | |
res = (NSHTTPURLResponse *)response; | |
dispatch_semaphore_signal(semaphore); | |
}] resume]; | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
#endif | |
FBLResponse *response = nil; | |
if ( data ) | |
{ | |
NSString *strresp = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : @""; | |
response = [[FBLResponse alloc] initWithInt:(res ? (int)[res statusCode] : 501) withNSString:strresp]; | |
} | |
else | |
{ | |
response = [[FBLResponse alloc] initWithInt:500 withNSString:@""]; | |
} | |
return response; | |
} | |
NSAssert(FALSE, @"Metodo non supportato!"); | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment