Skip to content

Commit

Permalink
Automatic determine whether use url perfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Arc-lin committed Jun 21, 2017
1 parent ed470e0 commit 6063124
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 79 deletions.
2 changes: 1 addition & 1 deletion ALNetworking.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "ALNetworking"
s.version = "1.0.5"
s.version = "1.1.0"
s.summary = "A networking framework , encapsulation of AFNetworking3.0,ReactiveCocoa2.5,YYCache,YYModel"

# This description is used to generate tags and improve search results.
Expand Down
5 changes: 1 addition & 4 deletions ALNetworking/ALNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@
/** Request method */
- (ALNetworking * (^)(ALNetworkRequestMethod metohd))method;

/** Request an url with prefix */
/** Request an url with prefix (If not include 'http://' or 'https://') */
- (ALNetworking * (^)(NSString *url))url;

/** Request an url without prefix */
- (ALNetworking * (^)(NSString *url))url_x;

/** Request Header */
- (ALNetworking * (^)(NSDictionary *header))header;

Expand Down
19 changes: 8 additions & 11 deletions ALNetworking/ALNetworking.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,14 @@ + (instancetype)allocWithZone:(struct _NSZone *)zone
if ([[url substringToIndex:1] isEqualToString:@"/"]) {
url = [url substringFromIndex:1];
}
self.request.urlStr = [NSString stringWithFormat:@"%@/%@",self.config.urlPerfix,url];
self.request.ignoreCustomResponseClass = NO;
return self;
};
}

- (ALNetworking *(^)(NSString *))url_x
{
return ^ALNetworking *(NSString *url){
self.request.urlStr = url;
self.request.ignoreCustomResponseClass = YES;
// 通过判断有没有http:// 或者 https:// 前缀去决定是否要使用url前缀 , 如果这种做法处理不当的话请告知我
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"]) {
self.request.urlStr = url;
self.request.ignoreCustomResponseClass = YES;
} else {
self.request.urlStr = [NSString stringWithFormat:@"%@/%@",self.config.urlPerfix,url];
self.request.ignoreCustomResponseClass = NO;
}
return self;
};
}
Expand Down
11 changes: 1 addition & 10 deletions ALNetworking/ALNetworkingConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@
#define paramsDic(...) params(NSDictionaryOfVariableBindings(__VA_ARGS__))

/** Build request expediently */
/** 带自定义url前缀 */
/** With url Prefix */

#define get(urlStr,...) url(urlStr).method(ALNetworkRequestMethodGET).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define post(urlStr,...) url(urlStr).method(ALNetworkRequestMethodGET).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define put(urlStr,...) url(urlStr).method(ALNetworkRequestMethodPUT).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define delete(urlStr,...) url(urlStr).method(ALNetworkRequestMethodDELETE).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define patch(urlStr,...) url(urlStr).method(ALNetworkRequestMethodPATCH).params(NSDictionaryOfVariableBindings(__VA_ARGS__))

/** 不带自定义url前缀 */
/** Without url Prefix */
#define get_x(urlStr,...) url_x(urlStr).method(ALNetworkRequestMethodGET).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define post_x(urlStr,...) url_x(urlStr).method(ALNetworkRequestMethodGET).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define put_x(urlStr,...) url_x(urlStr).method(ALNetworkRequestMethodPUT).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define delete_x(urlStr,...) url_x(urlStr).method(ALNetworkRequestMethodDELETE).params(NSDictionaryOfVariableBindings(__VA_ARGS__))
#define patch_x(urlStr,...) url_x(urlStr).method(ALNetworkRequestMethodPATCH).params(NSDictionaryOfVariableBindings(__VA_ARGS__))

