uc3-dmp-external-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/uc3-dmp-external-api/client.rb +84 -0
- data/lib/uc3-dmp-external-api/version.rb +5 -0
- data/lib/uc3-dmp-external-api.rb +12 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2f1ba261082e6ae6433baff78ec825e20d2ef45eed27da85fdf39eb9521ea5cd
|
4
|
+
data.tar.gz: 18c36e8a2561f4b659299afea146ef9cff81936b9e5a69eba76dcc04bed5f5d8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46402162efcd0f607d0b1d54391f79a383fda98f4df86ca7b72f548cf931049aecb21d41d08e695214294201b3e8635bfe7534b3967f26ea7d25359554310a16
|
7
|
+
data.tar.gz: 0edf9e90f3678ad22119d8c70c19be5b90f9f236d62e22bf0753de5c489ea3d8544960f9815e427ce3183fd5125273d47786026befb2608275288654b7055fc8
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require 'json'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Uc3DmpExternalApi
|
8
|
+
# Error from the External API
|
9
|
+
class ExternalApiError < StandardError; end
|
10
|
+
|
11
|
+
# Helper class for communicating with external APIs
|
12
|
+
class Client
|
13
|
+
MSG_ERROR_FROM_EXTERNAL_API = 'Received an error from %<url>s'
|
14
|
+
MSG_HTTPARTY_ERR = 'HTTParty failure when trying to call %<url>s'
|
15
|
+
MSG_INVALID_URI = '%<url>s is an invalid URI'
|
16
|
+
MSG_UNABLE_TO_PARSE = 'Unable to parse the response from %<url>s'
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Call the specified URL using the specified HTTP method, body and headers
|
20
|
+
# rubocop:disable Metrics/AbcSize
|
21
|
+
def call(url:, method: :get, body: '', additional_headers: {}, debug: false)
|
22
|
+
uri = URI(url)
|
23
|
+
# Skip the body if we are doing a get
|
24
|
+
body = nil if method.to_sym == :get
|
25
|
+
opts = _options(body: body, additional_headers: additional_headers, debug: debug)
|
26
|
+
resp = HTTParty.send(method.to_sym, uri, opts)
|
27
|
+
|
28
|
+
if resp.code != 200
|
29
|
+
msg = "status: #{resp&.code}, body: #{resp&.body}"
|
30
|
+
raise ExternalApiError, "#{format(MSG_ERROR_FROM_EXTERNAL_API, url: url)} - #{msg}"
|
31
|
+
end
|
32
|
+
resp.body.nil? || resp.body.empty? ? nil : _process_response(resp)
|
33
|
+
rescue JSON::ParserError
|
34
|
+
raise ExternalApiError, format(MSG_UNABLE_TO_PARSE, url: url)
|
35
|
+
rescue HTTParty::Error => e
|
36
|
+
raise ExternalApiError, "#{format(MSG_HTTPARTY_ERR, url: url)} - #{e.message}"
|
37
|
+
rescue URI::InvalidURIError
|
38
|
+
raise ExternalApiError, format(MSG_INVALID_URI, url: url)
|
39
|
+
end
|
40
|
+
# rubocop:enable Metrics/AbcSize
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# # Handle the response body based on the Content-Type
|
45
|
+
def _process_response(resp:)
|
46
|
+
return nil unless resp.nil? || resp.body.nil?
|
47
|
+
|
48
|
+
case resp.headers.fetch('content-type', '')
|
49
|
+
when 'application/json'
|
50
|
+
JSON.parse(resp.body)
|
51
|
+
else
|
52
|
+
resp.body.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Prepare the headers
|
57
|
+
def _headers(additional_headers: {})
|
58
|
+
domain = ENV['DOMAIN'].nil? ? 'dmptool.org' : ENV['DOMAIN']
|
59
|
+
email = ENV['ADMIN_EMAIL'].nil? ? 'dmptool@ucop.edu' : ENV['ADMIN_EMAIL']
|
60
|
+
base = {
|
61
|
+
'Content-Type': 'application/json',
|
62
|
+
Accept: 'application/json',
|
63
|
+
'User-Agent': "DMPTool #{ENV.fetch('LAMBDA_ENV', nil)} - #{domain} (mailto: #{email})"
|
64
|
+
}
|
65
|
+
base.merge(additional_headers)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Prepare the HTTParty gem options
|
69
|
+
def _options(body:, additional_headers: {}, debug: false)
|
70
|
+
hdrs = _headers(additional_headers: additional_headers)
|
71
|
+
opts = {
|
72
|
+
headers: hdrs,
|
73
|
+
follow_redirects: true,
|
74
|
+
limit: 6
|
75
|
+
}
|
76
|
+
# If the body is not already JSON and we intend to send JSON, convert it
|
77
|
+
opts[:body] = body.is_a?(Hash) && hdrs['Content-Type'] == 'application/json' ? body.to_json : body
|
78
|
+
# If debug is enabled then tap into the HTTParty gem's debug option
|
79
|
+
opts[:debug_output] = $stdout if debug
|
80
|
+
opts
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uc3-dmp-external-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Riley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: active_record_simple_execute
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-sns
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.60'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.60'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: aws-sdk-ssm
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.150'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.150'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.6'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.21.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.21.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 11.1.3
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 11.1.3
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.9.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.9.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.50.2
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.50.2
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-performance
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.17.1
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.17.1
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop-rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 2.20.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 2.20.0
|
153
|
+
description: Helpers for communicating with external systems in a standardized way
|
154
|
+
email:
|
155
|
+
- brian.riley@ucop.edu
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- README.md
|
161
|
+
- lib/uc3-dmp-external-api.rb
|
162
|
+
- lib/uc3-dmp-external-api/client.rb
|
163
|
+
- lib/uc3-dmp-external-api/version.rb
|
164
|
+
homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-external-api
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata:
|
168
|
+
rubygems_mfa_required: 'false'
|
169
|
+
post_install_message:
|
170
|
+
rdoc_options: []
|
171
|
+
require_paths:
|
172
|
+
- lib
|
173
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '2.7'
|
178
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: '0'
|
183
|
+
requirements: []
|
184
|
+
rubygems_version: 3.1.6
|
185
|
+
signing_key:
|
186
|
+
specification_version: 4
|
187
|
+
summary: DMPTool gem that provides general support for accessing external APIs
|
188
|
+
test_files: []
|