-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails.rb
executable file
·150 lines (126 loc) · 4.23 KB
/
details.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
#!/usr/bin/ruby
#
# Copyright 2017 Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'webrick'
require 'json'
require 'net/http'
if ARGV.length < 1 then
puts "usage: #{$PROGRAM_NAME} port"
exit(-1)
end
port = Integer(ARGV[0])
server = WEBrick::HTTPServer.new :BindAddress => '0.0.0.0', :Port => port
trap 'INT' do server.shutdown end
server.mount_proc '/health' do |req, res|
res.status = 200
res.body = {'status' => 'Details is healthy'}.to_json
res['Content-Type'] = 'application/json'
end
server.mount_proc '/details' do |req, res|
pathParts = req.path.split('/')
headers = get_forward_headers(req)
begin
begin
id = Integer(pathParts[-1])
rescue
raise 'please provide numeric product id'
end
details = get_book_details(id, headers)
res.body = details.to_json
res['Content-Type'] = 'application/json'
rescue => error
res.body = {'error' => error}.to_json
res['Content-Type'] = 'application/json'
res.status = 400
end
end
# TODO: provide details on different books.
def get_book_details(id, headers)
if ENV['ENABLE_EXTERNAL_BOOK_SERVICE'] === 'true' then
# the ISBN of one of Comedy of Errors on the Amazon
# that has Shakespeare as the single author
isbn = '0486424618'
return fetch_details_from_external_service(isbn, id, headers)
end
return {
'id' => id,
'author': 'William Shakespeare',
'year': 1595,
'type' => 'paperback',
'pages' => 200,
'publisher' => 'PublisherA',
'language' => 'English',
'ISBN-10' => '1234567890',
'ISBN-13' => '123-1234567890'
}
end
def fetch_details_from_external_service(isbn, id, headers)
uri = URI.parse('https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn)
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 5 # seconds
# DO_NOT_ENCRYPT is used to configure the details service to use either
# HTTP (true) or HTTPS (false, default) when calling the external service to
# retrieve the book information.
#
# Unless this environment variable is set to true, the app will use TLS (HTTPS)
# to access external services.
unless ENV['DO_NOT_ENCRYPT'] === 'true' then
http.use_ssl = true
end
request = Net::HTTP::Get.new(uri.request_uri)
headers.each { |header, value| request[header] = value }
response = http.request(request)
json = JSON.parse(response.body)
book = json['items'][0]['volumeInfo']
language = book['language'] === 'en'? 'English' : 'unknown'
type = book['printType'] === 'BOOK'? 'paperback' : 'unknown'
isbn10 = get_isbn(book, 'ISBN_10')
isbn13 = get_isbn(book, 'ISBN_13')
return {
'id' => id,
'author': book['authors'][0],
'year': book['publishedDate'],
'type' => type,
'pages' => book['pageCount'],
'publisher' => book['publisher'],
'language' => language,
'ISBN-10' => isbn10,
'ISBN-13' => isbn13
}
end
def get_isbn(book, isbn_type)
isbn_dentifiers = book['industryIdentifiers'].select do |identifier|
identifier['type'] === isbn_type
end
return isbn_dentifiers[0]['identifier']
end
def get_forward_headers(request)
headers = {}
incoming_headers = [ 'x-request-id',
'x-b3-traceid',
'x-b3-spanid',
'x-b3-parentspanid',
'x-b3-sampled',
'x-b3-flags',
'x-ot-span-context'
]
request.each do |header, value|
if incoming_headers.include? header then
headers[header] = value
end
end
return headers
end
server.start