ESAPIClient 1.6.1

ESAPIClient 1.6.1

Elf Sundae 维护。



ESAPIClient

Build status CocoaPods CocoaPods platforms

ESAPIClient 是一个基于 AFNetworking 开发的 API 客户端库。

要求

  • 最低部署目标:iOS 9.0,macOS 10.10,watchOS 2.0,tvOS 9.0

安装

pod 'ESAPIClient'

如果您正在使用 AFNetworking 3.x,您需要在 Podfile 中 Trunk 源之前添加我的 spec-repo 源 URL

source 'https://github.com/ElfSundae/CocoaPods-Specs.git'
source 'https://cdn.cocoapods.org/'

target 'Example' do
    pod 'ESAPIClient'
end

用法

为您的应用程序设置默认 API 客户端

NSURL *baseURL = [NSURL URLWithString:@"https://api.github.com"];
ESAPIClient *client = [[ESAPIClient alloc] initWithBaseURL:baseURL];
client.fileMultipartName = @"uploadFile";
ESAPIClient.defaultClient = client;

发送API请求

GET

[ESAPIClient.defaultClient GET:@"api/path" parameters:@{ @"foo": @"bar" } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    //
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //
}];

上传文件

[ESAPIClient.defaultClient uploadFile:fileURL
                                   to:@"upload/avatar"
                           parameters:@{ @"foo": @"bar" }
                             progress:^(NSProgress * _Nonnull progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        progressView.progress = progress.fractionCompleted;
    });
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    //
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //
}];

下载文件

[ESAPIClient.defaultClient download:@"download/file.zip" toFile:destFile parameters:nil progress:nil success:^(NSURLSessionDownloadTask * _Nonnull task, NSURL * _Nonnull filePath) {
    //
} failure:^(NSURLSessionDownloadTask * _Nullable task, NSError * _Nonnull error) {
    //
}];

[ESAPIClient.defaultClient download:@"https://example.com/file.zip" toDirectory:destDir parameters:nil progress:nil success:^(NSURLSessionDownloadTask * _Nonnull task, NSURL * _Nonnull filePath) {
    //
} failure:^(NSURLSessionDownloadTask * _Nullable task, NSError * _Nonnull error) {
    //
}];

授权

ESAPIClient遵循MIT授权协议。请参阅LICENSE文件获取更多信息。