rw-api-microservice 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6be14b3c6d8e3bce30dd3d2c92000bd085a099edf178616c3d8e6222019bbe9b
4
+ data.tar.gz: 1254e1a256e2a061717729bc0301d09f8a9000fa9d07ddfa1dac0e3f5b7c93d1
5
+ SHA512:
6
+ metadata.gz: d1956547a32d879d8518b51eb029dc0ffdaea76b3716d2d16b3638474921397d1d662664ff994518a661d302b3c041b66a9482951f1d59281381ea5ac15af430
7
+ data.tar.gz: 566ccfca969f57d75c7919756689c9cd8bb1ca3e3fa52d27befcadf09a2566124de7d18415b8c6dd201cb1e8c93eacf964950b05f41c4a47203c7e4f9e8c1d3a
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Api Gateway registration Rails engine
2
+
3
+ Rails engine that integrates your rails-build microservices with the [RW API](https://api.resourcewatch.org/)
4
+
5
+ [![Build Status](https://travis-ci.org/control-tower/rw-api-microservice-rails.svg?branch=master)](https://travis-ci.org/control-tower/rw-api-microservice-rails)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/85eeb71033246c5c259d/test_coverage)](https://codeclimate.com/github/control-tower/rw-api-microservice-rails/test_coverage)
7
+
8
+ ## Installation
9
+
10
+ Installing using `bundler` is recommended:
11
+
12
+ ```ruby
13
+ # Gemfile
14
+
15
+ gem 'rw-api-microservice-rails'
16
+ ```
17
+
18
+ And then execute:
19
+ ```bash
20
+ $ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Create a Rails initializer to define your connection settings to the RW API Gateway
26
+
27
+ ```ruby
28
+ #config/initializers/rw_api_microservice.rb
29
+ RwApiMicroservice.configure do |config|
30
+ config.gateway_url = 'http://your-gateway-url.com'
31
+ config.microservice_token = 'Gateway auth token'
32
+ end
33
+ ```
34
+
35
+
36
+ ## Current methods
37
+
38
+ This engine currently implements a convenience method:
39
+
40
+ - `microservice_request()` makes a request to a different microservice within the RW API environment.
41
+
42
+ ## Contributing
43
+ Feel free to contribute, pull requests are welcome.
44
+
45
+ ## License
46
+ The engine is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
48
+
49
+ ## Testing
50
+
51
+ ```bash
52
+ bundle exec rspec spec
53
+ ```
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ load 'lib/tasks/rw_api_microservice.rake'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'RwApiMicroservice'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+
23
+ load 'rails/tasks/statistics.rake'
24
+
25
+
26
+
27
+ require 'bundler/gem_tasks'
28
+
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/rw_api_microservice .js
2
+ //= link_directory ../stylesheets/rw_api_microservice .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ module RwApiMicroservice
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require_dependency "rw_api_microservice/application_controller"
2
+
3
+ module RwApiMicroservice
4
+ class InfoController < ApplicationController
5
+ def ping
6
+ render json: { success: true }, status: 200
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module RwApiMicroservice
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RwApiMicroservice
2
+ module InfoHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Ct register microservice</title>
5
+ <%= stylesheet_link_tag "rw_api_microservice/application", media: "all" %>
6
+ <%= javascript_include_tag "rw_api_microservice/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ RwApiMicroservice::Engine.routes.draw do
2
+ get :info, action: :info, controller: 'info'
3
+ get :ping, action: :ping, controller: 'info'
4
+ end
@@ -0,0 +1,11 @@
1
+ require 'rw_api_microservice'
2
+
3
+ module RwApiMicroservice
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace RwApiMicroservice
6
+
7
+ config.generators do |g|
8
+ g.test_framework :rspec
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,77 @@
1
+ module RwApiMicroservice
2
+
3
+ class MissingConfigError < StandardError;
4
+ end
5
+
6
+ class RwApiMicroserviceError < StandardError;
7
+ end
8
+
9
+ class AppSecretNotDefinedError < ::RwApiMicroservice::RwApiMicroserviceError;
10
+ end
11
+
12
+ class APIError < ::RwApiMicroservice::RwApiMicroserviceError
13
+ attr_accessor :rw_error_type, :rw_error_code, :rw_error_subcode, :rw_error_message,
14
+ :rw_error_user_msg, :rw_error_user_title, :http_status, :response_body
15
+
16
+
17
+ def initialize(http_status, response_body, error_message = nil)
18
+ if response_body
19
+ self.response_body = response_body.strip
20
+ else
21
+ self.response_body = ''
22
+ end
23
+ self.http_status = http_status
24
+
25
+ if error_message && error_message.is_a?(String)
26
+ message = error_message
27
+ else
28
+ unless error_message
29
+ begin
30
+ errors = MultiJson.load(response_body) if response_body
31
+ error_array = errors['errors'].map { |n| n['detail'] } if errors.key? 'errors'
32
+ rescue
33
+ end
34
+ error_array ||= []
35
+ end
36
+
37
+ if error_array.nil? or error_array.empty?
38
+ message = self.response_body
39
+ else
40
+ message = error_array.join(', ')
41
+ end
42
+ end
43
+
44
+ super(message)
45
+ end
46
+ end
47
+
48
+ # CT returned an invalid response body
49
+ class BadCTResponse < APIError;
50
+ end
51
+
52
+ # CT responded with an error while attempting to request an access token
53
+ class OAuthTokenRequestError < APIError;
54
+ end
55
+
56
+ # Any error with a 5xx HTTP status code
57
+ class ServerError < APIError;
58
+ end
59
+
60
+ # Any error with a 4xx HTTP status code
61
+ class ClientError < APIError;
62
+ end
63
+
64
+ # All API authentication failures.
65
+ class AuthenticationError < ClientError;
66
+ end
67
+
68
+ # not found
69
+ class NotFoundError < APIError;
70
+ end
71
+
72
+ # not found
73
+ class NoTokenError < APIError;
74
+ end
75
+
76
+
77
+ end
@@ -0,0 +1,60 @@
1
+ require 'rw_api_microservice/errors'
2
+
3
+ module RwApiMicroservice
4
+ class Gateway
5
+ def initialize
6
+ if RwApiMicroservice.config.gateway_url.nil?
7
+ raise MissingConfigError, 'Could not register microservice - No Gateway URL defined'
8
+ end
9
+ if RwApiMicroservice.config.microservice_token.nil?
10
+ raise MissingConfigError, 'Could not register microservice - No Microservice Token found'
11
+ end
12
+
13
+ @options = OpenStruct.new
14
+ end
15
+
16
+ attr_reader :credentials, :options, :response, :gateway_url
17
+
18
+ # TODO: make sure it works as intended, add unit tests
19
+ def microservice_request(uri, method, headers = {}, body = nil)
20
+ options.http_method = method
21
+ options.endpoint = uri
22
+ options.headers = headers
23
+ options.headers['authorization'] = 'Bearer '+RwApiMicroservice.config.microservice_token
24
+ options.body = body
25
+ result = make_call(options)
26
+ result
27
+ end
28
+
29
+ def initialize_options
30
+ @options = OpenStruct.new
31
+ end
32
+
33
+ private
34
+
35
+ def make_call(options)
36
+ result = RwApiMicroservice.make_request(options, credentials)
37
+ unless check_errors(result.status.to_i, result.body)
38
+ MultiJson.load("[#{result.body.to_s}]")[0]
39
+ end
40
+ initialize_options
41
+ @response = result.body
42
+ end
43
+
44
+ def check_errors(status, body)
45
+ case status
46
+ when 500
47
+ initialize_options
48
+ raise RwApiMicroservice::ServerError.new(status, body)
49
+ when 401
50
+ initialize_options
51
+ raise RwApiMicroservice::NoTokenError.new(status, body)
52
+ when 404
53
+ initialize_options
54
+ raise RwApiMicroservice::NotFoundError.new(status, body)
55
+ else
56
+ return false
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ module RwApiMicroservice
2
+ module HTTPService
3
+ class Endpoint
4
+
5
+ attr_reader :options, :credentials
6
+
7
+ def initialize(options, credentials = {})
8
+ @options = options
9
+ @credentials = credentials
10
+ end
11
+
12
+ def get
13
+ endpoint_uri
14
+ end
15
+
16
+ private
17
+
18
+ def endpoint_uri
19
+ options.endpoint
20
+ end
21
+
22
+ end
23
+ end
24
+ Endpoint = HTTPService::Endpoint
25
+ end
26
+
@@ -0,0 +1,14 @@
1
+ module RwApiMicroservice
2
+ module HTTPService
3
+ class Response
4
+ attr_reader :status, :body, :headers
5
+
6
+ def initialize(status, body, headers)
7
+ @status = status
8
+ @body = body
9
+ @headers = headers
10
+ end
11
+ end
12
+ end
13
+ Response = HTTPService::Response
14
+ end
@@ -0,0 +1,43 @@
1
+ require 'httparty'
2
+ require 'rw_api_microservice/http_service/response'
3
+ require 'rw_api_microservice/http_service/endpoint'
4
+
5
+ module RwApiMicroservice
6
+ module HTTPService
7
+ class << self
8
+ end
9
+
10
+ def self.make_request(options, credentials = {})
11
+ http_method = options.http_method&.to_sym || :get
12
+ url = RwApiMicroservice.config.gateway_url
13
+ headers = options.headers || {}
14
+ headers['Content-Type'] = 'application/json' if options.http_method == 'post' || options.http_method == 'put'
15
+ endpoint = Endpoint.new(options, credentials).get
16
+ body = options.body
17
+
18
+ case http_method
19
+ when :get
20
+ response = HTTParty.get(url+endpoint, headers: headers)
21
+ when :delete
22
+ response = HTTParty.delete(url+endpoint, headers: headers)
23
+ when :post
24
+ response = HTTParty.post(url+endpoint, {
25
+ headers: headers,
26
+ body: body
27
+ })
28
+ when :put
29
+ response = HTTParty.put(url+endpoint, {
30
+ headers: headers,
31
+ body: body
32
+ })
33
+ when :patch
34
+ response = HTTParty.patch(url+endpoint, {
35
+ headers: headers,
36
+ body: body
37
+ })
38
+ end
39
+
40
+ RwApiMicroservice::HTTPService::Response.new(response.code.to_i, response.body, response.headers)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module RwApiMicroservice
2
+ VERSION = '2.0.0'
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'rw_api_microservice/engine'
2
+ require 'rw_api_microservice/gateway'
3
+ require 'rw_api_microservice/http_service'
4
+ require 'rw_api_microservice/errors'
5
+ require 'multi_json'
6
+ require 'ostruct'
7
+
8
+ module RwApiMicroservice
9
+ class << self
10
+ attr_accessor :http_service, :config
11
+
12
+ def configure
13
+ yield config
14
+ end
15
+
16
+ def config
17
+ @config ||= OpenStruct.new
18
+ end
19
+ end
20
+
21
+ def self.http_service=(service)
22
+ @http_service = service
23
+ end
24
+
25
+ def self.make_request(options, credentials = {})
26
+ http_service.make_request(options, credentials)
27
+ end
28
+
29
+ self.http_service = HTTPService
30
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rw-api-microservice
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tiago Garcia
8
+ - Vizzuality
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2022-09-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 7.0.4
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 7.0.4
28
+ - !ruby/object:Gem::Dependency
29
+ name: httparty
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: multi_json
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: simplecov-console
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: webmock
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rspec-rails
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: sqlite3
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ description: This gem allows your Rails-built microservice to easily integrate with
141
+ the RW API
142
+ email:
143
+ - info@vizzuality.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - README.md
149
+ - Rakefile
150
+ - app/assets/config/rw_api_microservice_manifest.js
151
+ - app/assets/javascripts/rw_api_microservice/application.js
152
+ - app/assets/javascripts/rw_api_microservice/info.js
153
+ - app/assets/stylesheets/rw_api_microservice/application.css
154
+ - app/assets/stylesheets/rw_api_microservice/info.css
155
+ - app/controllers/rw_api_microservice/application_controller.rb
156
+ - app/controllers/rw_api_microservice/info_controller.rb
157
+ - app/helpers/rw_api_microservice/application_helper.rb
158
+ - app/helpers/rw_api_microservice/info_helper.rb
159
+ - app/views/layouts/rw_api_microservice/application.html.erb
160
+ - config/routes.rb
161
+ - lib/rw_api_microservice.rb
162
+ - lib/rw_api_microservice/engine.rb
163
+ - lib/rw_api_microservice/errors.rb
164
+ - lib/rw_api_microservice/gateway.rb
165
+ - lib/rw_api_microservice/http_service.rb
166
+ - lib/rw_api_microservice/http_service/endpoint.rb
167
+ - lib/rw_api_microservice/http_service/response.rb
168
+ - lib/rw_api_microservice/version.rb
169
+ homepage: https://vizzuality.com
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 3.3.22
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: RW API connector service
192
+ test_files: []