YTKNetwork

//  .h
#import <YTKRequest.h>

@interface VideoAPI : YTKRequest
/**
 *  返回 video数据模型数组
 */
- (NSArray *)responseArray;
@end
//  .m
#import "VideoAPI.h"
#import "Video.h"
@implementation VideoAPI


- (NSString *)requestUrl {
    return @"video";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodGet;
}

- (NSInteger)cacheTimeInSeconds {
    return 60 * 3;
}

- (YTKRequestSerializerType)requestSerializerType {
    return YTKRequestSerializerTypeJSON;
}

- (id)jsonValidator {
    return @{@"videos":[NSArray class]};
}

- (NSArray *)responseArray {
    return [NSArray arrayWithArray:[Video videoWithJsonDict:self.responseJSONObject]];
}
@end
  • 登陆post请求
// .h
#import <YTKRequest.h>

@interface LoginAPI : YTKRequest
- (id)initWithUsername:(NSString *)username password:(NSString *)password;

- (NSString *)loginResult;
@end
//  .m
#import "LoginAPI.h"

@implementation LoginAPI
{
    NSString *_username;
    NSString *_password;
}

- (id)initWithUsername:(NSString *)username password:(NSString *)password {
    self = [super init];
    if (self) {
        _username = username;
        _password = password;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"login";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPost;
}

- (id)requestArgument {
    return @{
             @"username": _username,
             @"pwd": _password
             };
}
/**
 *  检测返回的数据  是否登陆成功
 */
- (id)jsonValidator {
    return @{
             @"success": [NSString class]
             };
}

- (NSString *)loginResult {


#if 1
    //  登陆成功
    if ([[self responseJSONObject] objectForKey:@"success"]) {

        return [[self responseJSONObject] objectForKey:@"success"];
    }
    else    //  登陆失败
        return [[self responseJSONObject] objectForKey:@"error"];
#endif





}

@end
  • 下载图片
//  .h
#import <YTKRequest.h>

@interface GetImageAPI : YTKRequest

- (id)initWithImageId:(NSString *)imageId;

- (NSString *)resumableDownloadPath;
@end
//  .m

#import "GetImageAPI.h"

@implementation GetImageAPI {
    NSString *_imageId;
}

- (id)initWithImageId:(NSString *)imageId {
    self = [super init];
    if (self) {
        _imageId = imageId;
    }
    return self;
}

- (NSString *)requestUrl {
    return [NSString stringWithFormat:@"resources/images/%@", _imageId];
}

- (BOOL)useCDN {
    return NO;
}

- (NSString *)resumableDownloadPath {
    NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *cachePath = [libPath stringByAppendingPathComponent:@"Caches"];
    NSString *filePath = [cachePath stringByAppendingPathComponent:_imageId];
    NSLog(@"\n%@",filePath);
    return filePath;
}


@end
  • 上传图片
// .h
#import <YTKRequest.h>

@interface UploadImageAPI : YTKRequest
- (id)initWithImage:(UIImage *)image;

- (NSString *)responseMessage;
@end
//  .m
#import "UploadImageAPI.h"

@implementation UploadImageAPI
{
    UIImage *_image;
}

- (id)initWithImage:(UIImage *)image {
    self = [super init];
    if (self) {
        _image = image;
    }
    return self;
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPost;
}

- (NSString *)requestUrl {
    return @"upload";
}

- (AFConstructingBlock)constructingBodyBlock {
    return ^(id<AFMultipartFormData> formData) {
        NSData *data = UIImageJPEGRepresentation(_image, 0.9);
        NSString *name = @"image";
        NSString *formKey = @"file";
        NSString *type = @"image/png";
        [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
    };
}

- (id)jsonValidator {
    return @{ @"success": [NSString class] };
}

- (NSString *)responseMessage {
    NSDictionary *dict = self.responseJSONObject;
    if (dict[@"success"]) {

        return dict[@"success"];
    }else
        return dict[@"error"];
}

@end
  • 上传gzip文件
//  .h 
#import <YTKRequest.h>

@interface UploadGzipAPI : YTKRequest
- (id)initWithGzipfilePath:(NSString *)gzipFilepath;

- (NSString *)responseMessage;
@end
//  .m
#import "UploadGzipAPI.h"

@implementation UploadGzipAPI
{
    NSData *_data;
}
- (id)initWithGzipfilePath:(NSString *)gzipFilepath
{
    self = [super init];
    if (self) {
        _data = [[NSData alloc]initWithContentsOfFile:gzipFilepath];
    }
    return self;
}

- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPost;
}

- (NSString *)requestUrl
{
    return @"upload";
}

- (AFConstructingBlock)constructingBodyBlock
{
    return ^(id<AFMultipartFormData> formData) {
      NSString *name    = @"name";
      NSString *formKey = @"file";
      NSString *type    = @"application/x-gzip";

      [formData appendPartWithFileData:_data name:formKey fileName:name mimeType:type];
    };
}

- (NSString *)responseMessage {
    NSDictionary *dict = self.responseJSONObject;
    if (dict[@"success"]) {

        return dict[@"success"];
    }else
        return dict[@"error"];
}
@end
/**
 *  发送批量的网络请求
 */
- (void)sendBatchRequestWithRequests:(NSArray *)requsets {

    NSMutableArray *imageIds = [NSMutableArray array];
    for (Video *vio in requsets) {

        [imageIds addObject:[[GetImageAPI alloc] initWithImageId:[[vio.image componentsSeparatedByString:@"/"] lastObject]]];

    }

    YTKBatchRequest *batchRequest = [[YTKBatchRequest alloc] initWithRequestArray:imageIds];
    [batchRequest startWithCompletionBlockWithSuccess:^(YTKBatchRequest *batchRequest) {
         NSLog(@"succeed");

        NSArray *result = batchRequest.requestArray;
//        for (GetImageAPI *imageI in result) {
//            NSLog(@"imagePath--%@",imageI.resumableDownloadPath);
//        }

        [result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            NSLog(@"imagePath--%@",((GetImageAPI *)obj).resumableDownloadPath);
        }];


    } failure:^(YTKBatchRequest *batchRequest) {
         NSLog(@"failed");
    }];


}
#pragma mark - 链式请求

/**
 *  链式请求

 */
- (void)sendChainRequest {

    LoginAPI *login = [[LoginAPI alloc] initWithUsername:@"520it" password:@"520it"];

    YTKChainRequest *chain = [[YTKChainRequest alloc] init];
    [chain addRequest:login callback:^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
       //   登陆成功后下载图片
        GetImageAPI *image = [[GetImageAPI alloc] initWithImageId:@"minion_16.png"];
        [chainRequest addRequest:image callback:nil];

    }];
    chain.delegate = self;
    //  开始发送请求
    [chain start];
}
//  所有请求完成
- (void)chainRequestFinished:(YTKChainRequest *)chainRequest {
    // all requests are done

}
//  某个请求失败
- (void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest *)request {
    // some one of request is failed
    NSLog(@"failed request %@",[request class]);
}

results matching ""

    No results matching ""