Skip to content

Commit 5f4b8c0

Browse files
authored
Merge pull request #189 from line/feature/retry_key
Support X-Line-Retry-Key
2 parents 507470b + 9546ab2 commit 5f4b8c0

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

lib/line/bot/client.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,16 @@ def revoke_channel_token(access_token)
106106
#
107107
# @param user_id [String] User Id
108108
# @param messages [Hash or Array] Message Objects
109+
# @param headers [Hash] HTTP Headers
109110
# @return [Net::HTTPResponse]
110-
def push_message(user_id, messages)
111+
def push_message(user_id, messages, headers: {})
111112
channel_token_required
112113

113114
messages = [messages] if messages.is_a?(Hash)
114115

115116
endpoint_path = '/bot/message/push'
116117
payload = { to: user_id, messages: messages }.to_json
117-
post(endpoint, endpoint_path, payload, credentials)
118+
post(endpoint, endpoint_path, payload, credentials.merge(headers))
118119
end
119120

120121
# Reply messages to a user using replyToken.
@@ -151,30 +152,33 @@ def reply_message(token, messages)
151152
#
152153
# @param to [Array or String] Array of userIds
153154
# @param messages [Hash or Array] Message Objects
155+
# @param headers [Hash] HTTP Headers
154156
# @return [Net::HTTPResponse]
155-
def multicast(to, messages)
157+
def multicast(to, messages, headers: {})
156158
channel_token_required
157159

158160
to = [to] if to.is_a?(String)
159161
messages = [messages] if messages.is_a?(Hash)
160162

161163
endpoint_path = '/bot/message/multicast'
162164
payload = { to: to, messages: messages }.to_json
163-
post(endpoint, endpoint_path, payload, credentials)
165+
post(endpoint, endpoint_path, payload, credentials.merge(headers))
164166
end
165167

166168
# Send messages to all friends.
167169
#
168170
# @param messages [Hash or Array] Message Objects
171+
# @param headers [Hash] HTTP Headers
172+
#
169173
# @return [Net::HTTPResponse]
170-
def broadcast(messages)
174+
def broadcast(messages, headers: {})
171175
channel_token_required
172176

173177
messages = [messages] if messages.is_a?(Hash)
174178

175179
endpoint_path = '/bot/message/broadcast'
176180
payload = { messages: messages }.to_json
177-
post(endpoint, endpoint_path, payload, credentials)
181+
post(endpoint, endpoint_path, payload, credentials.merge(headers))
178182
end
179183

180184
# Narrowcast messages to users
@@ -186,9 +190,10 @@ def broadcast(messages)
186190
# @param recipient [Hash]
187191
# @param filter [Hash]
188192
# @param limit [Hash]
193+
# @param headers [Hash] HTTP Headers
189194
#
190195
# @return [Net::HTTPResponse]
191-
def narrowcast(messages, recipient: nil, filter: nil, limit: nil)
196+
def narrowcast(messages, recipient: nil, filter: nil, limit: nil, headers: {})
192197
channel_token_required
193198

194199
messages = [messages] if messages.is_a?(Hash)
@@ -200,7 +205,7 @@ def narrowcast(messages, recipient: nil, filter: nil, limit: nil)
200205
filter: filter,
201206
limit: limit
202207
}.to_json
203-
post(endpoint, endpoint_path, payload, credentials)
208+
post(endpoint, endpoint_path, payload, credentials.merge(headers))
204209
end
205210

206211
def leave_group(group_id)

spec/line/bot/client_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,35 @@ def generate_client
126126
messages = body[:messages]
127127
expect(messages[0]).to eq message
128128
end
129+
130+
it 'set X-Line-Retry-Key' do
131+
client = Line::Bot::Client.new do |config|
132+
config.channel_token = dummy_config[:channel_token]
133+
end
134+
135+
available_endpoints = [
136+
'/bot/message/push',
137+
'/bot/message/multicast',
138+
'/bot/message/narrowcast',
139+
'/bot/message/broadcast'
140+
]
141+
142+
available_endpoints.each do |available_endpoint|
143+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + available_endpoint
144+
stub_request(:post, uri_template).with do |request|
145+
expect(request.headers['X-Line-Retry-Key']).to eq '123e4567-e89b-12d3-a456-426614174000'
146+
end.to_return(body: '{}', status: 200)
147+
end
148+
149+
user_id = "user1"
150+
message = {
151+
type: "text",
152+
text: "Hello, world"
153+
}
154+
155+
client.push_message(user_id, message, headers: {'X-Line-Retry-Key' => '123e4567-e89b-12d3-a456-426614174000'})
156+
client.multicast(user_id, message, headers: {'X-Line-Retry-Key' => '123e4567-e89b-12d3-a456-426614174000'})
157+
client.narrowcast(message, headers: {'X-Line-Retry-Key' => '123e4567-e89b-12d3-a456-426614174000'})
158+
client.broadcast(message, headers: {'X-Line-Retry-Key' => '123e4567-e89b-12d3-a456-426614174000'})
159+
end
129160
end

0 commit comments

Comments
 (0)