AYHttp 2.1.1

AYHttp 2.1.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Feb 2023

Maintained by Alan Yeh.



 
Depends on:
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

  • By
  • 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 Param

  Restful风格的api常常使用URL param,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);
    }];

License

AYHttp is available under the MIT license. See the LICENSE file for more info.