apimaticcalculatortest1 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # apimatic_calculator client class.
8
+ class Client
9
+ attr_reader :config
10
+
11
+ # Access to simple_calculator controller.
12
+ # @return [SimpleCalculatorController] Returns the controller instance.
13
+ def simple_calculator
14
+ @simple_calculator ||= SimpleCalculatorController.new config
15
+ end
16
+
17
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
18
+ backoff_factor: 1, environment: Environment::PRODUCTION,
19
+ config: nil)
20
+ @config = if config.nil?
21
+ Configuration.new(timeout: timeout, max_retries: max_retries,
22
+ retry_interval: retry_interval,
23
+ backoff_factor: backoff_factor,
24
+ environment: environment)
25
+ else
26
+ config
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,94 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # An enum for SDK environments.
8
+ class Environment
9
+ # PRODUCTION: This environment connect to the LIVE calculator API
10
+ ENVIRONMENT = [
11
+ PRODUCTION = 'production'.freeze
12
+ ].freeze
13
+ end
14
+
15
+ # An enum for API servers.
16
+ class Server
17
+ SERVER = [
18
+ CALCULATOR = 'Calculator'.freeze
19
+ ].freeze
20
+ end
21
+
22
+ # All configuration including auth info and base URI for the API access
23
+ # are configured in this class.
24
+ class Configuration
25
+ # The attribute readers for properties.
26
+ attr_reader :http_client
27
+ attr_reader :timeout
28
+ attr_reader :max_retries
29
+ attr_reader :retry_interval
30
+ attr_reader :backoff_factor
31
+ attr_reader :environment
32
+
33
+ class << self
34
+ attr_reader :environments
35
+ end
36
+
37
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
38
+ backoff_factor: 1, environment: Environment::PRODUCTION)
39
+ # The value to use for connection timeout
40
+ @timeout = timeout
41
+
42
+ # The number of times to retry an endpoint call if it fails
43
+ @max_retries = max_retries
44
+
45
+ # Pause in seconds between retries
46
+ @retry_interval = retry_interval
47
+
48
+ # The amount to multiply each successive retry's interval amount
49
+ # by in order to provide backoff
50
+ @backoff_factor = backoff_factor
51
+
52
+ # Current API environment
53
+ @environment = String(environment)
54
+
55
+ # The Http Client to use for making requests.
56
+ @http_client = create_http_client
57
+ end
58
+
59
+ def clone_with(timeout: nil, max_retries: nil, retry_interval: nil,
60
+ backoff_factor: nil, environment: nil)
61
+ timeout ||= self.timeout
62
+ max_retries ||= self.max_retries
63
+ retry_interval ||= self.retry_interval
64
+ backoff_factor ||= self.backoff_factor
65
+ environment ||= self.environment
66
+
67
+ Configuration.new(timeout: timeout, max_retries: max_retries,
68
+ retry_interval: retry_interval,
69
+ backoff_factor: backoff_factor,
70
+ environment: environment)
71
+ end
72
+
73
+ def create_http_client
74
+ FaradayClient.new(timeout: timeout, max_retries: max_retries,
75
+ retry_interval: retry_interval,
76
+ backoff_factor: backoff_factor)
77
+ end
78
+
79
+ # All the environments the SDK can run in.
80
+ ENVIRONMENTS = {
81
+ Environment::PRODUCTION => {
82
+ Server::CALCULATOR => 'http://examples.apimatic.io/apps/calculator'
83
+ }
84
+ }.freeze
85
+
86
+ # Generates the appropriate base URI for the environment and the server.
87
+ # @param [Configuration::Server] The server enum for which the base URI is
88
+ # required.
89
+ # @return [String] The base URI.
90
+ def get_base_uri(server = Server::CALCULATOR)
91
+ ENVIRONMENTS[environment][server].clone
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,49 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # BaseController.
8
+ class BaseController
9
+ attr_accessor :config, :http_call_back
10
+
11
+ def initialize(config, http_call_back: nil)
12
+ @config = config
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.merge!(@global_headers)
33
+
34
+ response = if binary
35
+ config.http_client.execute_as_binary(request)
36
+ else
37
+ config.http_client.execute_as_string(request)
38
+ end
39
+ @http_call_back.on_after_response(response) if @http_call_back
40
+
41
+ response
42
+ end
43
+
44
+ def validate_response(response)
45
+ raise APIException.new 'HTTP Response Not OK', response unless
46
+ response.status_code.between?(200, 208) # [200,208] = HTTP OK
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # SimpleCalculatorController
8
+ class SimpleCalculatorController < BaseController
9
+ def initialize(config, http_call_back: nil)
10
+ super(config, http_call_back: http_call_back)
11
+ end
12
+
13
+ # Calculates the expression using the specified operation.
14
+ # @param [OperationTypeEnum] operation Required parameter: The operator to
15
+ # apply on the variables
16
+ # @param [Float] x Required parameter: The LHS value
17
+ # @param [Float] y Required parameter: The RHS value
18
+ # @return [Float] response from the API call
19
+ def get_calculate(options = {})
20
+ # Prepare query url.
21
+ _query_builder = config.get_base_uri
22
+ _query_builder << '/{operation}'
23
+ _query_builder = APIHelper.append_url_with_template_parameters(
24
+ _query_builder,
25
+ 'operation' => { 'value' => options['operation'], 'encode' => true }
26
+ )
27
+ _query_builder = APIHelper.append_url_with_query_parameters(
28
+ _query_builder,
29
+ 'x' => options['x'],
30
+ 'y' => options['y']
31
+ )
32
+ _query_url = APIHelper.clean_url _query_builder
33
+
34
+ # Prepare and execute HttpRequest.
35
+ _request = config.http_client.get(
36
+ _query_url
37
+ )
38
+ _response = execute_request(_request)
39
+ validate_response(_response)
40
+
41
+ # Return appropriate response type.
42
+ _response.raw_body.to_f
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
7
+ # Class for exceptions when there is a network error, status code error, etc.
8
+ class APIException < StandardError
9
+ attr_reader :response, :response_code
10
+
11
+ # The constructor.
12
+ # @param [String] The reason for raising an exception.
13
+ # @param [HttpResponse] The HttpReponse of the API call.
14
+ def initialize(reason, response)
15
+ super(reason)
16
+ @response = response
17
+ @response_code = response.status_code
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,67 @@
1
+ # apimatic_calculator
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 ApimaticCalculator
10
+ # An implementation of HttpClient.
11
+ class FaradayClient < HttpClient
12
+ # The constructor.
13
+ def initialize(timeout:, max_retries:, retry_interval:,
14
+ backoff_factor:, cache: false, verify: true)
15
+ @connection = Faraday.new do |faraday|
16
+ faraday.use Faraday::HttpCache, serializer: Marshal if cache
17
+ faraday.use FaradayMiddleware::FollowRedirects
18
+ faraday.use :gzip
19
+ faraday.request :multipart
20
+ faraday.request :url_encoded
21
+ faraday.ssl[:ca_file] = Certifi.where
22
+ faraday.ssl[:verify] = verify
23
+ faraday.request :retry, max: max_retries, interval: retry_interval,
24
+ backoff_factor: backoff_factor
25
+ faraday.adapter Faraday.default_adapter
26
+ faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
27
+ faraday.options[:timeout] = timeout if timeout > 0
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.http_method == HttpMethodEnum::GET &&
39
+ http_request.parameters.empty?
40
+ request.body = http_request.parameters
41
+ end
42
+ end
43
+ convert_response(response, http_request)
44
+ end
45
+
46
+ # Method overridden from HttpClient.
47
+ def execute_as_binary(http_request)
48
+ response = @connection.send(
49
+ http_request.http_method.downcase,
50
+ http_request.query_url
51
+ ) do |request|
52
+ request.headers = http_request.headers
53
+ unless http_request.http_method == HttpMethodEnum::GET &&
54
+ http_request.parameters.empty?
55
+ request.body = http_request.parameters
56
+ end
57
+ end
58
+ convert_response(response, http_request)
59
+ end
60
+
61
+ # Method overridden from HttpClient.
62
+ def convert_response(response, http_request)
63
+ HttpResponse.new(response.status, response.reason_phrase,
64
+ response.headers, response.body, http_request)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
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 [HttpResponse] The HttpReponse of the API call.
19
+ def on_after_response(_http_response)
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
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
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,13 @@
1
+ # apimatic_calculator
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module ApimaticCalculator
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