Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

allow custom timeout interval. #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion MKNetworkKit/MKNetworkEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@

/*!
* @abstract Creates a simple GET Operation with a request URL, parameters, HTTP Method and the SSL switch
*
* @discussion
* Creates an operation with the given URL path.
* The ssl option when true changes the URL to https.
* The ssl option when false changes the URL to http.
* The default headers you specified in your MKNetworkEngine subclass gets added to the headers
* The params dictionary in this method gets attached to the URL as query parameters if the HTTP Method is GET/DELETE
* The params dictionary is attached to the body if the HTTP Method is POST/PUT
* The previously mentioned methods operationWithPath: and operationWithPath:params: call this internally
*/
-(MKNetworkOperation*) operationWithPath:(NSString*) path
params:(NSDictionary*) body
httpMethod:(NSString*)method
ssl:(BOOL) useSSL;

/*!
* @abstract Creates a simple GET Operation with a request URL, parameters, HTTP Method, the SSL switch and timeout interval
*
* @discussion
* Creates an operation with the given URL path.
Expand All @@ -148,7 +165,8 @@
-(MKNetworkOperation*) operationWithPath:(NSString*) path
params:(NSDictionary*) body
httpMethod:(NSString*)method
ssl:(BOOL) useSSL;
ssl:(BOOL) useSSL
timeoutInterval:(NSTimeInterval)timeoutInterval;

/*!
* @abstract Creates a simple GET Operation with a request URL
Expand Down Expand Up @@ -192,6 +210,25 @@
params:(NSDictionary*) body
httpMethod:(NSString*) method;

/*!
* @abstract Creates a simple Operation with a request URL, parameters ,HTTP Method and timeout interval
*
* @discussion
* Creates an operation with the given absolute URL.
* The hostname of the engine is *NOT* prefixed
* The default headers you specified in your MKNetworkEngine subclass gets added to the headers
* The params dictionary in this method gets attached to the URL as query parameters if the HTTP Method is GET/DELETE
* The params dictionary is attached to the body if the HTTP Method is POST/PUT
* This method can be over-ridden by subclasses to tweak the operation creation mechanism.
* You would typically over-ride this method to create a subclass of MKNetworkOperation (if you have one). After you create it, you should call [super prepareHeaders:operation] to attach any custom headers from super class.
* @seealso
* prepareHeaders:
*/
-(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
params:(NSDictionary*) body
httpMethod:(NSString*) method
timeoutInterval:(NSTimeInterval)timeoutInterval;

/*!
* @abstract adds the custom default headers
*
Expand Down Expand Up @@ -422,4 +459,5 @@
*/
@property (nonatomic, assign) BOOL shouldSendAcceptLanguageHeader;

@property (nonatomic, assign) NSTimeInterval timeoutInterval;
@end
23 changes: 20 additions & 3 deletions MKNetworkKit/MKNetworkEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ - (id) initWithHostName:(NSString*) hostName portNumber:(int)portNumber apiPath:

self.customOperationSubclass = [MKNetworkOperation class];
self.shouldSendAcceptLanguageHeader = YES;
self.timeoutInterval = kMKNetworkKitRequestTimeOutInSeconds;
}

return self;
Expand Down Expand Up @@ -352,7 +353,15 @@ -(MKNetworkOperation*) operationWithPath:(NSString*) path
-(MKNetworkOperation*) operationWithPath:(NSString*) path
params:(NSDictionary*) body
httpMethod:(NSString*)method
ssl:(BOOL) useSSL {
ssl:(BOOL) useSSL{
return [self operationWithPath:path params:body httpMethod:method ssl:useSSL timeoutInterval:self.timeoutInterval];
}

-(MKNetworkOperation*) operationWithPath:(NSString*) path
params:(NSDictionary*) body
httpMethod:(NSString*)method
ssl:(BOOL) useSSL
timeoutInterval:(NSTimeInterval)timeoutInterval{

if(self.hostName == nil) {

Expand All @@ -377,7 +386,7 @@ -(MKNetworkOperation*) operationWithPath:(NSString*) path
}


return [self operationWithURLString:urlString params:body httpMethod:method];
return [self operationWithURLString:urlString params:body httpMethod:method timeoutInterval:timeoutInterval];
}

-(MKNetworkOperation*) operationWithURLString:(NSString*) urlString {
Expand All @@ -396,7 +405,15 @@ -(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
params:(NSDictionary*) body
httpMethod:(NSString*)method {

MKNetworkOperation *operation = [[self.customOperationSubclass alloc] initWithURLString:urlString params:body httpMethod:method];
return [self operationWithURLString:urlString params:body httpMethod:method timeoutInterval:self.timeoutInterval];
}

-(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
params:(NSDictionary*) body
httpMethod:(NSString*)method
timeoutInterval:(NSTimeInterval)timeoutInterval{

MKNetworkOperation *operation = [[self.customOperationSubclass alloc] initWithURLString:urlString params:body httpMethod:method timeoutInterval:timeoutInterval];
operation.shouldSendAcceptLanguageHeader = self.shouldSendAcceptLanguageHeader;

[self prepareHeaders:operation];
Expand Down
7 changes: 6 additions & 1 deletion MKNetworkKit/MKNetworkOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,12 @@ typedef enum {
-(void) updateOperationBasedOnPreviousHeaders:(NSMutableDictionary*) headers;
-(NSString*) uniqueIdentifier;

- (instancetype)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)params
httpMethod:(NSString *)method;

- (instancetype)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)params
httpMethod:(NSString *)method;
httpMethod:(NSString *)method
timeoutInterval:(NSTimeInterval)timeoutInterval;
@end
17 changes: 15 additions & 2 deletions MKNetworkKit/MKNetworkOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ @interface MKNetworkOperation (/*Private Methods*/)

@property (strong, nonatomic) NSError *error;

- (instancetype)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)body
httpMethod:(NSString *)method;

- (instancetype)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)body
httpMethod:(NSString *)method;
httpMethod:(NSString *)method
timeoutInterval:(NSTimeInterval)timeoutInterval;

-(NSData*) bodyData;

Expand Down Expand Up @@ -609,9 +614,17 @@ -(void) addDownloadStream:(NSOutputStream*) outputStream {
[self.downloadStreams addObject:outputStream];
}

- (instancetype)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)params
httpMethod:(NSString *)method
{
return [self initWithURLString:aURLString params:params httpMethod:method timeoutInterval:kMKNetworkKitRequestTimeOutInSeconds];
}

- (id)initWithURLString:(NSString *)aURLString
params:(NSDictionary *)params
httpMethod:(NSString *)method
timeoutInterval:(NSTimeInterval)timeoutInterval

{
if((self = [super init])) {
Expand Down Expand Up @@ -659,7 +672,7 @@ - (id)initWithURLString:(NSString *)aURLString

self.request = [NSMutableURLRequest requestWithURL:finalURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:kMKNetworkKitRequestTimeOutInSeconds];
timeoutInterval:timeoutInterval];

[self.request setHTTPMethod:method];

Expand Down