At first you should create request factory
@interface MyRequestFactory : DXDALRequestFactory
where you should override two methods:
- (id <DXDALDataProvider>)getDataProvider;
- (Class)getDefaultRequestClass;
for example:
- (id<DXDALDataProvider>)getDataProvider {
return [[DXDALDataProviderHTTP alloc] initWithBaseURL:[NSURL URLWithString:@"http://google.com"]];
}
- (Class)getDefaultRequestClass {
return [DXDALRequestHTTP class];
}
use
- (void)addDefaultConfig:(DXDALRequestConfigurationBlock)configBlock;
ัะพ add default configuration blocks for every request, for example:
MyRequestFactory *factory = [MyRequestFactory sharedInstance];
[factory addDefaultConfig:^(id request) {
[request addParam:user.userID withName:@"user_id"];
}];
- (DXDALRequest *)getLastUpdateDate {
return [self buildRequestWithConfigBlock:^(DXDALRequestHTTP *request) {
request.httpMethod = @"GET";
request.httpPath = @"get_last_updated"; // method
[request addParam:somedate withName:@"date"];
request.parser = [DXDALParserJSON new];
request.mapper = [MDUpdateDateMapper new];
request.entityClass = [NSString class];
}];
}
- (DXDALRequest*)loginUserName:(NSString *)userName password:(NSString *)password {
return [self buildRequestWithConfigBlock:^(DXDALRequestHTTP *request) {
request.httpMethod = @"POST";
request.httpPath = @"check_credentials"; //metod
[request addParam:userName withName:@"username"];
[request addParam:password withName:@"password"];
request.parser = [DXDALParserJSON new];
request.mapper = [UserModelMapper new];
request.entityClass = [User class];
[request addErrorHandler:[self standartErrorHandler]];
}];
}
it's class which implements DXDALMapper protocol with method:
- (id)mapFromInputData:(id)inputData withClass:(Class)mappingClass;
where inputData - it's parsed dictionary from server response, for example:
- (id)mapFromInputData:(id)inputData withClass:(Class)mappingClass {
User *user = [[User alloc] init];
user.firstName = [inputData objectForKey:@"first_name"];
user.lastName = [inputData objectForKey:@"last_name"];
return user;
}
DXDALRequestSuccesHandler successLogin = ^(id response){
User *user = response;
NSLog(@"user name: %@ %@", user.firstName, user.lastName);
}
MDAuthRequestFactory *factory = [MDAuthRequestFactory sharedInstance];
DXDALRequest *request = [factory loginUserName:_loginField.text password:_passwordField.text];
[request addSuccessHandler:successLogin];
[request addErrorHandler:^(NSError *error)
{
NSLog(@"Login request error: %@", [error description]);
}];
[request start];
You can add progress block:
- (void)addProgressHandler:(DXDALProgressHandler)handler;
it will returns progressValue and progressDelta, for example:
DXDALProgressHandler progressHandler = ^(float progress, float progressDelta) {
NSLog(@"progress: %f, progressDelta: %f", progress, progressDelta);
};
[request addProgressHandler:progressHandler];