This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.rb
699 lines (621 loc) · 20.5 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
require 'bundler/setup'
%w(yaml json digest redis csv).each { |req| require req }
Bundler.require(:default)
require 'sinatra'
require "sinatra/multi_route"
require 'mongo'
require_relative 'funs'
require_relative 'models/models'
# feature flag: toggle redis
$use_redis = true
# mongo
mongo_host = [ ENV.fetch('MONGO_PORT_27017_TCP_ADDR') + ":" + ENV.fetch('MONGO_PORT_27017_TCP_PORT') ]
client_options = {
:database => 'bienusers',
:user => ENV.fetch('BIEN_MONGO_USER'),
:password => ENV.fetch('BIEN_MONGO_PWD'),
:max_pool_size => 25,
:connect_timeout => 15,
:wait_queue_timeout => 15
}
mongo = Mongo::Client.new(mongo_host, client_options)
# mongo = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'bienusers')
$busers = mongo[:users]
# api key
$api_key = ENV['BIEN_API_KEY']
$config = YAML::load_file(File.join(__dir__, ENV['RACK_ENV'] == 'test' ? 'test_config.yaml' : 'config.yaml'))
$config = YAML::load_file(File.join(__dir__, 'config.yaml'))
$redis = Redis.new host: ENV.fetch('REDIS_PORT_6379_TCP_ADDR', 'localhost'),
port: ENV.fetch('REDIS_PORT_6379_TCP_PORT', 6379)
ActiveSupport::Deprecation.silenced = true
ActiveRecord::Base.establish_connection($config['db'])
ActiveRecord::Base.logger = Logger.new(STDOUT)
class API < Sinatra::Application
configure do
enable :logging
set :dump_errors, false
set :raise_errors, false
set :show_exceptions, false
set :server, :puma
set :protection, :except => [:json_csrf]
end
# handle missed route
not_found do
halt 404, { error: 'route not found' }.to_json
end
# handle other errors
error do
halt 500, { error: 'server error' }.to_json
end
# method not allowed
error 405 do
halt 405, {'Content-Type' => 'application/json'}, JSON.generate({ 'error' => 'Method Not Allowed' })
end
# unsupported media type
error 415 do
halt 415, { error: 'Unsupported media type', message: 'supported media types are application/json, text/csv' }.to_json
end
$paths_to_ignore = ["/", "/heartbeat", "/heartbeat/", "/token", "/token/"]
before do
# puts '[env]'
# p env
# puts '[Params]'
# p params
# puts '[Authorization]'
# p request.env['HTTP_AUTHORIZATION'].slice(7..-1)
$route = request.path
# set headers
headers 'Content-Type' => 'application/json; charset=utf8'
headers 'Access-Control-Allow-Methods' => 'HEAD, GET'
headers 'Access-Control-Allow-Origin' => '*'
headers 'Strict-Transport-Security' => 'max-age=86400; includeSubDomains'
cache_control :public, :must_revalidate, max_age: 60
# use redis caching
acc = request.env['HTTP_ACCEPT']
acc = acc == "*/*" ? "application/json" : acc
if $config['caching'] && $use_redis && authorized?
if !$paths_to_ignore.include? request.path_info
@cache_key = Digest::MD5.hexdigest([request.url, acc].join('-'))
if $redis.exists(@cache_key)
headers 'Cache-Hit' => 'true'
halt 200, $redis.get(@cache_key)
end
end
end
def accept_ok?
accept = request.env['HTTP_ACCEPT']
['application/json', 'text/csv'].include? accept
end
415 unless accept_ok?
pass if %w[/ /heartbeat /heartbeat/].include? request.path_info
# halt 401, { error: 'not authorized' }.to_json unless !request.env['HTTP_AUTHORIZATION'].nil?
# httpauth = request.env['HTTP_AUTHORIZATION'] || ""
# halt 401, { error: 'not authorized' }.to_json unless valid_key?(httpauth.slice(7..-1))
end
after do
# cache response in redis
if $config['caching'] &&
$use_redis &&
!response.headers['Cache-Hit'] &&
response.status == 200 &&
request.path_info != "/" &&
request.path_info != ""
$redis.set(@cache_key, response.body[0], ex: $config['caching']['expires'])
end
end
helpers do
def valid_key?(key)
key == $api_key
end
def serve_data(ha, data)
# puts '[HTTP_ACCEPT]'
# puts request.env['HTTP_ACCEPT'].nil?
case request.env['HTTP_ACCEPT']
when 'application/json'
ha.to_json
when 'text/csv'
to_csv(data)
when '*/*'
ha.to_json
when nil
ha.to_json
else
415
end
end
# if method not allowed, halt with error
def halt_method(x = ['GET'])
if !x.include?(request.request_method)
halt 405
end
end
end
configure do
mime_type :apidocs, 'text/html'
mime_type :csv, 'text/csv'
end
# handler - redirects any /foo -> /foo/
# - if has any query params, passes to handler as before
# get %r{(/.*[^\/])$} do
# if request.query_string == "" or request.query_string.nil?
# redirect request.script_name + "#{params[:captures].first}/"
# else
# pass
# end
# end
def valid_email?(email)
raise Exception.new("an email must be given") unless not email.nil?
res = EmailAddress.error email
if not res.nil?
if res.empty?
res = "email string given doesn't appear to be an email address"
end
halt 403, { error: { message: res }}.to_json
end
end
def token_make(email)
valid_email? email
tg = token_get(email)
if tg.nil?
# email not found, create token
tok = SecureRandom.urlsafe_base64.gsub(/[^0-9a-zA-Z]/i, '')
# on successful token creation, store in database
$busers.insert_one({ email: email, token: tok })
x = $busers.find({ email: params[:email] }).first
x.delete("_id")
return x
else
# email found, give back token
return tg
end
end
def token_get(email)
valid_email? email
res = $busers.find({ email: params[:email] }).first
return res unless not res.nil?
res.delete("_id")
return res
end
get '/token/?' do
begin
tok = token_make(params[:email])
content_type :json
tok.to_json
rescue Exception => e
halt 400, { error: { message: e.message }}.to_json
end
end
def token_valid?(x)
if $busers.find({ token: x }).count != 1
raise Exception.new("token not found; get a token first with the /token route")
end
end
def authorized?
return true if $paths_to_ignore.include? request.path_info
tok = env.fetch('HTTP_AUTHORIZATION', nil)
if tok.nil?
content_type :json
halt 401, { error: 'A token must be given in the Authorization header' }.to_json
end
begin
token_valid? tok
rescue Exception => e
content_type :json
halt 403, { error: e.message }.to_json
end
return true
end
get '/authorized/?' do
authorized?
content_type :json
{ authorized: true }.to_json
end
# default to landing page
## used to go to /heartbeat
get '/?' do
halt_method
content_type :apidocs
send_file File.join(settings.public_folder, '/index.html')
end
# route listing route
get '/heartbeat/?' do
{ routes: API.routes["GET"].map{ |w| w[0].to_s } }.to_json
end
# generate routes from the models
# Models.models.each do |model_name|
# model = Models.const_get(model_name)
# get "/#{model_name.to_s.downcase}/?#{model.primary_key ? ':id?/?' : '' }" do
# begin
# data = model.endpoint(params)
# raise Exception.new('no results found') if data.length.zero?
# ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
# serve_data(ha, data)
# rescue Exception => e
# halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
# end
# end
# end
get '/list/?' do
authorized?
halt_method
begin
data = List.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/list/country/?' do
authorized?
halt_method
begin
data = ListCountry.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# plot routes
get '/plot/metadata/?' do
authorized?
halt_method
begin
data = PlotMetadata.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## List available sampling protocols
get '/plot/protocols/?' do
authorized?
halt_method
begin
data = PlotProtocols.endpoint
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## Get a sampling protocol by name
# get '/plot/protocols/:protocol/?' do
# authorized?
# halt_method
# begin
# data = PlotSamplingProtocol.endpoint(params)
# raise Exception.new('no results found') if data.length.zero?
# ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
# serve_data(ha, data)
# rescue Exception => e
# halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
# end
# end
## Plot data by plot name
# get '/plot/name/?' do
# halt_method
# begin
# data = PlotName.endpoint(params)
# raise Exception.new('no results found') if data.length.zero?
# ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
# serve_data(ha, data)
# rescue Exception => e
# halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
# end
# end
# trait routes
## all traits
get '/traits/?' do
authorized?
halt_method
begin
data = Traits.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## traits by family
get '/traits/family/?' do
authorized?
halt_method
begin
data = TraitsFamily.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## trait id by family
get '/traits/family/:id/?' do
# call env.merge("PATH_INFO" => '/traits/family/')
authorized?
halt_method
begin
data = TraitsFamilyId.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# occurrence routes
## species
get '/occurrence/species/?' do
authorized?
halt_method
begin
data = OccurrenceSpecies.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: nil, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## genus
get '/occurrence/genus/?' do
authorized?
halt_method
begin
data = OccurrenceGenus.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: nil, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## family
get '/occurrence/family/?' do
authorized?
halt_method
begin
data = OccurrenceFamily.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: nil, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
## family
post '/occurrence/spatial/?' do
authorized?
halt_method(['POST'])
begin
data = OccurrenceSpatial.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil ).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# count
get '/occurrence/count/?' do
authorized?
halt_method
begin
data = OccurrenceCount.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# taxonomy routes
get '/taxonomy/species/?' do
authorized?
begin
halt_method
data = TaxonomySpecies.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).size, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.backtrace }}.to_json
end
end
get '/taxonomy/genus/?' do
authorized?
begin
halt_method
data = TaxonomyGenus.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).size, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.backtrace }}.to_json
end
end
get '/taxonomy/family/?' do
authorized?
begin
halt_method
data = TaxonomyFamily.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).size, returned: data.size, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.backtrace }}.to_json
end
end
# phylogeny route
get '/phylogeny/?' do
authorized?
begin
halt_method
data = Phylogeny.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# meta routes
get '/meta/version/?' do
authorized?
begin
halt_method
data = MetaVersion.endpoint
raise Exception.new('no results found') if data.length.zero?
ha = { data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { data: nil, error: { message: e.message }}.to_json
end
end
get '/meta/citations/traits/:id/?' do
authorized?
begin
halt_method
data = CitationsTrait.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { data: nil, error: { message: e.message }}.to_json
end
end
get '/meta/citations/occurrence/:id/?' do
authorized?
begin
halt_method
data = CitationsOccurrence.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { data: nil, error: { message: e.message }}.to_json
end
end
get '/meta/politicalnames/?' do
authorized?
begin
halt_method
data = MetaPoliticalNames.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# ranges routes
get '/ranges/list/?' do
authorized?
begin
halt_method
data = RangesList.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/ranges/species/?' do
authorized?
begin
halt_method
data = RangesSpecies.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/ranges/genus/?' do
authorized?
begin
halt_method
data = RangesGenus.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/ranges/spatial/?' do
authorized?
begin
halt_method
data = RangesSpatial.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# stem routes
get '/stem/species/?' do
authorized?
begin
halt_method
data = StemSpecies.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/stem/genus/?' do
authorized?
begin
halt_method
data = StemGenus.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/stem/family/?' do
authorized?
begin
halt_method
data = StemFamily.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
get '/stem/datasource/?' do
authorized?
begin
halt_method
data = StemDataSource.endpoint(params)
raise Exception.new('no results found') if data.length.zero?
ha = { count: data.limit(nil).count(1), returned: data.length, data: data, error: nil }
serve_data(ha, data)
rescue Exception => e
halt 400, { count: 0, returned: 0, data: nil, error: { message: e.message }}.to_json
end
end
# prevent some routes
route :copy, :patch, :put, :post, :options, :trace, :delete, '/*' do
halt 405
end
end