Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 3374bc5

Browse files
committed
Code refactor
1 parent 2844474 commit 3374bc5

File tree

4 files changed

+69
-46
lines changed

4 files changed

+69
-46
lines changed

ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
11+
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
1112
/* End PBXBuildFile section */
1213

1314
/* Begin PBXCopyFilesBuildPhase section */
@@ -112,6 +113,7 @@
112113
isa = PBXSourcesBuildPhase;
113114
buildActionMask = 2147483647;
114115
files = (
116+
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */,
115117
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
116118
);
117119
runOnlyForDeploymentPostprocessing = 0;

ios/RNFetchBlob/RNFetchBlob.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// RNFetchBlob.h
33
//
4-
// Created by Ben Hsieh on 2016/4/28.
4+
// Created by suzuri04x2 on 2016/4/28.
55
// Copyright © 2016年 Facebook. All rights reserved.
66
//
77

@@ -10,7 +10,14 @@
1010

1111
#import "RCTBridgeModule.h"
1212

13-
@interface RNFetchBlob : NSObject <RCTBridgeModule>
13+
@interface RNFetchBlob : NSObject <RCTBridgeModule>
14+
15+
@end
16+
17+
@interface FetchBlobUtils : NSObject
18+
19+
+ (void) onBlobResponse;
20+
+ (NSMutableDictionary *) normalizeHeaders;
1421

1522
@end
1623

ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// RNFetchBlob.m
33
//
4-
// Created by Ben Hsieh on 2016/4/28.
4+
// Created by suzuri04x2 on 2016/4/28.
55
// Copyright © 2016年 Facebook. All rights reserved.
66
//
77

@@ -10,25 +10,68 @@
1010
#import "RCTLog.h"
1111
#import <Foundation/Foundation.h>
1212

13-
// CalendarManager.m
13+
14+
////////////////////////////////////////
15+
//
16+
// Util functions
17+
//
18+
////////////////////////////////////////
19+
20+
@implementation FetchBlobUtils
21+
22+
// callback class method to handle request
23+
+ (void) onBlobResponse:(NSURLResponse * _Nullable)response withData:(NSData * _Nullable)data withError:(NSError * _Nullable)connectionError withCallback:(RCTResponseSenderBlock)callback{
24+
25+
NSHTTPURLResponse* resp = (NSHTTPURLResponse *) response;
26+
NSString* status = [NSString stringWithFormat:@"%d", resp.statusCode];
27+
28+
if(connectionError)
29+
{
30+
callback(@[[connectionError localizedDescription], [NSNull null]]);
31+
}
32+
else if(![status isEqualToString:@"200"]) {
33+
callback(@[status, [NSNull null]]);
34+
}
35+
else {
36+
callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
37+
}
38+
39+
}
40+
41+
// removing case of headers
42+
+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
43+
44+
NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
45+
for(NSString * key in headers) {
46+
[mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
47+
}
48+
49+
return mheaders;
50+
}
51+
52+
@end
53+
54+
55+
////////////////////////////////////////
56+
//
57+
// Exported native methods
58+
//
59+
////////////////////////////////////////
60+
1461
@implementation RNFetchBlob
1562

1663
RCT_EXPORT_MODULE();
1764

1865
// Fetch blob data request
1966
RCT_EXPORT_METHOD(fetchBlobForm:(NSString *)method url:(NSString *)url headers:(NSDictionary *)headers form:(NSArray *)form callback:(RCTResponseSenderBlock)callback)
2067
{
68+
2169
// send request
2270
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
2371
initWithURL:[NSURL
2472
URLWithString: url]];
73+
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ FetchBlobUtils normalizeHeaders:headers]];
2574

26-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] init];
27-
28-
// make headers case insensitive
29-
for(NSString * key in headers) {
30-
[mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
31-
}
3275

3376
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
3477
NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
@@ -40,7 +83,7 @@ @implementation RNFetchBlob
4083
if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
4184
NSMutableData * postData = [[NSMutableData alloc] init];
4285

43-
// combine body
86+
// combine multipart/form-data body
4487
for(id field in form) {
4588
NSString * name = [field valueForKey:@"name"];
4689
NSString * content = [field valueForKey:@"data"];
@@ -81,19 +124,7 @@ @implementation RNFetchBlob
81124
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
82125
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
83126

84-
NSHTTPURLResponse* resp = (NSHTTPURLResponse *) response;
85-
NSString* status = [NSString stringWithFormat:@"%d", resp.statusCode];
86-
87-
if(connectionError)
88-
{
89-
callback(@[[connectionError localizedDescription], [NSNull null]]);
90-
}
91-
else if(![status isEqualToString:@"200"]) {
92-
callback(@[status, [NSNull null]]);
93-
}
94-
else {
95-
callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
96-
}
127+
[FetchBlobUtils onBlobResponse:response withData:data withError: connectionError withCallback: callback];
97128

98129
}];
99130

@@ -107,16 +138,12 @@ @implementation RNFetchBlob
107138
initWithURL:[NSURL
108139
URLWithString: url]];
109140

110-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] init];
111-
112-
// make headers case insensitive
113-
for(NSString * key in headers) {
114-
[mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
115-
}
141+
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[FetchBlobUtils normalizeHeaders:headers]];
116142

117143
// if method is POST or PUT, convert data string format
118144
if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
119145

146+
// generate octet-stream body
120147
NSData* blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
121148
NSMutableData* postBody = [[NSMutableData alloc] init];
122149
[postBody appendData:[NSData dataWithData:blobData]];
@@ -132,23 +159,10 @@ @implementation RNFetchBlob
132159
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
133160
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
134161

135-
NSHTTPURLResponse* resp = (NSHTTPURLResponse *) response;
136-
NSString* status = [NSString stringWithFormat:@"%d", resp.statusCode];
137-
138-
if(connectionError)
139-
{
140-
callback(@[[connectionError localizedDescription], [NSNull null]]);
141-
}
142-
else if(![status isEqualToString:@"200"]) {
143-
callback(@[status, [NSNull null]]);
144-
}
145-
else {
146-
callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
147-
}
162+
[FetchBlobUtils onBlobResponse:response withData:data withError: connectionError withCallback: callback];
148163

149164
}];
150165

151166
}
167+
@end
152168

153-
154-
@end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fetch-blob",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "A react-native plugin for fetch blob data via HTTP.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)