FARequest 1.0.3

FARequest 1.0.3

fadi维护。



FARequest 1.0.3

  • fadizant

FARequest

CI Status Version License Platform

示例

要运行示例项目,请克隆仓库,然后从Example目录中首先运行 pod install。如果您需要测试上传文件,可能需要设置您的Apple帐户权限以用于iCloud服务。

特性

易于使用

FARequest使用闭包来发送您的请求,无需从它创建实例。

默认配置

您可以设置默认配置,以使请求更容易使用。

状态处理器

您可以通过为每个状态设置键来轻松处理响应状态代码。当这些状态发生时,使用您的键通过通知中心进行处理。您还可以向原始闭包发送新的响应,以继续您的工作。

队列请求

在FARequest中,您可以使用FAQueueRequest创建队列,并且可以随时停止队列。

上传和下载文件带可选进度

FARequest使上传和下载文件变得比以往任何时候都要简单,您可以立即发送图片或设置文件URL或任何文件数据,并让FARequest为您完成工作。FARequest可以下载任何文件并将其保存到应用程序文档,您可以通过停止覆盖以获取当前是否存在文件来随时获取此文件。您还可以使用FAQueueRequest功能上传和下载文件。

JSON响应

如果响应是JSON格式,FARequest将返回响应作为JSON;如果不是,则将按原样返回。

示例

简单请求
// Objc
[FARequest sendWithRequestObject:[[FARequestObject alloc] initWithUrl:[[NSURL alloc] initWithString:@"https://jsonplaceholder.typicode.com/posts/1"]] RequestCompleted:^(FAResponse *response) {
        if (response.responseCode == FAResponseStatusOK) {
            NSLog([response.JSONResult description]);
        }
    }];
    
// Swift4
FARequest.send(with: FARequestObject.init(url: URL.init(string: "https://jsonplaceholder.typicode.com/posts/1"))) { (response) in
            if response?.responseCode == FAResponseStatusOK {
                print((response?.jsonResult as AnyObject).description)
            }
        }
默认配置
// Objc
[FARequest setDefaultConfiguration:[[FARequestConfiguration alloc]initWithRequestType:FARequestTypeGET  Header:nil Object:nil UseCashe:NO  Encoding:NO TimeOut:120]];
    
// Swift4
FARequest.setDefaultConfiguration(FARequestConfiguration(requestType: .GET, header: nil, object: nil, useCashe: false, encoding: false, timeOut: 120))
状态处理器
// Objc
[FARequest setStatusHandler:@{@FAResponseStatusOK:@"successful"}];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(successful:)
                                             name:[FARequest notificationNameFromKey:@FAResponseStatusOK]
                                           object:nil];

// notification center method
- (void) successful:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:[FARequest notificationNameFromKey:@FAResponseStatusOK]]){
        FARequestObject *request = [notification.userInfo objectForKey:FARequestNotificationRequest];
        FAResponse *response = [notification.userInfo objectForKey:FARequestNotificationResponse];
        requestCompleted requestCompleted = [notification.userInfo objectForKey:FARequestNotificationRequestCompleted];
        // your work here
    }
}


