FJHttpClient 0.0.2

FJHttpClient 0.0.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2017年8月

jeff_njut 维护。



 
依赖
AFNetworking>= 0
JSONModel>= 0
YYModel>= 0
CocoaSecurity>= 0
 

  • jeff_njut

如何开始

FJHttpClient帮助实现高效RESTful API集成,并简化HTTP(s)请求的用法。FJHttpClient基于JSONModel、YYModel和AFNetwork,以实现请求的自动序列化和响应的解序列化。FJHttpClient支持在项目中安装库。

Podfile

要使用CocoaPods将FJHttpClient集成到您的Xcode项目中,请在Podfile中指定它:

pod 'FJHttpClient', :git => 'https://github.com/jeffnjut/FJHttpClient.git'

然后,运行以下命令:

$ pod install

如果有任何更新发生,请运行以下命令:

$ pod update

导入

导入FJHttpClient头文件

如果只包含HTTP(s)功能API,导入如下:

#import <FJHttpClient/FJHttpClient.h>

除了导入以下更多类别之外:

#import <FJHttpClient/FJHttpClientHeader.h>

用法

'FJHttpClientConfig' 对象定义

hostUrl                   // [Optional] Host url

timeout_non_multipart     // [Optional] Max time for a common post

timeout_multipart         // [Optional] Max time for a multi-part post

enableRelativePath        // [Optional] Whether full url or relative url enabled, Default is YES. 
                          // YES: method 'relativePath' must be implemented in sub-class of BaseRequest. Both full url and relative url(hostUrl must be given) are acceptable.
                          // NO : method 'relativePath' could be omitted.

headerFields              // [Optional] Set HTTP/HTTPS request header-fields

requestSerializer         // [Optional] AFNetworking Request Serializer 

responseSerializer        // [Optional] AFNetworking Response Serializer

buildCommonParamsBlock    // [Optional] A block for building common request parameters

processRelativePathBlock  // [Optional] A block for modifing relative url

enableLog                 // [Optional] Whether enable logging
                          // YES: printing log 
                          // NO : do not printing

jsonModelType             // [Optional] The Model for de-serializing json, default is JSONModel

创建一个 'FJHttpClientConfig' 对象

FJHttpClientConfig *config = [FJHttpClientConfig new];

config.hostUrl = @"ur url";
config.enableRelativePath = YES;
config.headerFields = nil;

config.buildCommonParamsBlock = ^(NSMutableDictionary **params){

    // get params for request
    NSMutableDictionary *mutableParam = *params;

    // the common paramters
    NSMutableDictionary *dict = @{@"xxx":@"token",
                                  @"320":@"screenwidth"}

    // combine params for request & the common paramters
    [mutableParam addEntriesFromDictionary:dict];
};

config.processRelativePathBlock = ^(NSMutableString **relativeUrl) {

    // get url for request
    NSMutableString *url = *relativeUrl;

    // modify the original url
    [url insertString:[NSString stringWithFormat:@"/%@",@"v5"] atIndex:0];
    NSString *cur = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [url replaceCharactersInRange:NSMakeRange(0, url.length) withString:cur];
};

然后,'FJHttpClient' 将与 'FJHttpClientConfig' 一同使用

FJHttpClient *client = [FJHttpClient sharedInstance:config];

基本请求/响应API是用来做什么的?

所有API的请求/响应模型都必须继承自BaseRequest/BaseResponse。您会发现在BaseRequest/BaseResponse模型也已继承自JSONModel,目的是为了自动将HTTP(s)响应解序列化到您的API响应模型。

BaseRequest

@interface BaseRequest : JSONModel

- (NSString*)relativePath;

@end
@implementation BaseRequest

- (NSString*)relativePath {
return nil;
}

@end

BaseResponse

@interface BaseResponse : JSONModel

@end
@implementation BaseResponse

@end

定义自定义请求/响应API

