单军华
2018-05-04 7db6ba57b17fb7c0c364219750b14ee3d5cc351b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
//  MCURLResponse.m
//  MCDownloadManager
//
//  Created by 马超 on 16/9/21.
//  Copyright © 2016年 qikeyun. All rights reserved.
//
 
#import "MCURLResponse.h"
 
@interface MCURLResponse()
@property (nonatomic, copy, readwrite, nullable) id responseObject;
@property (nonatomic, copy, readwrite) NSDictionary *requestParameter; // 这里封装的应该是请求封装,请求封装中能够拿到必要的配置信息,
@property (nonatomic, assign, readwrite, getter=isCache) BOOL cache;
@property (nonatomic, strong, readwrite, nullable) NSURLSessionDataTask *task;
@property (nonatomic, strong, readwrite, nullable) NSError *error;
@end
 
@implementation MCURLResponse
 
- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.status = MCURLResponseStatusNone;
    return self;
}
 
- (instancetype)initWithURLSessionDataTask:(NSURLSessionDataTask *)task responseObject:(id)responseObject {
    return [self initWithURLSessionDataTask:task responseObject:responseObject error:nil];
}
 
- (instancetype)initWithURLSessionDataTask:(NSURLSessionDataTask *)task error:(NSError *)error {
    return [self initWithURLSessionDataTask:task responseObject:nil error:error];
}
 
- (instancetype)initWithURLSessionDataTask:(NSURLSessionDataTask *)task responseObject:(id)responseObject error:(NSError *)error {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.status = MCURLResponseStatusNone;
    if (error) {
        self.error = error;
        if (error.code == -1001) {
            self.status = MCURLResponseStatusTimeout;
        }
    }
    
    if (!responseObject) {
        self.status = MCURLResponseStatusDataInvalid;
    }
    
    if (task) {
        self.task = task;
        self.URLIdentifier = task.originalRequest.URL.absoluteString;
    }
    
    self.responseObject = responseObject;
    
    return self;
}
@end