// Swift4
FARequest.setStatusHandler([FAResponseStatusOK:"successful"])
NotificationCenter.default.addObserver(self, selector: #selector(successful(_:)), name: NSNotification.Name(rawValue: FARequest.notificationName(fromKey: FAResponseStatusOK as NSNumber)), object: nil)

// notification center method
@objc func successful(_ notification:NSNotification){
        if notification.name == NSNotification.Name(rawValue: FARequest.notificationName(fromKey: FAResponseStatusOK as NSNumber)) {
            let request = userInfo.object(forKey: FARequestNotificationRequest) as? FARequestObject
            let response = userInfo.object(forKey: FARequestNotificationResponse) as? FAResponse
            let requestCompleted = userInfo.object(forKey: FARequestNotificationRequestCompleted) as? requestCompleted
            // your work here
        }
    }
排队请求
// Objc
FARequestObject *requestObject1 = [[FARequestObject alloc] initWithUrl:[[NSURL alloc] initWithString:@"https://jsonplaceholder.typicode.com/posts/1"]];
    requestObject1.configuration.object = @1;
    
    FARequestObject *requestObject2 = [[FARequestObject alloc] initWithUrl:[[NSURL alloc] initWithString:@"https://jsonplaceholder.typicode.com/posts/1"]];
    requestObject2.configuration.object = @2;
    
    FARequestObject *requestObject3 = [[FARequestObject alloc] initWithUrl:[[NSURL alloc] initWithString:@"https://jsonplaceholder.typicode.com/posts/1"]];
    requestObject3.configuration.object = @3;
    
    FAQueueRequest * queue = [[FAQueueRequest alloc] initWithQueue:[[NSMutableArray alloc] initWithArray:@[requestObject1,requestObject2,requestObject3]]];
    
    [queue sendWithRequestCompleted:^(FARequestObject *request, FAResponse *response) {
        NSLog([response.JSONResult description]);
    } Completed:^(BOOL successful) {
        NSLog(@"Finished %d",successful);
    } Stopped:^{
        NSLog(@"Stopped");
    }];
    
// Swift4
let requestObject1 = FARequestObject.init(url: URL.init(string: "https://jsonplaceholder.typicode.com/posts/1"))
        requestObject1?.configuration.object = 1
        
        let requestObject2 = FARequestObject.init(url: URL.init(string: "https://jsonplaceholder.typicode.com/posts/1"))
        requestObject2?.configuration.object = 2
        
        let requestObject3 = FARequestObject.init(url: URL.init(string: "https://jsonplaceholder.typicode.com/posts/1"))
        requestObject3?.configuration.object = 3
        
        
        let queue = FAQueueRequest.init(queue: NSMutableArray(array: [requestObject1 ?? FARequestObject(),
                                                                      requestObject2 ?? FARequestObject(),
                                                                      requestObject3 ?? FARequestObject()]));
        
        queue?.send(requestCompleted: { (request,response) in
            print((response?.jsonResult as AnyObject).description)
        }, completed: { (finish) in
            self.textView.text.append("\nFinished \(finish)")
        },stopped: {
            print("Stopped")
        })
上传
// Objc
FARequestObject *request = [[FARequestObject alloc] initWithUrl:[[NSURL alloc] initWithString:@"SERVER_URL"]];
request.configuration.requestType = FARequestTypePOST;
//set image info 
FARequestMediaFile *media1 = [[FARequestMediaFile alloc] initWithName:@"file" Image:image];
request.mediaFiles = @[media1];

[FARequest sendWithRequestObject:request RequestProgress:^(float progress) {
    NSLog(@"progress = %f",progress);
} RequestCompleted:^(FAResponse *response) {
    if (response.responseCode == FAResponseStatusOK) {
        NSLog([response.JSONResult description]);
    }
}];
    
// Swift4
let request = FARequestObject.init(url: URL.init(string: "http://168.144.38.45:8097/NewController/addMedia"))
request?.configuration.requestType = .POST
request?.mediaFiles = [FARequestMediaFile.init(name: "file", image: image)]
FARequest.send(with: request, requestProgress: { (progress) in
    print("progress = \(progress)")
}) { (response) in
    if response?.responseCode == FAResponseStatusOK {
        print((response?.jsonResult as AnyObject).description)
    }
}
下载
// Objc
[FARequest getWithRequestObject:[[FARequestObject alloc] initWithUrl:[NSURL URLWithString:@"http://mirrors.standaloneinstaller.com/video-sample/jellyfish-25-mbps-hd-hevc.mp4"]
                                                                    SaveInFolder:@"Download"
                                                                    SaveWithName:@"video"
                                                               SaveWithExtension:@"mp4"
                                                                       OverWrite:YES]
                            RequestProgress:^(float progress) {
                                NSLog(@"progress = %f",progress);
                            } RequestCompleted:^(FAResponse *response) {
                                if (response.responseCode == FAResponseStatusOK) {
                                    NSLog([response.JSONResult description]);
                                }
                            }];
    
// Swift4
FARequest.getWith(FARequestObject.init(url: URL.init(string: "http://mirrors.standaloneinstaller.com/video-sample/jellyfish-25-mbps-hd-hevc.mp4"),
                                               saveInFolder: "Download",
                                               saveWithName: "video",
                                               saveWithExtension: "mp4",
                                               overWrite: true),
                        requestProgress: { (progress) in
                            print("progress = \(progress)")
                        }) { (respons) in
                            if response?.responseCode == FAResponseStatusOK {
                                print((response?.jsonResult as AnyObject).description)
                            }
                        }

安装

FARequest 通过 CocoaPods 提供使用。要安装它,只需在 Podfile 中添加以下行

pod "FARequest"

作者

fadizant, [email protected]

许可

FARequest采用MIT许可。请参阅LICENSE文件获取更多信息。