/**
Requst metohd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworking.m"
timestampString = "519277287.683043"
timestampString = "519709800.16892"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "224"
endingLineNumber = "224"
startingLineNumber = "221"
endingLineNumber = "221"
landmarkName = "-handleRequest:"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -90,11 +90,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworking.m"
timestampString = "519277287.683043"
timestampString = "519709800.16892"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "220"
endingLineNumber = "220"
startingLineNumber = "217"
endingLineNumber = "217"
landmarkName = "-handleRequest:"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -105,13 +105,13 @@
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworking.m"
timestampString = "519277297.809152"
filePath = "ALNetworking/ALNetworkingConfig.m"
timestampString = "519277323.166102"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "73"
endingLineNumber = "73"
landmarkName = "-url"
startingLineNumber = "63"
endingLineNumber = "63"
landmarkName = "-urlPerfix"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand All @@ -122,11 +122,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworkingConfig.m"
timestampString = "519277323.166102"
timestampString = "519277324.941295"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "63"
endingLineNumber = "63"
startingLineNumber = "67"
endingLineNumber = "67"
landmarkName = "-urlPerfix"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -137,13 +137,29 @@
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworkingConfig.m"
timestampString = "519277324.941295"
filePath = "ALNetworking/ALNetworking.m"
timestampString = "519709800.16892"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "67"
endingLineNumber = "67"
landmarkName = "-urlPerfix"
startingLineNumber = "77"
endingLineNumber = "77"
landmarkName = "-url"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ALNetworking/ALNetworking.m"
timestampString = "519709800.16892"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "80"
endingLineNumber = "80"
landmarkName = "-url"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down
16 changes: 6 additions & 10 deletions ALNetworkingDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ - (void)viewDidLoad {

[SVProgressHUD setMaximumDismissTimeInterval:1.0f];

// 示例1 : 淘宝接口:不使用配置好的前缀.有请求参数 EXAMPLE 1 : NO URL PREFIX
// 示例1 : EXAMPLE 1
@weakify(self);
[[self.btn1 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
[self example1];
}];

// 示例2 : 使用配置好的前缀 EXAMPLE 2 : HAS URL PERFIX
// 示例2 : EXAMPLE 2
[[self.btn2 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
[self example2];
Expand Down Expand Up @@ -68,28 +68,24 @@ - (void)example1
3. Custom response class should be inherit from ALNetworkingResponse
*/


// Below Code Not Use url Perfix
// 下面的示例代码不使用url前缀

// 写法1
RACSignal *example1 = self.networking
.url_x(@"http://ip.taobao.com/service/getIpInfo.php")
.url(@"http://ip.taobao.com/service/getIpInfo.php")
.params(@{@"ip":@"192.168.0.103"})
.executeSignal();

// 写法2 使用 NSDictionaryOfVariableBindings() 将对象名作为键,对象的值作为值,组成字典
// Use NSDictionaryOfVariableBindings() object key as key, object value as value ,generate a dictionary
NSString *ip = @"192.168.0.103";
RACSignal *example1_1 = self.networking
.url_x(@"http://ip.taobao.com/service/getIpInfo.php")
.url(@"http://ip.taobao.com/service/getIpInfo.php")
.paramsDic(ip)
.executeSignal();

// 写法3 针对不同类型的请求,可以设定请求方式和请求体类型
// Setting different request method and different type of parameters
RACSignal *example1_2 = self.networking
.url_x(@"http://ip.taobao.com/service/getIpInfo.php")
.url(@"http://ip.taobao.com/service/getIpInfo.php")
.paramsDic(ip)
.paramsType(ALNetworkRequestParamsTypeJSON)
.method(ALNetworkRequestMethodGET)
Expand All @@ -100,7 +96,7 @@ - (void)example1
// Use marco to save your code! url(xxx).paramsDic(a,b).method(ALNetworkRequestMethodGET) ===> get(xxx,a,b)

RACSignal *example1_3 = self.networking
.get_x(@"http://ip.taobao.com/service/getIpInfo.php",ip)
.get(@"http://ip.taobao.com/service/getIpInfo.php",ip)
.paramsType(ALNetworkRequestParamsTypeJSON)
.executeSignal();

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

