All Projects → Suzhibin → Zbnetworking

Suzhibin / Zbnetworking

Licence: mit
AFNetworking4.X封装 GET/POST /PUT/PATCH /DELETE / Upload /DownLoad 网络请求 添加了请求缓存,断点下载,显示缓存大小,删除缓存,取消当前请求等功能

Projects that are alternatives of or similar to Zbnetworking

Sjnetwork
SJNetwork is a high level network request tool based on AFNetworking and inspired on YTKNetwork.
Stars: ✭ 231 (+24.19%)
Mutual labels:  cache, afnetworking
KJNetworkPlugin
🎡A lightweight but powerful Network library. Network Plugin, Support batch and chain operation. 插件版网络架构
Stars: ✭ 43 (-76.88%)
Mutual labels:  cache, afnetworking
Ppnetworkhelper
AFNetworking 3.x 与YYCache封装
Stars: ✭ 1,586 (+752.69%)
Mutual labels:  cache, afnetworking
Holster
A place to keep useful golang functions and small libraries
Stars: ✭ 166 (-10.75%)
Mutual labels:  cache
Imagepipeline
Folio Image Pipeline is an image loading and caching framework for iOS clients
Stars: ✭ 170 (-8.6%)
Mutual labels:  cache
Dyld cache extract
A macOS utility to extract dynamic libraries from the dyld_shared_cache of macOS and iOS.
Stars: ✭ 180 (-3.23%)
Mutual labels:  cache
Wechatvideocourse
《微信公众号+小程序快速开发》视频教程课件及代码
Stars: ✭ 185 (-0.54%)
Mutual labels:  cache
Springbootlearning
《Spring Boot教程》源码
Stars: ✭ 2,065 (+1010.22%)
Mutual labels:  cache
Phpfastcache
A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
Stars: ✭ 2,171 (+1067.2%)
Mutual labels:  cache
Liget
NuGet server and cache running on kestrel in docker
Stars: ✭ 177 (-4.84%)
Mutual labels:  cache
Ansible Role Redis
Ansible Role - Redis
Stars: ✭ 176 (-5.38%)
Mutual labels:  cache
Tmp Cache
A least-recently-used cache in 35 lines of code~!
Stars: ✭ 170 (-8.6%)
Mutual labels:  cache
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (-2.69%)
Mutual labels:  cache
Http Cache Semantics
RFC 7234 in JavaScript. Parses HTTP headers to correctly compute cacheability of responses, even in complex cases
Stars: ✭ 169 (-9.14%)
Mutual labels:  cache
Thinkgo
A lightweight MVC framework written in Go (Golang).
Stars: ✭ 184 (-1.08%)
Mutual labels:  cache
Hitchcock
The Master of Suspense 🍿
Stars: ✭ 167 (-10.22%)
Mutual labels:  cache
Recent Images
Do you noticed the new feature of Telegram or Instagram?! They show your latest images when you try to attach or post a picture. So I developed this library the same with lots of customization. Simple way to get all images of device based on date taken, name, id and other customization
Stars: ✭ 182 (-2.15%)
Mutual labels:  cache
Flatcache
Implementation of Soroush Khanlou's Flat Cache.
Stars: ✭ 173 (-6.99%)
Mutual labels:  cache
Ktvhttpcache
A powerful media cache framework.
Stars: ✭ 2,113 (+1036.02%)
Mutual labels:  cache
Akavache
An asynchronous, persistent key-value store created for writing desktop and mobile applications, based on SQLite3. Akavache is great for both storing important data as well as cached local data that expires.
Stars: ✭ 2,185 (+1074.73%)
Mutual labels:  cache

ZBNetworking    介绍文档

本来想发布到cocoaPods的发现名字已经被使用了,也不想改名了。大家就手动下载用吧

变更日志

优点:

