Coder Social home page Coder Social logo

ayhttp's Introduction

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.

ayhttp's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.