AYHttp 2.1.1

AYHttp 2.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2023年2月

Alan Yeh 维护。



 
依赖项
AFNetworking/Reachability~> 3.0
AFNetworking/Serialization~> 3.0
AFNetworking/Security~> 3.0
AFNetworking/NSURLSession~> 3.0
AYPromise>= 0
AYFile>= 0
AYQuery>= 0
AYCategory>= 0
 

AYHttp 2.1.1

  • 作者:
  • Alan Yeh

AYHttp

CI Status Version License Platform

引用

  使用 CocoaPods 可以方便地引入 AYHttp。在 Podfile 中添加 AYHttp 的依赖。

pod "AYHttp"

简介

  AYHttp 是基于 AFNetworking 的网络请求框架。使用 Promise 语法进行操作,可以使代码更清晰和方便。同时,AYHttp 简化了网络请求,使用起来非常简洁。

用例

GET请求

    [AYHttpClient executeRequest:AYGETRequest(@"https://api.github.com/search/repositories").withQueryParam(@"q", @"AYHttp")]
    .then(^(AYHttpResponse *response){
        //请求成功
        NSDictionary *result = response.responseJson;
        //其它业务
    }).catch(^(NSError *error){
        NSLog(error.localizedDescription);
    });

URL参数

Restful风格的API常常使用URL参数,AYHttp对此类URL进行了处理。

    [AYHttpClient executeRequest:AYGETRequest(@"https://api.douban.com/v2/book/{bookID}").withPathParam(@"bookID", @"1220562")]
    .then(^(AYHttpResponse *response){
        //请求成功
        NSDictionary *result = response.responseJson;
        //其它业务
    }).catch(^(NSError *error){
        NSLog(error.localizedDescription);
    });

AYHttp会自动将url中的{xxxx}替换成params中的对应key的参数值,同时将params对应的key移除。

文件上传

    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"zip"]];
    
    AYHttpRequest *uploadRequest = AYPOSTRequest(@"http://10.0.1.2:8080/MDDisk/file").withBodyParam(@"file", [AYHttpFileParam paramWithData:data andName:@"aaa.zip"]);
    
    [uploadRequest setUploadProgress:^(NSProgress * _Nonnull progress) {
        ///上传进度
        NSLog(@"%@", progress);
    }];
    
    [AYHttpClient executeRequest:uploadRequest]
    .then(^(AYHttpResponse *response){
        //上传成功
        NSDictionary *result = response.responseJson;
        //其它业务
    }).catch(^(NSError *error){
        //上传失败
        NSLog(error.localizedDescription);
    });

文件下载

文件下载分为两种类型:一种是普通下载,另一种是断点下载。

普通下载

使用executeRequest。下载成功后,在AYHttpResponse的responseData属性中获得数据。

断点下载

使用downloadRequest。下载成功后,在AYHttpResponse的responseFile属性中获得文件。

    AYHttpRequest *downloadReq = AYGETRequest(@"https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz");
    
    [downloadReq setDownloadProgress:^(NSProgress * _Nonnull progress) {
        //下载进度
        NSLog(@"%@", progress);
    }];
    
    [AYHttpClient downloadRequest:downloadReq].then(^(AYHttpResponse *response){
        //下载成功
        NSLog(@"%@", response.responseFile);
    }).catch(^(NSError *error){
        //下载失败
        NSLog(error.localizedDescription);
    });

断点续传

  • 暂停下载,使用then可以获取暂停后生成的暂存数据文件
    [AYHttpClient suspendDownloadRequest:downloadReq].then(^(AYFile *config){
        NSLog(@"%@", config);
    });
  • 断点续传,使用暂停下载时生成的暂存数据文件,可以继续下载。
    AYHttpRequest *request = nil;
    [AYHttpClient resumeDownloadRequest:&request withConfig:config].then(^(AYHttpResponse *response){
        //下载成功
        NSLog(@"%@", response.responseFile);
    }).catch(^(NSError *error){
        //下载失败
        NSLog(error.localizedDescription);
    });
    [request setDownloadProgress:^(NSProgress * _Nonnull progress) {
        //下载进度
        NSLog(@"%@", progress);
    }];

许可证

AYHttp 以 MIT 许可证可用。有关更多信息,请参阅 LICENSE 文件。