1.请求类型丰富 /*GET请求//*POST请求//*PUT请求//*PATCH请求//*DELETE请求//*Upload请求//*DownLoad请求/

2.低耦合,易扩展。

3.通过Block配置信息,有Block回调,delegate回调 ,支持公共配置;

4.请求参数parameters 支持字典,数组,字符串等类型

5.有插件机制 可以统一 预处理 所有 请求,响应,错误 处理逻辑的方法

6.内存缓存,沙盒缓存,有缓存文件过期机制 默认一周

7.显示缓存大小/个数,全部清除缓存/单个文件清除缓存/按时间清除缓存/按路径清除缓存  方法多样  并且都可以自定义路径   可扩展性强

8.有缓存key过滤功能

9.DownLoad支持断点下载 ,批量请求等功能

10.多种请求缓存类型的判断。也可不遵循,自由随你定。

  /**
     重新请求:   不读取缓存,不存储缓存
     没有缓存需求的,单独使用
     */
    ZBRequestTypeRefresh,
    
    /**
     重新请求:   不读取缓存,但存储缓存
     可以与 ZBRequestTypeCache 配合使用
     */
    ZBRequestTypeRefreshAndCache,
    /**
     读取缓存:   有缓存,读取缓存--无缓存,重新请求并存储缓存
     可以与ZBRequestTypeRefreshAndCache 配合使用
     */
    ZBRequestTypeCache,
    /**
     重新请求:  上拉加载更多业务,不读取缓存,不存储缓存
     用于区分业务 可以不用
     */
    ZBRequestTypeRefreshMore,

11.可见的缓存文件

使用

公共配置

 /**
     基础配置
     需要在请求之前配置,设置后所有请求都会带上 此基础配置
     */
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"github"] = @"https://github.com/Suzhibin/ZBNetworking";
    parameters[@"jianshu"] = @"https://www.jianshu.com/p/55cda3341d11";
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.2f",timeInterval];
    parameters[@"timeString"] =timeString;//时间戳

    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    headers[@"Token"] = @"Token";
    
    [ZBRequestManager setupBaseConfig:^(ZBConfig * _Nullable config) {
         /**
         config.baseServer 设置基础服务器地址
         如果同一个环境,有多个服务器地址,可以在每个请求单独设置 requestr.server  优先级大于config.baseServer
         */
        config.baseServer=server_URL;
        /**
         config.parameters公共参数
         如果同一个环境,有多个服务器地址,公共参数不同有两种方式
         1.在每个请求单独添加parameters
         2.在插件机制里 预处理 请求。判断对应的server添加
         */
        config.parameters=parameters;
        // filtrationCacheKey因为时间戳是变动参数,缓存key需要过滤掉 变动参数,如果 不使用缓存功能 或者 没有变动参数 则不需要设置。
        [email protected][@"timeString"];
        config.headers=headers;//请求头
        config.requestSerializer=ZBJSONRequestSerializer; //全局设置 请求格式 默认JSON
        config.responseSerializer=ZBJSONResponseSerializer; //全局设置 响应格式 默认JSON
        config.timeoutInterval=15;//超时时间  优先级 小于 单个请求重新设置
        config.retryCount=2;//请求失败 所有请求重新连接次数
        config.consoleLog=YES;//开log
        [email protected][@"text/aaa",@"text/bbb"];//添加新的响应数据类型
        /**
         内部已存在的响应数据类型
         @"text/html",@"application/json",@"text/json", @"text/plain",@"text/javascript",@"text/xml",@"image/*",@"multipart/form-data",@"application/octet-stream",@"application/zip"
         */
    }];