请求API
@interface APICommonDictRequest : BaseRequest

@end

// If enableRelativePath is YES, relativePath method implemetation is a MUST.
@implementation APICommonDictRequest

- (NSString *)relativePath {
    return @"/?m=index&c=data_dict";
}

@end
响应JSON格式
{
    "code": 0,
    "msg": "awesome, that's correct!!!",
    "data": {
                "name": "jeff",
                "age": 20
            }
}
响应对象
@class Person;

@interface APICommonDictResponse : BaseResponse

// Fill model corresponding to json response
@property (nonatomic, assign) int code;
@property (nonatomic, copy) NSString *msg;
@property (nonatomic, strong) Person *data;

@end

@interface Person : JSONModel

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;

@end

发布请求

[[FJHttpClient sharedInstance] post:[APICommonDictRequest new] multipart:nil callback:^(id data, NSError *error) {
    NSLog(@"data : %@  error : %@", data, error);
}];

Multipart-Post

@interface MultipartData : NSObject

@property (nonatomic, copy) NSString *name;       // 参数名

@property (nonatomic, copy) NSString *fileName;   // 文件名

@property (nonatomic, copy) NSString *mineType;   // 比如图片"image/jpg"

@property (nonatomic, strong) NSData *data;       // 比如图片的Data

@end

GET 示例(从 itune connect 获取应用信息)

主要
FJHttpClientConfig *config = [FJHttpClientConfig new];

config.enableRelativePath = YES;

config.responseSerializer = [AFJSONResponseSerializer serializer];

config.jsonModelType = JsonModelType_YYModel;

FJHttpClient *client = [FJHttpClient sharedInstance:config];

GetAppInfoRequest *getAppRequest = [GetAppInfoRequest new];

getAppRequest.bundleId = @"ur app bundle id";

[client get:getAppRequest callback:^(id data, NSError *error) {
    NSLog(@"data : %@  error : %@", data, error);
}];
请求/响应 API
@interface GetAppInfoRequest : BaseRequest

@property (nonatomic, copy) NSString *bundleId;

@end
@implementation GetAppInfoRequest

- (NSString *)relativePath {
    return @"https://itunes.apple.com/lookup";
}

@end
@interface GetAppInfoResponse : BaseResponse

@property (nonatomic, assign) int resultCount;
@property (nonatomic, strong) NSMutableArray<AppInfoResult, Optional> *results;

@end
@implementation GetAppInfoResponse

@end
模型
@class AppInfoResult;
@protocol AppInfoResult;
@protocol NSString;

@interface AppInfoResult : JSONModel
@property (nonatomic, copy) NSString<Optional> *artworkUrl512;
@property (nonatomic, copy) NSString<Optional> *artistViewUrl;
@property (nonatomic, copy) NSString<Optional> *kind;
@property (nonatomic, strong) NSMutableArray<NSString,Optional> *screenshotUrls;
@property (nonatomic, copy) NSString<Optional> *trackCensoredName;
@property (nonatomic, copy) NSString<Optional> *sellerName;
@property (nonatomic, copy) NSString<Optional> *version;
@property (nonatomic, copy) NSString<Optional> *formattedPrice;
@property (nonatomic, copy) NSString<Optional> *description;

@end

@protocol AppInfoResult

@end

@protocol NSString

@end
@implementation AppInfoResult

@end

贡献

如有需要帮助或有错误,请打开一个问题或拉取请求。

联系

待办事项

  • 文档

许可

FJHttpClient 在 MIT 许可证下可用。更多信息见 LICENSE 文件。

MIT 许可证(MIT)

版权所有 (c) 2017 Jeff

在此特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人免费处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,以及允许向软件提供软件的人员行使这些权利,前提是以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,不容许作者或版权所有者对任何索赔、损害或其他责任负责,无论这种责任属于合同、侵权或其他责任,无论源于、包括或其他与软件或软件的使用或其他交易有关。