17
17
package com .ctrip .framework .apollo .openapi .client .service ;
18
18
19
19
import com .ctrip .framework .apollo .openapi .client .exception .ApolloOpenApiException ;
20
+ import com .ctrip .framework .apollo .openapi .client .url .OpenApiPathBuilder ;
20
21
import com .google .common .base .Preconditions ;
21
22
import com .google .common .base .Strings ;
22
- import com .google .common .escape .Escaper ;
23
- import com .google .common .net .UrlEscapers ;
24
23
import com .google .gson .Gson ;
25
- import java .io .IOException ;
26
24
import org .apache .http .HttpResponse ;
27
25
import org .apache .http .StatusLine ;
28
- import org .apache .http .client .methods .CloseableHttpResponse ;
29
- import org .apache .http .client .methods .HttpDelete ;
30
- import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
31
- import org .apache .http .client .methods .HttpGet ;
32
- import org .apache .http .client .methods .HttpPost ;
33
- import org .apache .http .client .methods .HttpPut ;
34
- import org .apache .http .client .methods .HttpUriRequest ;
26
+ import org .apache .http .client .methods .*;
35
27
import org .apache .http .entity .ContentType ;
36
28
import org .apache .http .entity .StringEntity ;
37
29
import org .apache .http .impl .client .CloseableHttpClient ;
38
30
import org .apache .http .util .EntityUtils ;
39
31
40
- abstract class AbstractOpenApiService {
41
- private static final Escaper pathEscaper = UrlEscapers .urlPathSegmentEscaper ();
42
- private static final Escaper queryParamEscaper = UrlEscapers .urlFormParameterEscaper ();
32
+ import java .io .IOException ;
43
33
34
+ abstract class AbstractOpenApiService {
44
35
private final String baseUrl ;
45
36
46
37
protected final CloseableHttpClient client ;
@@ -52,38 +43,30 @@ abstract class AbstractOpenApiService {
52
43
this .gson = gson ;
53
44
}
54
45
55
- protected CloseableHttpResponse get (String path ) throws IOException {
56
- HttpGet get = new HttpGet (String . format ( "%s/%s" , baseUrl , path ));
46
+ protected CloseableHttpResponse get (OpenApiPathBuilder path ) throws IOException {
47
+ HttpGet get = new HttpGet (path . buildPath ( baseUrl ));
57
48
58
49
return execute (get );
59
50
}
60
51
61
- protected CloseableHttpResponse post (String path , Object entity ) throws IOException {
62
- HttpPost post = new HttpPost (String . format ( "%s/%s" , baseUrl , path ));
52
+ protected CloseableHttpResponse post (OpenApiPathBuilder path , Object entity ) throws IOException {
53
+ HttpPost post = new HttpPost (path . buildPath ( baseUrl ));
63
54
64
55
return execute (post , entity );
65
56
}
66
57
67
- protected CloseableHttpResponse put (String path , Object entity ) throws IOException {
68
- HttpPut put = new HttpPut (String . format ( "%s/%s" , baseUrl , path ));
58
+ protected CloseableHttpResponse put (OpenApiPathBuilder path , Object entity ) throws IOException {
59
+ HttpPut put = new HttpPut (path . buildPath ( baseUrl ));
69
60
70
61
return execute (put , entity );
71
62
}
72
63
73
- protected CloseableHttpResponse delete (String path ) throws IOException {
74
- HttpDelete delete = new HttpDelete (String . format ( "%s/%s" , baseUrl , path ));
64
+ protected CloseableHttpResponse delete (OpenApiPathBuilder path ) throws IOException {
65
+ HttpDelete delete = new HttpDelete (path . buildPath ( baseUrl ));
75
66
76
67
return execute (delete );
77
68
}
78
69
79
- protected String escapePath (String path ) {
80
- return pathEscaper .escape (path );
81
- }
82
-
83
- protected String escapeParam (String param ) {
84
- return queryParamEscaper .escape (param );
85
- }
86
-
87
70
private CloseableHttpResponse execute (HttpEntityEnclosingRequestBase requestBase , Object entity ) throws IOException {
88
71
requestBase .setEntity (new StringEntity (gson .toJson (entity ), ContentType .APPLICATION_JSON ));
89
72
@@ -98,7 +81,6 @@ private CloseableHttpResponse execute(HttpUriRequest request) throws IOException
98
81
return response ;
99
82
}
100
83
101
-
102
84
private void checkHttpResponseStatus (HttpResponse response ) {
103
85
if (response .getStatusLine ().getStatusCode () == 200 ) {
104
86
return ;
0 commit comments