插件机制

    /**
       插件机制
       自定义 所有 请求,响应,错误 处理逻辑的方法

       比如 1.自定义缓存逻辑 感觉ZBNetworking缓存不好,想使用yycache 等
           2.自定义响应逻辑 服务器会在成功回调里做 返回code码的操作
           3.一个应用有多个服务器地址,可在此进行配置
           4.统一loading 等UI处理
           5.为某个服务器单独添加参数
           6. ......
       */
    [ZBRequestManager setRequestProcessHandler:^(ZBURLRequest * _Nullable request, id  _Nullable __autoreleasing * _Nullable setObject) {
         NSLog(@"请求之前");
         if ([request.server isEqualToString:m4_URL]) {
            //为某个服务器 单独添加公共参数
            [request.parameters setValue:@"pb这个参数,只会在下载文件的参数里显示" forKey:@"pb"];
        }
    }];
    
    [ZBRequestManager setResponseProcessHandler:^id(ZBURLRequest * _Nullable request, id  _Nullable responseObject, NSError * _Nullable __autoreleasing * _Nullable error) {
        NSLog(@"成功回调 数据返回之前");
        NSArray * authors=responseObject[@"authors"]
        NSString * errorCode= [[authors objectAtIndex:0]objectForKey:@"errorCode"];
         if ([orCode isEqualToString:@"401"]) {//假设401 登录过期
             NSDictionary *userInfo = @{NSLocalizedDescriptionKey:@"登录过期"};
             NSLog(@"重新开始业务请求:%@ 参数:%@",request.url,request.parameters[@"path"]);
              //⚠️给*error指针 错误信息,网络请求就会走 失败回调
               *error = [NSError errorWithDomain:NSURLErrorDomain code:[errorCode integerValue] userInfo:userInfo];
          }
    }];
    [ZBRequestManager setErrorProcessHandler:^(ZBURLRequest * _Nullable request, NSError * _Nullable error) {
        if (error.code==NSURLErrorCancelled){
            NSLog(@"请求取消❌------------------");
        }else if (error.code==NSURLErrorTimedOut){
            NSLog(@"请求超时");
        }else{
            NSLog(@"请求失败");
        }
    }];

Block 请求方法

//请求方法 会默认创建缓存路径    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"path"] = @"HomeViewController";
    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    headers[@"headers"] = @"herader";
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.url=list_URL;
        request.methodType=ZBMethodTypeGET;//默认为GET
        request.apiType=ZBRequestTypeRefresh;//(默认为ZBRequestTypeRefresh 不读取缓存,不存储缓存)
        request.parameters=parameters;//支持 字典、数组、字符串 等类型 。
        request.headers=headers;
        /**
         保留第一次或最后一次请求结果 只在请求时有用  读取缓存无效果。默认ZBResponseKeepNone 什么都不做
         使用场景是在 重复点击造成的 多次请求,如发帖,评论,搜索等业务
         */
        request.keepType=ZBResponseKeepNone;
        [email protected][@""];//与basefiltrationCacheKey 兼容
        request.requestSerializer=ZBJSONRequestSerializer; //单次请求设置 请求格式 默认JSON,优先级大于 全局设置,不影响其他请求设置
        request.responseSerializer=ZBJSONResponseSerializer; //单次请求设置 响应格式 默认JSON,优先级大于 全局设置,不影响其他请求设置
        request.retryCount=2;//请求失败 当次请求重新连接次数
        request.timeoutInterval=10;//默认30 //优先级 高于 全局设置,不影响其他请求设置
      
    }  success:^(id responseObj,ZBURLRequest * request){
        if (request.apiType==ZBRequestTypeRefresh) 
             //结束刷新
        }
        if (request.apiType==ZBRequestTypeLoadMore) {
            //结束上拉加载
        }
        //请求成功
          NSLog(@"得到数据:%@",responseObject);
      
    } failure:^(NSError *error){
    }];

Delegate 请求方法

  [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
       request.url=listUrl;
       request.apiType=type;
  } target:self];//ZBURLRequestDelegate
  
