DAL
Pull request Compare This branch is 63 commits behind 111minutes:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
DXDAL.xcodeproj
DXDAL.xcworkspace
DXDAL
DXDALTests
TestProject
.gitignore
Podfile
README.md

README.md

Using DXDAL

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];
}

Default parameters

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"];
}];

Requestst definition

GET Request

- (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];

    }];
}

POST Request

- (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]];
    }];
}

ModelMapper

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;
}

Using request example

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];

Progress

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];