-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.rb
243 lines (208 loc) · 7.24 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require 'httpclient'
require 'json'
require 'stringio'
module Plugin::Worldon
class APIResult
attr_reader :value
attr_reader :header
def initialize(value, header = nil)
@value = value
@header = header
end
def [](idx)
@value[idx]
end
def []=(idx, val)
@value[idx] = val
end
def to_h
@value.to_h
end
def to_a
@value.to_a
end
end
class API
class << self
# httpclient向けにパラメータHashを変換する
def build_query(params, headers)
# Hashで渡されるクエリパラメータをArray化する。
# valueがArrayだった場合はkeyに[]を付加して平たくする。
conv = []
params.each do |key, val|
if val.is_a? Array
val.each do |v|
elem_key = "#{key.to_s}[]"
conv << [elem_key, v]
end
else
conv << [key.to_s, val]
end
end
params = conv
# valueの種類に応じてhttpclientに渡すものを変える
files = []
to_multipart = params.any? {|key, value| value.is_a?(Pathname) || value.is_a?(Plugin::Photo::Photo) }
if to_multipart
headers << ["Content-Type", "multipart/form-data"]
end
params = params.map do |key, value|
case value
when Pathname
# multipart/form-data にするが、POSTリクエストではない可能性がある(PATCH等)ため、ある程度自力でつくる。
# boundary作成や実際のbody構築はhttpclientに任せる。
filename = value.basename.to_s
disposition = "form-data; name=\"#{key}\"; filename=\"#{filename}\""
f = File.open(value.to_s, 'rb')
files << f
{
"Content-Type" => "application/octet-stream",
"Content-Disposition" => disposition,
:content => f,
}
when Plugin::Photo::Photo
filename = Pathname(value.perma_link.path).basename.to_s
disposition = "form-data; name=\"#{key}\"; filename=\"#{filename}\""
{
"Content-Type" => "application/octet-stream",
"Content-Disposition" => "form-data; name=\"#{key}\"; filename=\"#{filename}\"",
:content => StringIO.new(value.blob, 'r'),
}
else
if to_multipart
{
"Content-Type" => "application/octet-stream",
"Content-Disposition" => "form-data; name=\"#{key}\"",
:content => value,
}
else
[key, value]
end
end
end
[params, headers, files]
end
# APIアクセスを行うhttpclientのラッパメソッド
def call(method, domain, path = nil, access_token = nil, opts = {}, headers = [], **params)
begin
if domain.is_a? Diva::URI
uri = domain
domain = uri.host
path = uri.path
else
url = 'https://' + domain + path
uri = Diva::URI.new(url)
end
if access_token && !access_token.empty?
headers += [["Authorization", "Bearer " + access_token]]
end
begin
query, headers, files = build_query(params, headers)
body = nil
if method != :get # :post, :patch
body = query
query = nil
end
notice "Worldon::API.call #{method.to_s} #{uri} #{headers.to_s} #{query.to_s} #{body.to_s}"
client = HTTPClient.new
resp = client.request(method, uri.to_s, query, body, headers)
ensure
files.each do |f|
f.close
end
end
case resp.status
when 200
hash = JSON.parse(resp.content, symbolize_names: true)
parse_Link(resp, hash)
else
warn "API.call did'nt return 200 Success"
pp [uri.to_s, query, body, headers, resp] if Mopt.error_level >= 2
$stdout.flush
nil
end
rescue => e
error "API.call raise exception"
pp e if Mopt.error_level >= 1
$stdout.flush
nil
end
end
def parse_Link(resp, hash)
link = resp.header['Link'].first
return APIResult.new(hash) if ((!hash.is_a? Array) || link.nil?)
header =
link
.split(', ')
.map do |line|
/^<(.*)>; rel="(.*)"$/.match(line) do |m|
[$2.to_sym, Diva::URI.new($1)]
end
end
.to_h
APIResult.new(hash, header)
end
def status(domain, id)
call(:get, domain, '/api/v1/statuses/' + id.to_s)
end
def status_by_url(domain, access_token, url)
resp = call(:get, domain, '/api/v1/search', access_token, q: url.to_s, resolve: true)
return nil if resp.nil?
resp[:statuses]
end
def account_by_url(domain, access_token, url)
resp = call(:get, domain, '/api/v1/search', access_token, q: url.to_s, resolve: true)
return nil if resp.nil?
resp[:accounts]
end
def get_local_status_id(world, status)
return status.id if world.domain == status.domain
# 別インスタンス起源のstatusなので検索する
statuses = status_by_url(world.domain, world.access_token, status.url)
if statuses.nil? || statuses[0].nil? || statuses[0][:id].nil?
nil
else
statuses[0][:id]
end
end
def get_local_account_id(world, account)
return account.id if world.domain == account.domain
# 別インスタンス起源のaccountなので検索する
accounts = account_by_url(world.domain, world.access_token, account.url)
if accounts.nil? || accounts[0].nil? || accounts[0][:id].nil?
nil
else
accounts[0][:id]
end
end
# Link headerがあるAPIを連続的に叩いて1要素ずつyieldする
# ==== Args
# [method] HTTPメソッド
# [domain] 対象ドメイン
# [path] APIパス
# [access_token] トークン
# [opts] オプション
# [:direction] :next or :prev
# [:wait] APIコール間にsleepで待機する秒数
# [headers] 追加ヘッダ
# [params] GET/POSTパラメータ
def all(method, domain, path = nil, access_token = nil, opts = {}, headers = [], **params)
opts[:direction] ||= :next
opts[:wait] ||= 1
while true
list = API.call(method, domain, path, access_token, opts, headers, **params)
if list && list.value.is_a?(Array)
list.value.each { |hash| yield hash }
end
break unless list.header.has_key?(opts[:direction])
url = list.header[opts[:direction]]
params = URI.decode_www_form(url.query).to_h.symbolize
sleep opts[:wait]
end
end
def all_with_world(world, method, path = nil, opts = {}, headers = [], **params, &block)
all(method, world.domain, path, world.access_token, opts, headers, **params, &block)
end
end
end
end