---

## [1.1.0](https://github.com/Arc-lin/ALNetworking/releases/tag/1.0.5) (06/16/2017)

#### Update
* Determine whether you want to use a custom URL prefix by whether the URL has a'http://'or'https://' prefix

## [1.0.5](https://github.com/Arc-lin/ALNetworking/releases/tag/1.0.5) (06/16/2017)

#### Update
Expand Down
34 changes: 10 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ALNetworking
## 这是什么

ALNetworking 是一个基于AFNetworking的框架. __只能基于ReactiveCocoa(2.5版本)环境下使用__.
只能在Objective-C语言下使用.


## 特性

Expand Down Expand Up @@ -74,11 +74,9 @@ ALNetworking 是基于AFNetworking 3.x 的. 更多详情看[AFNetworking README]

__这里有一个[简单的MVVM+RAC使用示例](https://github.com/Arc-lin/ALMVVMDemo)__

### url() & url_x()

`url()` : 请求的时候会在该内容前面加上一个自定义的前缀拼成Url
### url()

`url_x()` : 请求的时候直接用该内容作为url
`url()` : 请求的时候会在该内容前面加上一个自定义的前缀拼成Url,如果有`https://` 或者 `http://`前缀的话,就不会拼上自定义的前缀,如果这种处理方式不当的话请告知我

### params() & paramsDic()

Expand All @@ -90,19 +88,14 @@ __这里有一个[简单的MVVM+RAC使用示例](https://github.com/Arc-lin/ALMV

同样道理

带url前缀

`url(xxx).paramsDic(a,b).method(ALNetworkRequestMethodPOST)` 也可以写成 `post(xxx,a,b)`

不带url前缀

`url_x(xxx).paramsDic(a,b).method(ALNetworkRequestMethodPOST)` 也可以写成 `post_x(xxx,a,b)`

and so on.
等等

__一些配置信息写在了AppDelegate,请下载Demo查看__

### example1 (不使用自定义的前缀)
### example1

```
// example1
Expand Down Expand Up @@ -141,7 +134,7 @@ __一些配置信息写在了AppDelegate,请下载Demo查看__
```

### 示例2 (使用自定义的前缀,然后我们来试试使用缓存策略)
### 示例2 (试试使用缓存策略)
```
NSString *Id = @"1";
NSString *name = @"22";
Expand Down Expand Up @@ -233,11 +226,9 @@ ALNetworking is based on AFNetworking. You can find more detail about version co

__Here has a simple example for [MVVM+RAC](https://github.com/Arc-lin/ALMVVMDemo)__

### url() & url_x()

`url()` : Combine perfix with the content as url
### url()

`url_x()` : Only use conent as url
`url()` : Combine perfix with the content as url, if you have a prefix of `https://` or `http://`, you will not have custom prefix attached. If this is not handled properly, please let me know

### params() & paramsDic()

Expand All @@ -249,21 +240,16 @@ __Here has a simple example for [MVVM+RAC](https://github.com/Arc-lin/ALMVVMDemo

On the same

With url prefix

`url(xxx).paramsDic(a,b).method(ALNetworkRequestMethodPOST)` can be written as `post(xxx,a,b)`

Without url prefix

`url_x(xxx).paramsDic(a,b).method(ALNetworkRequestMethodPOST)` can be written as `post_x(xxx,a,b)`

and so on.

__Some configure message is write on AppDelegate,please download the Demo to check__

__There are more messages in the demo , please download the demo and read it__

### example1 (Without url prefix)
### example1

```
// example1
Expand Down Expand Up @@ -302,7 +288,7 @@ __There are more messages in the demo , please download the demo and read it__
```

### example2 (With url prefix, try to use cache stategy)
### example2 (Try to use cache stategy)
```
NSString *Id = @"1";
NSString *name = @"22";
Expand Down

0 comments on commit 6063124

Please sign in to comment.