md-notes-resource 1.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.
@@ -0,0 +1,49 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
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,140 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # OAuthAuthorizationController
8
+ class OAuthAuthorizationController < BaseController
9
+ def initialize(config, http_call_back: nil)
10
+ super(config, http_call_back: http_call_back)
11
+ end
12
+
13
+ # Create a new OAuth 2 token.
14
+ # @param [String] authorization Required parameter: Authorization header in
15
+ # Basic auth format
16
+ # @param [String] username Required parameter: Resource owner username
17
+ # @param [String] password Required parameter: Resource owner password
18
+ # @param [String] scope Optional parameter: Requested scopes as a
19
+ # space-delimited list.
20
+ # @param [Hash] _field_parameters Additional optional form parameters are
21
+ # supported by this endpoint.
22
+ # @return [OAuthToken] response from the API call
23
+ def request_token(authorization,
24
+ username,
25
+ password,
26
+ scope: nil,
27
+ _field_parameters: nil)
28
+ # Prepare query url.
29
+ _query_builder = config.get_base_uri
30
+ _query_builder << '/oauth/token'
31
+ _query_url = APIHelper.clean_url _query_builder
32
+
33
+ # Prepare headers.
34
+ _headers = {
35
+ 'accept' => 'application/json',
36
+ 'Authorization' => authorization
37
+ }
38
+
39
+ # Prepare form parameters.
40
+ _parameters = {
41
+ 'grant_type' => 'password',
42
+ 'username' => username,
43
+ 'password' => password,
44
+ 'scope' => scope
45
+ }
46
+ if !_parameters.nil? && !_field_parameters.nil?
47
+ _parameters.merge!(_field_parameters)
48
+ end
49
+ _parameters = APIHelper.form_encode_parameters(_parameters)
50
+
51
+ # Prepare and execute HttpRequest.
52
+ _request = config.http_client.post(
53
+ _query_url,
54
+ headers: _headers,
55
+ parameters: _parameters
56
+ )
57
+ _response = execute_request(_request)
58
+
59
+ # Validate response against endpoint and global error codes.
60
+ if _response.status_code == 400
61
+ raise OAuthProviderException.new(
62
+ 'OAuth 2 provider returned an error.',
63
+ _response
64
+ )
65
+ elsif _response.status_code == 401
66
+ raise OAuthProviderException.new(
67
+ 'OAuth 2 provider says client authentication failed.',
68
+ _response
69
+ )
70
+ end
71
+ validate_response(_response)
72
+
73
+ # Return appropriate response type.
74
+ decoded = APIHelper.json_deserialize(_response.raw_body)
75
+ OAuthToken.from_hash(decoded)
76
+ end
77
+
78
+ # Obtain a new access token using a refresh token
79
+ # @param [String] authorization Required parameter: Authorization header in
80
+ # Basic auth format
81
+ # @param [String] refresh_token Required parameter: Refresh token
82
+ # @param [String] scope Optional parameter: Requested scopes as a
83
+ # space-delimited list.
84
+ # @param [Hash] _field_parameters Additional optional form parameters are
85
+ # supported by this endpoint.
86
+ # @return [OAuthToken] response from the API call
87
+ def refresh_token(authorization,
88
+ refresh_token,
89
+ scope: nil,
90
+ _field_parameters: nil)
91
+ # Prepare query url.
92
+ _query_builder = config.get_base_uri
93
+ _query_builder << '/oauth/token'
94
+ _query_url = APIHelper.clean_url _query_builder
95
+
96
+ # Prepare headers.
97
+ _headers = {
98
+ 'accept' => 'application/json',
99
+ 'Authorization' => authorization
100
+ }
101
+
102
+ # Prepare form parameters.
103
+ _parameters = {
104
+ 'grant_type' => 'refresh_token',
105
+ 'refresh_token' => refresh_token,
106
+ 'scope' => scope
107
+ }
108
+ if !_parameters.nil? && !_field_parameters.nil?
109
+ _parameters.merge!(_field_parameters)
110
+ end
111
+ _parameters = APIHelper.form_encode_parameters(_parameters)
112
+
113
+ # Prepare and execute HttpRequest.
114
+ _request = config.http_client.post(
115
+ _query_url,
116
+ headers: _headers,
117
+ parameters: _parameters
118
+ )
119
+ _response = execute_request(_request)
120
+
121
+ # Validate response against endpoint and global error codes.
122
+ if _response.status_code == 400
123
+ raise OAuthProviderException.new(
124
+ 'OAuth 2 provider returned an error.',
125
+ _response
126
+ )
127
+ elsif _response.status_code == 401
128
+ raise OAuthProviderException.new(
129
+ 'OAuth 2 provider says client authentication failed.',
130
+ _response
131
+ )
132
+ end
133
+ validate_response(_response)
134
+
135
+ # Return appropriate response type.
136
+ decoded = APIHelper.json_deserialize(_response.raw_body)
137
+ OAuthToken.from_hash(decoded)
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,40 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # ServiceController
8
+ class ServiceController < BaseController
9
+ def initialize(config, http_call_back: nil)
10
+ super(config, http_call_back: http_call_back)
11
+ end
12
+
13
+ # TODO: type endpoint description here
14
+ # @return [ServiceStatus] response from the API call
15
+ def get_status
16
+ # Prepare query url.
17
+ _query_builder = config.get_base_uri
18
+ _query_builder << '/api/status'
19
+ _query_url = APIHelper.clean_url _query_builder
20
+
21
+ # Prepare headers.
22
+ _headers = {
23
+ 'accept' => 'application/json'
24
+ }
25
+
26
+ # Prepare and execute HttpRequest.
27
+ _request = config.http_client.get(
28
+ _query_url,
29
+ headers: _headers
30
+ )
31
+ OAuth2.apply(config, _request)
32
+ _response = execute_request(_request)
33
+ validate_response(_response)
34
+
35
+ # Return appropriate response type.
36
+ decoded = APIHelper.json_deserialize(_response.raw_body)
37
+ ServiceStatus.from_hash(decoded)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # UserController
8
+ class UserController < BaseController
9
+ def initialize(config, http_call_back: nil)
10
+ super(config, http_call_back: http_call_back)
11
+ end
12
+
13
+ # TODO: type endpoint description here
14
+ # @return [User] response from the API call
15
+ def get_user
16
+ # Prepare query url.
17
+ _query_builder = config.get_base_uri
18
+ _query_builder << '/api/user'
19
+ _query_url = APIHelper.clean_url _query_builder
20
+
21
+ # Prepare headers.
22
+ _headers = {
23
+ 'accept' => 'application/json'
24
+ }
25
+
26
+ # Prepare and execute HttpRequest.
27
+ _request = config.http_client.get(
28
+ _query_url,
29
+ headers: _headers
30
+ )
31
+ OAuth2.apply(config, _request)
32
+ _response = execute_request(_request)
33
+ validate_response(_response)
34
+
35
+ # Return appropriate response type.
36
+ decoded = APIHelper.json_deserialize(_response.raw_body)
37
+ User.from_hash(decoded)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
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,44 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MdNotes
7
+ # OAuth 2 Authorization endpoint exception.
8
+ class OAuthProviderException < APIException
9
+ # Gets or sets error code.
10
+ # @return [OAuthProviderErrorEnum]
11
+ attr_accessor :error
12
+
13
+ # Gets or sets human-readable text providing additional information on
14
+ # error.
15
+ # Used to assist the client developer in understanding the error that
16
+ # occurred.
17
+ # @return [String]
18
+ attr_accessor :error_description
19
+
20
+ # Gets or sets a URI identifying a human-readable web page with information
21
+ # about the error, used to provide the client developer with additional
22
+ # information about the error.
23
+ # @return [String]
24
+ attr_accessor :error_uri
25
+
26
+ # The constructor.
27
+ # @param [String] The reason for raising an exception.
28
+ # @param [HttpResponse] The HttpReponse of the API call.
29
+ def initialize(reason, response)
30
+ super(reason, response)
31
+ hash = APIHelper.json_deserialize(@response.raw_body)
32
+ unbox(hash)
33
+ end
34
+
35
+ # Populates this object by extracting properties from a hash.
36
+ # @param [Hash] The deserialized response sent by the server in the
37
+ # response body.
38
+ def unbox(hash)
39
+ @error = hash['error']
40
+ @error_description = hash['error_description']
41
+ @error_uri = hash['error_uri']
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,78 @@
1
+ # md_notes
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ require 'base64'
7
+
8
+ module MdNotes
9
+ # Utility class for OAuth 2 authorization and token management.
10
+ class OAuth2
11
+ def self.auth_controller(config)
12
+ OAuthAuthorizationController.new config
13
+ end
14
+
15
+ # Add OAuth2 authentication to the http request.
16
+ # @param [HttpRequest] The HttpRequest object to which authentication will
17
+ # be added.
18
+ def self.apply(config, http_request)
19
+ check_auth config.o_auth_token
20
+ token = config.o_auth_token.access_token
21
+ http_request.headers['Authorization'] = "Bearer #{token}"
22
+ end
23
+
24
+ # Get an OAuth token that you must then set in your Configuration object
25
+ # to authorize subsequent calls.
26
+ # @param [String | Array of String] Any scopes for the authentication token.
27
+ # @param [Hash] Any additional form parameters.
28
+ def self.authorize(config, scope: nil, additional_params: nil)
29
+ token = auth_controller(config).request_token(
30
+ build_basic_auth_header(config),
31
+ config.o_auth_username,
32
+ config.o_auth_password,
33
+ scope: scope ? Array(scope).compact.join(' ') : nil,
34
+ _field_parameters: additional_params
35
+ )
36
+ token.expiry = (Time.now.to_i + token.expires_in.to_i) if token.expires_in
37
+ token
38
+ end
39
+
40
+ # Builds the basic auth header for endpoints in the
41
+ # OAuth Authorization Controller.
42
+ def self.build_basic_auth_header(config)
43
+ value = "#{config.o_auth_client_id}:" \
44
+ "#{config.o_auth_client_secret}"
45
+ encoded = Base64.strict_encode64(value)
46
+ "Basic #{encoded}"
47
+ end
48
+
49
+ # Checks if OAuth token is valid.
50
+ def self.check_auth(token)
51
+ # Check if OAuth token exists.
52
+ if token.nil?
53
+ raise 'Client is not authorized. An OAuth token is needed to ' \
54
+ 'make API calls.'
55
+ # Check if OAuth token has expired.
56
+ elsif token.expiry && token.expiry < Time.now.to_i
57
+ raise 'The OAuth token has expired. Please refresh the existing token' \
58
+ ' or generate a new one.'
59
+ end
60
+ end
61
+
62
+ # Refreshes OAuth token and returns it. You must then set it in your
63
+ # Configuration object.
64
+ # @param [String | Array of String] Any scopes for the new authentication
65
+ # token.
66
+ # @param [Hash] Any additional form parameters.
67
+ def self.refresh_token(config, scope: nil, additional_params: nil)
68
+ token = auth_controller(config).refresh_token(
69
+ build_basic_auth_header(config),
70
+ config.o_auth_token.refresh_token,
71
+ scope: scope ? Array(scope).compact.join(' ') : nil,
72
+ _field_parameters: additional_params
73
+ )
74
+ token.expiry = (Time.now.to_i + token.expires_in.to_i) if token.expires_in
75
+ token
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,70 @@
1
+ # md_notes
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 MdNotes
10
+ # An implementation of HttpClient.
11
+ class FaradayClient < HttpClient
12
+ # The constructor.
13
+ def initialize(timeout:, max_retries:, retry_interval:,
14
+ backoff_factor:, retry_statuses:, retry_methods:,
15
+ cache: false, verify: true)
16
+ @connection = Faraday.new do |faraday|
17
+ faraday.use Faraday::HttpCache, serializer: Marshal if cache
18
+ faraday.use FaradayMiddleware::FollowRedirects
19
+ faraday.use :gzip
20
+ faraday.request :multipart
21
+ faraday.request :url_encoded
22
+ faraday.ssl[:ca_file] = Certifi.where
23
+ faraday.ssl[:verify] = verify
24
+ faraday.request :retry, max: max_retries, interval: retry_interval,
25
+ backoff_factor: backoff_factor,
26
+ retry_statuses: retry_statuses,
27
+ methods: retry_methods
28
+ faraday.adapter Faraday.default_adapter
29
+ faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
30
+ faraday.options[:timeout] = timeout if timeout > 0
31
+ end
32
+ end
33
+
34
+ # Method overridden from HttpClient.
35
+ def execute_as_string(http_request)
36
+ response = @connection.send(
37
+ http_request.http_method.downcase,
38
+ http_request.query_url
39
+ ) do |request|
40
+ request.headers = http_request.headers
41
+ unless http_request.http_method == HttpMethodEnum::GET &&
42
+ http_request.parameters.empty?
43
+ request.body = http_request.parameters
44
+ end
45
+ end
46
+ convert_response(response, http_request)
47
+ end
48
+
49
+ # Method overridden from HttpClient.
50
+ def execute_as_binary(http_request)
51
+ response = @connection.send(
52
+ http_request.http_method.downcase,
53
+ http_request.query_url
54
+ ) do |request|
55
+ request.headers = http_request.headers
56
+ unless http_request.http_method == HttpMethodEnum::GET &&
57
+ http_request.parameters.empty?
58
+ request.body = http_request.parameters
59
+ end
60
+ end
61
+ convert_response(response, http_request)
62
+ end
63
+
64
+ # Method overridden from HttpClient.
65
+ def convert_response(response, http_request)
66
+ HttpResponse.new(response.status, response.reason_phrase,
67
+ response.headers, response.body, http_request)
68
+ end
69
+ end
70
+ end