#pragma mark - ZBURLRequestDelegate
- (void)request:(ZBURLRequest *)request successForResponseObject:(id)responseObject{
        if (request.apiType==ZBRequestTypeRefresh) 
             //结束刷新
        }
        if (request.apiType==ZBRequestTypeLoadMore) {
            //结束上拉加载
        }
        //请求成功
          NSLog(@"得到数据:%@",responseObject);
}
- (void)request:(ZBURLRequest *)request failedForError:(NSError * _Nullable)error{
}
- (void)request:(ZBURLRequest *)request forProgress:(NSProgress *)progress{
    NSLog(@"onProgress: %.f", 100.f * progress.completedUnitCount/progress.totalUnitCount);
}
- (void)request:(ZBURLRequest *)request finishedForResponseObject:(id)responseObject forError:(NSError *)error{
//    NSLog(@"code:%ld",error.code);
//    NSLog(@"URLString:%@",request.URLString);
}

断点下载

    [ZBRequestManager requestWithConfig:^(ZBURLRequest * request) {
        [email protected]"https://URL";
        request.methodType=ZBMethodTypeDownLoad;
        request.downloadState=ZBDownloadStateStart;//开始 //ZBDownloadStateStop暂停
    } progress:^(NSProgress * _Nullable progress) {
        NSLog(@"onProgress: %.2f", 100.f * progress.completedUnitCount/progress.totalUnitCount);
    } success:^(id  responseObject,ZBURLRequest * request) {
        NSLog(@"ZBMethodTypeDownLoad 此时会返回存储路径文件: %@", responseObject);
         //在任何地方拿到下载文件
        NSString *file=[ZBRequestManager getDownloadFileForKey:request.url];
    } failure:^(NSError * _Nullable error) {
        NSLog(@"error: %@", error);
    }];

批量请求

 [ZBRequestManager sendBatchRequest:^(ZBBatchRequest *batchRequest)
            for (NSString *urlString in offlineArray) {
            ZBURLRequest *request=[[ZBURLRequest alloc]init];
            request.url=urlString;
            [batchRequest.urlArray addObject:request];
        }
    }  success:^(id responseObj,ZBURLRequest * request){
    } failure:^(NSError *error){
    } finished:^(NSArray * _Nullable responseObjects, NSArray<NSError *> * _Nullable errors, NSArray<ZBURLRequest *> * _Nullable requests) {
            NSLog(@"批量完成事件");
    }];

//具体演示看demo

取消请求

 //取消当前请求
 [ZBRequestManager cancelRequest:identifier];
 //取消批量请求
 [ZBRequestManager cancelBatchRequest:batchRequest];
 //取消所有请求
 [ZBRequestManager cancelAllRequest];

缓存相关

 //显示缓存大小 可以自定义路径
 [[ZBCacheManager sharedInstance]getCacheSize];
  //显示缓存个数  可以自定义路径
 [[ZBCacheManager sharedInstance]getCacheCount];
 //清除沙盒缓存
 [[ZBCacheManager sharedInstance]clearCache];
 //清除内存缓存
 [[ZBCacheManager sharedInstance]clearMemory];
  //清除单个缓存
 [[ZBCacheManager sharedInstance]clearCacheForkey:list_URL];
 //按时间清除缓存
  [[ZBCacheManager sharedInstance]clearCacheForkey:menu_URL time:60*60];
  //按路径清除缓存
 [[ZBCacheManager sharedInstance]clearDiskWithpath:@"路径" completion:nil];

缓存key过滤

   NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
   NSTimeInterval a=[dat timeIntervalSince1970];
   NSString *timeString = [NSString stringWithFormat:@"&time=%f", a];
   
    // 使用了parameters 的请求 缓存key会是URLString+parameters,parameters里有是时间戳或者其他动态参数,key一直变动 无法拿到缓存。所以定义一个 filtrationCacheKey 过滤掉parameters 缓存key里的 变动参数比如 时间戳
   [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
       [email protected]"http://URL";
       request.methodType=ZBMethodTypePOST;//默认为GET
       [email protected]{@"1": @"one", @"2": @"two", @"time": @"12345667"};
       [email protected][@"time"];//过滤掉time
   }success:nil failure:nil];

License

ZBNetworking is released under the MIT license. See LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].