Skip to content

Commit 54f658b

Browse files
authored
Merge pull request #428 from kazuki-ma/domain
Support api-data.line.me
2 parents 898d585 + dce9944 commit 54f658b

22 files changed

+769
-205
lines changed

line-bot-api-client/src/main/java/com/linecorp/bot/client/ChannelManagementClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@Setter
3333
@Accessors(fluent = true)
3434
public class ChannelManagementClientBuilder {
35-
private URI apiEndPoint = URI.create(LineClientConstants.DEFAULT_API_END_POINT);
35+
private URI apiEndPoint = LineClientConstants.DEFAULT_API_END_POINT;
3636
private ChannelTokenSupplier channelTokenSupplier;
3737

3838
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.client;
18+
19+
import java.util.concurrent.CompletableFuture;
20+
21+
import com.linecorp.bot.model.response.BotApiResponse;
22+
23+
/**
24+
* API client of blog (binary large object).
25+
*/
26+
public interface LineBlobClient {
27+
/**
28+
* Download image, video, and audio data sent from users.
29+
*
30+
* @see <a href="https://developers.line.me/en/reference/messaging-api/#get-content">//developers.line.me/en/reference/messaging-api/#get-content</a>
31+
*/
32+
CompletableFuture<MessageContentResponse> getMessageContent(String messageId);
33+
34+
/**
35+
* Download rich menu image.
36+
*
37+
* @see <a href="https://developers.line.me/en/docs/messaging-api/reference/#download-rich-menu-image">//developers.line.me/en/docs/messaging-api/reference/#download-rich-menu-image</a>
38+
*/
39+
CompletableFuture<MessageContentResponse> getRichMenuImage(String richMenuId);
40+
41+
/**
42+
* Set RichMenu image.
43+
*
44+
* @see <a href="https://developers.line.me/en/docs/messaging-api/reference/#upload-rich-menu-image">//developers.line.me/en/docs/messaging-api/reference/#upload-rich-menu-image</a>
45+
*/
46+
CompletableFuture<BotApiResponse> setRichMenuImage(
47+
String richMenuId, String contentType, byte[] content);
48+
49+
static LineBlobClientBuilder builder(String channelToken) {
50+
return builder(FixedChannelTokenSupplier.of(channelToken));
51+
}
52+
53+
static LineBlobClientBuilder builder(ChannelTokenSupplier channelTokenSupplier) {
54+
return new LineBlobClientBuilder().channelTokenSupplier(channelTokenSupplier);
55+
}
56+
}
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
* Copyright 2019 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.client;
18+
19+
import static java.util.Objects.requireNonNull;
20+
21+
import java.net.URI;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.concurrent.TimeUnit;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
31+
import com.linecorp.bot.model.objectmapper.ModelObjectMapper;
32+
33+
import lombok.NonNull;
34+
import lombok.Setter;
35+
import lombok.ToString;
36+
import lombok.experimental.Accessors;
37+
import lombok.experimental.PackagePrivate;
38+
import okhttp3.Interceptor;
39+
import okhttp3.OkHttpClient;
40+
import okhttp3.logging.HttpLoggingInterceptor;
41+
import okhttp3.logging.HttpLoggingInterceptor.Level;
42+
import retrofit2.Retrofit;
43+
import retrofit2.converter.jackson.JacksonConverterFactory;
44+
45+
@ToString
46+
@Accessors(fluent = true)
47+
public class LineBlobClientBuilder {
48+
private static final ObjectMapper objectMapper = ModelObjectMapper.createNewObjectMapper();
49+
50+
/**
51+
* Use {@link LineBlobClient#builder} to create instance.
52+
*/
53+
@PackagePrivate
54+
LineBlobClientBuilder() {
55+
}
56+
57+
/**
58+
* API Endpoint.
59+
*
60+
* <p>Default value = "https://api.line.me/".
61+
*/
62+
private URI apiEndPoint = LineClientConstants.DEFAULT_API_END_POINT;
63+
64+
/**
65+
* API Endpoint.
66+
*
67+
* @deprecated use {@link #apiEndPoint(URI)}.
68+
*/
69+
@Deprecated
70+
public LineBlobClientBuilder apiEndPoint(String apiEndPoint) {
71+
return apiEndPoint(URI.create(apiEndPoint));
72+
}
73+
74+
/**
75+
* API Endpoint.
76+
*
77+
* <p>Default value = "https://api.line.me/".
78+
*/ // We can remove this after delete `setApiEndPoint(String apiEndPoint)`.
79+
public LineBlobClientBuilder apiEndPoint(URI apiEndPoint) {
80+
this.apiEndPoint = requireNonNull(apiEndPoint, "apiEndPoint");
81+
return this;
82+
}
83+
84+
/**
85+
* Connection timeout.
86+
*
87+
* <p>Default value = {@value LineClientConstants#DEFAULT_CONNECT_TIMEOUT_MILLIS}ms.
88+
*/
89+
@Setter
90+
private long connectTimeout = LineClientConstants.DEFAULT_CONNECT_TIMEOUT_MILLIS;
91+
92+
/**
93+
* Connection timeout.
94+
*
95+
* <p>Default value = {@value LineClientConstants#DEFAULT_READ_TIMEOUT_MILLIS}ms.
96+
*/
97+
@Setter
98+
private long readTimeout = LineClientConstants.DEFAULT_READ_TIMEOUT_MILLIS;
99+
100+
/**
101+
* Write timeout.
102+
*
103+
* <p>Default value = {@value LineClientConstants#DEFAULT_WRITE_TIMEOUT_MILLIS}ms.
104+
*/
105+
@Setter
106+
private long writeTimeout = LineClientConstants.DEFAULT_WRITE_TIMEOUT_MILLIS;
107+
108+
/**
109+
* Channel token supplier of this client.
110+
*
111+
* <p>MUST BE NULL except you configured your own
112+
*/
113+
@Setter
114+
private ChannelTokenSupplier channelTokenSupplier;
115+
116+
/**
117+
* Custom {@link Retrofit.Builder} used internally.
118+
*
119+
* <p>If you want to use your own setting, specify {@link Retrofit.Builder} instance.
120+
* Default builder is used in case of {@code null} (default).
121+
*
122+
* <p>To use this method, please add dependency to 'com.squareup.retrofit2:retrofit'.
123+
*
124+
* @see #createDefaultRetrofitBuilder()
125+
*/
126+
@Setter
127+
private Retrofit.Builder retrofitBuilder;
128+
129+
/**
130+
* Add authentication header.
131+
*
132+
* <p>Default = {@value}. If you manage authentication header yourself, set to {@code false}.
133+
*/
134+
@Setter
135+
private boolean addAuthenticationHeader = true;
136+
137+
private OkHttpClient.Builder okHttpClientBuilder;
138+
139+
/**
140+
* Custom interceptors.
141+
*
142+
* <p>You can add your own interceptors.
143+
*
144+
* <p>Note: Authentication interceptor is automatically added by default.
145+
*
146+
* @see #addAuthenticationHeader(boolean)
147+
*/
148+
@Setter
149+
private List<Interceptor> additionalInterceptors = new ArrayList<>();
150+
151+
/**
152+
* Set fixed channel token. This overwrites {@link #channelTokenSupplier(ChannelTokenSupplier)}.
153+
*
154+
* @see #channelTokenSupplier(ChannelTokenSupplier)
155+
*/
156+
public LineBlobClientBuilder channelToken(String channelToken) {
157+
channelTokenSupplier(FixedChannelTokenSupplier.of(channelToken));
158+
return this;
159+
}
160+
161+
/**
162+
* Set customized OkHttpClient.Builder.
163+
*
164+
* <p>In case of you need your own customized {@link OkHttpClient},
165+
* this builder allows specify {@link OkHttpClient.Builder} instance.
166+
*
167+
* <p>To use this method, please add dependency to 'com.squareup.retrofit2:retrofit'.
168+
*
169+
* @param addAuthenticationHeader If true, all default okhttp interceptors ignored.
170+
* You should insert authentication headers yourself.
171+
*/
172+
public LineBlobClientBuilder okHttpClientBuilder(
173+
final @NonNull OkHttpClient.Builder okHttpClientBuilder,
174+
final boolean addAuthenticationHeader) {
175+
this.okHttpClientBuilder = okHttpClientBuilder;
176+
this.addAuthenticationHeader = addAuthenticationHeader;
177+
178+
return this;
179+
}
180+
181+
/**
182+
* Creates a new {@link LineBlobService}.
183+
*/
184+
<T> T buildRetrofitIface(URI apiEndPoint, Class<T> retrofitIFace) {
185+
if (okHttpClientBuilder == null) {
186+
okHttpClientBuilder = new OkHttpClient.Builder();
187+
}
188+
189+
// Add interceptors.
190+
if (addAuthenticationHeader) {
191+
okHttpClientBuilder.addInterceptor(buildAuthenticationInterceptor(channelTokenSupplier));
192+
}
193+
if (additionalInterceptors != null) {
194+
additionalInterceptors.forEach(okHttpClientBuilder::addInterceptor);
195+
}
196+
okHttpClientBuilder.addInterceptor(buildLoggingInterceptor());
197+
198+
// Set timeout.
199+
okHttpClientBuilder
200+
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
201+
.readTimeout(readTimeout, TimeUnit.MILLISECONDS)
202+
.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
203+
204+
final OkHttpClient okHttpClient = okHttpClientBuilder.build();
205+
206+
if (retrofitBuilder == null) {
207+
retrofitBuilder = createDefaultRetrofitBuilder();
208+
}
209+
retrofitBuilder.client(okHttpClient);
210+
retrofitBuilder.baseUrl(apiEndPoint.toString());
211+
212+
final Retrofit retrofit = retrofitBuilder.build();
213+
214+
return retrofit.create(retrofitIFace);
215+
}
216+
217+
static HeaderInterceptor buildAuthenticationInterceptor(ChannelTokenSupplier channelTokenSupplier) {
218+
requireNonNull(channelTokenSupplier, "channelTokenSupplier");
219+
return HeaderInterceptor.forChannelTokenSupplier(channelTokenSupplier);
220+
}
221+
222+
static Interceptor buildLoggingInterceptor() {
223+
final Logger slf4jLogger = LoggerFactory.getLogger("com.linecorp.bot.client.wire");
224+
225+
return new HttpLoggingInterceptor(slf4jLogger::info)
226+
.setLevel(Level.BODY);
227+
}
228+
229+
static Retrofit.Builder createDefaultRetrofitBuilder() {
230+
return new Retrofit.Builder()
231+
.addConverterFactory(JacksonConverterFactory.create(objectMapper));
232+
}
233+
234+
/**
235+
* Creates a new {@link LineBlobService}.
236+
*/
237+
public LineBlobClient build() {
238+
return new LineBlobClientImpl(
239+
buildRetrofitIface(apiEndPoint, LineBlobService.class));
240+
}
241+
242+
/**
243+
* Creates a new {@link LineBlobService}.
244+
*/
245+
public LineBlobClient buildBlobClient() {
246+
return new LineBlobClientImpl(buildRetrofitIface(
247+
apiEndPoint,
248+
LineBlobService.class));
249+
}
250+
}

0 commit comments

Comments
 (0)