testrb 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +28 -0
- data/README.md +141 -0
- data/lib/testrb.rb +35 -0
- data/lib/testrb/api_helper.rb +275 -0
- data/lib/testrb/configuration.rb +51 -0
- data/lib/testrb/controllers/base_controller.rb +51 -0
- data/lib/testrb/controllers/simple_calculator_controller.rb +53 -0
- data/lib/testrb/exceptions/api_exception.rb +20 -0
- data/lib/testrb/http/faraday_client.rb +64 -0
- data/lib/testrb/http/http_call_back.rb +24 -0
- data/lib/testrb/http/http_client.rb +104 -0
- data/lib/testrb/http/http_context.rb +20 -0
- data/lib/testrb/http/http_method_enum.rb +13 -0
- data/lib/testrb/http/http_request.rb +50 -0
- data/lib/testrb/http/http_response.rb +23 -0
- data/lib/testrb/models/base_model.rb +36 -0
- data/lib/testrb/models/operation_type_enum.rb +23 -0
- data/lib/testrb/testrb_client.rb +21 -0
- data/test/controllers/controller_test_base.rb +33 -0
- data/test/controllers/test_simple_calculator_controller.rb +33 -0
- data/test/http_response_catcher.rb +20 -0
- data/test/test_helper.rb +99 -0
- metadata +161 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
# CohesityManagementSdk
|
7
|
+
module Testrb
|
8
|
+
# All configuration including auth info and base URI for the API access
|
9
|
+
# are configured in this class.
|
10
|
+
class Configuration
|
11
|
+
# Set the array parameter serialization method.
|
12
|
+
# (allowed: indexed, unindexed, plain, csv, tsv, psv)
|
13
|
+
@array_serialization = 'indexed'
|
14
|
+
|
15
|
+
# An enum for SDK environments.
|
16
|
+
class Environment
|
17
|
+
#PRODUCTION: This environment connect to the LIVE calculator API
|
18
|
+
ENVIRONMENT = [PRODUCTION = 0].freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
# An enum for API servers.
|
22
|
+
class Server
|
23
|
+
SERVER = [CALCULATOR = 0].freeze
|
24
|
+
end
|
25
|
+
|
26
|
+
# The environment in which the SDK is running.
|
27
|
+
@environment = Environment::PRODUCTION
|
28
|
+
|
29
|
+
# All the environments the SDK can run in.
|
30
|
+
@environments = {
|
31
|
+
Environment::PRODUCTION => {
|
32
|
+
Server::CALCULATOR => 'http://examples.apimatic.io/apps/calculator'
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
# Generates the appropriate base URI for the environment and the server.
|
37
|
+
# @param [Configuration::Server] The server enum for which the base URI is
|
38
|
+
# required.
|
39
|
+
# @return [String] The base URI.
|
40
|
+
def self.get_base_uri(server = Server::CALCULATOR)
|
41
|
+
environments[environment][server].clone
|
42
|
+
end
|
43
|
+
|
44
|
+
# The attribute accessors for public properties.
|
45
|
+
class << self
|
46
|
+
attr_accessor :array_serialization
|
47
|
+
attr_accessor :environment
|
48
|
+
attr_accessor :environments
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# Base controller.
|
8
|
+
class BaseController
|
9
|
+
attr_accessor :http_client, :http_call_back
|
10
|
+
|
11
|
+
def initialize(http_client: nil, http_call_back: nil)
|
12
|
+
@http_client = http_client || FaradayClient.new
|
13
|
+
@http_call_back = http_call_back
|
14
|
+
|
15
|
+
@global_headers = {
|
16
|
+
'user-agent' => 'APIMATIC 2.0'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_parameters(args)
|
21
|
+
args.each do |_name, value|
|
22
|
+
if value.nil?
|
23
|
+
raise ArgumentError, "Required parameter #{_name} cannot be nil."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute_request(request, binary: false)
|
29
|
+
@http_call_back.on_before_request(request) if @http_call_back
|
30
|
+
|
31
|
+
APIHelper.clean_hash(request.headers)
|
32
|
+
request.headers = @global_headers.clone.merge(request.headers)
|
33
|
+
|
34
|
+
response = if binary
|
35
|
+
@http_client.execute_as_binary(request)
|
36
|
+
else
|
37
|
+
@http_client.execute_as_string(request)
|
38
|
+
end
|
39
|
+
context = HttpContext.new(request, response)
|
40
|
+
|
41
|
+
@http_call_back.on_after_response(context) if @http_call_back
|
42
|
+
|
43
|
+
context
|
44
|
+
end
|
45
|
+
|
46
|
+
def validate_response(context)
|
47
|
+
raise APIException.new 'HTTP Response Not OK', context unless
|
48
|
+
context.response.status_code.between?(200, 208) # [200,208] = HTTP OK
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# SimpleCalculatorController
|
8
|
+
class SimpleCalculatorController < BaseController
|
9
|
+
@instance = SimpleCalculatorController.new
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :instance
|
13
|
+
end
|
14
|
+
|
15
|
+
def instance
|
16
|
+
self.class.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
# Calculates the expression using the specified operation.
|
20
|
+
# @param [OperationTypeEnum] operation Required parameter: The operator to
|
21
|
+
# apply on the variables
|
22
|
+
# @param [Float] x Required parameter: The LHS value
|
23
|
+
# @param [Float] y Required parameter: The RHS value
|
24
|
+
# @return Float response from the API call
|
25
|
+
def get_calculate(options = {})
|
26
|
+
# Prepare query url.
|
27
|
+
_path_url = '/{operation}'
|
28
|
+
_path_url = APIHelper.append_url_with_template_parameters(
|
29
|
+
_path_url,
|
30
|
+
'operation' => options['operation']
|
31
|
+
)
|
32
|
+
_query_builder = Configuration.get_base_uri
|
33
|
+
_query_builder << _path_url
|
34
|
+
_query_builder = APIHelper.append_url_with_query_parameters(
|
35
|
+
_query_builder,
|
36
|
+
{
|
37
|
+
'x' => options['x'],
|
38
|
+
'y' => options['y']
|
39
|
+
},
|
40
|
+
array_serialization: Configuration.array_serialization
|
41
|
+
)
|
42
|
+
_query_url = APIHelper.clean_url _query_builder
|
43
|
+
# Prepare and execute HttpRequest.
|
44
|
+
_request = @http_client.get(
|
45
|
+
_query_url
|
46
|
+
)
|
47
|
+
_context = execute_request(_request)
|
48
|
+
validate_response(_context)
|
49
|
+
# Return appropriate response type.
|
50
|
+
_context.response.raw_body.to_f
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# Class for exceptions when there is a network error, status code error, etc.
|
8
|
+
class APIException < StandardError
|
9
|
+
attr_reader :context, :response_code
|
10
|
+
|
11
|
+
# The constructor.
|
12
|
+
# @param [String] The reason for raising an exception.
|
13
|
+
# @param [HttpContext] The HttpContext of the API call.
|
14
|
+
def initialize(reason, context)
|
15
|
+
super(reason)
|
16
|
+
@context = context
|
17
|
+
@response_code = context.response.status_code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
require 'faraday/http_cache'
|
7
|
+
require 'faraday_middleware'
|
8
|
+
|
9
|
+
module Testrb
|
10
|
+
# An implementation of HttpClient.
|
11
|
+
class FaradayClient < HttpClient
|
12
|
+
# The constructor.
|
13
|
+
def initialize(timeout: nil, cache: false,
|
14
|
+
max_retries: nil, retry_interval: nil)
|
15
|
+
@connection = Faraday.new do |faraday|
|
16
|
+
faraday.use Faraday::HttpCache, serializer: Marshal if cache
|
17
|
+
faraday.use FaradayMiddleware::FollowRedirects
|
18
|
+
faraday.request :multipart
|
19
|
+
faraday.request :url_encoded
|
20
|
+
faraday.ssl[:ca_file] = Certifi.where
|
21
|
+
faraday.request :retry, max: max_retries, interval: if max_retries &&
|
22
|
+
retry_interval
|
23
|
+
retry_interval
|
24
|
+
end
|
25
|
+
faraday.adapter Faraday.default_adapter
|
26
|
+
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
27
|
+
faraday.options[:open_timeout] = timeout if timeout
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Method overridden from HttpClient.
|
32
|
+
def execute_as_string(http_request)
|
33
|
+
response = @connection.send(
|
34
|
+
http_request.http_method.downcase,
|
35
|
+
http_request.query_url
|
36
|
+
) do |request|
|
37
|
+
request.headers = http_request.headers
|
38
|
+
unless http_request.parameters.empty?
|
39
|
+
request.body = http_request.parameters
|
40
|
+
end
|
41
|
+
end
|
42
|
+
convert_response(response)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Method overridden from HttpClient.
|
46
|
+
def execute_as_binary(http_request)
|
47
|
+
response = @connection.send(
|
48
|
+
http_request.http_method.downcase,
|
49
|
+
http_request.query_url
|
50
|
+
) do |request|
|
51
|
+
request.headers = http_request.headers
|
52
|
+
unless http_request.parameters.empty?
|
53
|
+
request.body = http_request.parameters
|
54
|
+
end
|
55
|
+
end
|
56
|
+
convert_response(response)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Method overridden from HttpClient.
|
60
|
+
def convert_response(response)
|
61
|
+
HttpResponse.new(response.status, response.headers, response.body)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# HttpCallBack allows defining callables for pre and post API calls.
|
8
|
+
class HttpCallBack
|
9
|
+
# A controller will call this method before making an HTTP Request.
|
10
|
+
# @param [HttpRequest] The HttpRequest object which the HttpClient
|
11
|
+
# will execute.
|
12
|
+
def on_before_request(_http_request)
|
13
|
+
raise NotImplementedError, 'This method needs
|
14
|
+
to be implemented in a child class.'
|
15
|
+
end
|
16
|
+
|
17
|
+
# A controller will call this method after making an HTTP Request.
|
18
|
+
# @param [HttpContext] The HttpContext of the API call.
|
19
|
+
def on_after_response(_http_context)
|
20
|
+
raise NotImplementedError, 'This method needs
|
21
|
+
to be implemented in a child class.'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# An interface for the methods that an HTTP Client must implement.
|
8
|
+
#
|
9
|
+
# This class should not be instantiated but should be used as a base class
|
10
|
+
# for HTTP Client classes.
|
11
|
+
class HttpClient
|
12
|
+
# Execute an HttpRequest when the response is expected to be a string.
|
13
|
+
# @param [HttpRequest] The HttpRequest to be executed.
|
14
|
+
def execute_as_string(_http_request)
|
15
|
+
raise NotImplementedError, 'This method needs
|
16
|
+
to be implemented in a child class.'
|
17
|
+
end
|
18
|
+
|
19
|
+
# Execute an HttpRequest when the response is expected to be binary.
|
20
|
+
# @param [HttpRequest] The HttpRequest to be executed.
|
21
|
+
def execute_as_binary(_http_request)
|
22
|
+
raise NotImplementedError, 'This method needs
|
23
|
+
to be implemented in a child class.'
|
24
|
+
end
|
25
|
+
|
26
|
+
# Converts the HTTP Response from the client to an HttpResponse object.
|
27
|
+
# @param [Dynamic] The response object received from the client.
|
28
|
+
def convert_response(_response)
|
29
|
+
raise NotImplementedError, 'This method needs
|
30
|
+
to be implemented in a child class.'
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get a GET HttpRequest object.
|
34
|
+
# @param [String] The URL to send the request to.
|
35
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
36
|
+
def get(query_url,
|
37
|
+
headers: {})
|
38
|
+
HttpRequest.new(HttpMethodEnum::GET,
|
39
|
+
query_url,
|
40
|
+
headers: headers)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get a HEAD HttpRequest object.
|
44
|
+
# @param [String] The URL to send the request to.
|
45
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
46
|
+
def head(query_url,
|
47
|
+
headers: {})
|
48
|
+
HttpRequest.new(HttpMethodEnum::HEAD,
|
49
|
+
query_url,
|
50
|
+
headers: headers)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get a POST HttpRequest object.
|
54
|
+
# @param [String] The URL to send the request to.
|
55
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
56
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
57
|
+
def post(query_url,
|
58
|
+
headers: {},
|
59
|
+
parameters: {})
|
60
|
+
HttpRequest.new(HttpMethodEnum::POST,
|
61
|
+
query_url,
|
62
|
+
headers: headers,
|
63
|
+
parameters: parameters)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Get a PUT HttpRequest object.
|
67
|
+
# @param [String] The URL to send the request to.
|
68
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
69
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
70
|
+
def put(query_url,
|
71
|
+
headers: {},
|
72
|
+
parameters: {})
|
73
|
+
HttpRequest.new(HttpMethodEnum::PUT,
|
74
|
+
query_url,
|
75
|
+
headers: headers,
|
76
|
+
parameters: parameters)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Get a PATCH HttpRequest object.
|
80
|
+
# @param [String] The URL to send the request to.
|
81
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
82
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
83
|
+
def patch(query_url,
|
84
|
+
headers: {},
|
85
|
+
parameters: {})
|
86
|
+
HttpRequest.new(HttpMethodEnum::PATCH,
|
87
|
+
query_url,
|
88
|
+
headers: headers,
|
89
|
+
parameters: parameters)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Get a DELETE HttpRequest object.
|
93
|
+
# @param [String] The URL to send the request to.
|
94
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
95
|
+
def delete(query_url,
|
96
|
+
headers: {},
|
97
|
+
parameters: {})
|
98
|
+
HttpRequest.new(HttpMethodEnum::DELETE,
|
99
|
+
query_url,
|
100
|
+
headers: headers,
|
101
|
+
parameters: parameters)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# Represents an Http call in context.
|
8
|
+
class HttpContext
|
9
|
+
attr_accessor :request, :response
|
10
|
+
|
11
|
+
# The constructor.
|
12
|
+
# @param [HttpRequest] An HttpRequest object representing the HTTP request.
|
13
|
+
# @param [HttpResponse] An HttpResponse object representing the HTTP
|
14
|
+
# response.
|
15
|
+
def initialize(request, response)
|
16
|
+
@request = request
|
17
|
+
@response = response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# HTTP Methods Enumeration.
|
8
|
+
class HttpMethodEnum
|
9
|
+
HTTPMETHODENUM = [GET = 'GET'.freeze, POST = 'POST'.freeze,
|
10
|
+
PUT = 'PUT'.freeze, PATCH = 'PATCH'.freeze,
|
11
|
+
DELETE = 'DELETE'.freeze, HEAD = 'HEAD'.freeze].freeze
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# testrb
|
2
|
+
#
|
3
|
+
# This file was automatically generated by APIMATIC v2.0 (
|
4
|
+
# https://apimatic.io ).
|
5
|
+
|
6
|
+
module Testrb
|
7
|
+
# Represents a single Http Request.
|
8
|
+
class HttpRequest
|
9
|
+
attr_accessor :http_method, :query_url, :headers,
|
10
|
+
:parameters, :username, :password
|
11
|
+
|
12
|
+
# The constructor.
|
13
|
+
# @param [HttpMethodEnum] The HTTP method.
|
14
|
+
# @param [String] The URL to send the request to.
|
15
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
16
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
17
|
+
def initialize(http_method,
|
18
|
+
query_url,
|
19
|
+
headers: {},
|
20
|
+
parameters: {})
|
21
|
+
@http_method = http_method
|
22
|
+
@query_url = query_url
|
23
|
+
@headers = headers
|
24
|
+
@parameters = parameters
|
25
|
+
end
|
26
|
+
|
27
|
+
# Add a header to the HttpRequest.
|
28
|
+
# @param [String] The name of the header.
|
29
|
+
# @param [String] The value of the header.
|
30
|
+
def add_header(name, value)
|
31
|
+
@headers[name] = value
|
32
|
+
end
|
33
|
+
|
34
|
+
# Add a parameter to the HttpRequest.
|
35
|
+
# @param [String] The name of the parameter.
|
36
|
+
# @param [String] The value of the parameter.
|
37
|
+
def add_parameter(name, value)
|
38
|
+
@parameters[name] = value
|
39
|
+
end
|
40
|
+
|
41
|
+
# Add a query parameter to the HttpRequest.
|
42
|
+
# @param [String] The name of the query parameter.
|
43
|
+
# @param [String] The value of the query parameter.
|
44
|
+
def add_query_parameter(name, value)
|
45
|
+
@query_url = APIHelper.append_url_with_query_parameters(@query_url,
|
46
|
+
name => value)
|
47
|
+
@query_url = APIHelper.clean_url(@query_url)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|