celitech 2.0.7

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.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +225 -0
  3. data/lib/celitech/client.rb +81 -0
  4. data/lib/celitech/config.rb +48 -0
  5. data/lib/celitech/environment.rb +7 -0
  6. data/lib/celitech/error.rb +50 -0
  7. data/lib/celitech/http/connection.rb +321 -0
  8. data/lib/celitech/http/hooks.rb +17 -0
  9. data/lib/celitech/http/oauth_token_manager.rb +76 -0
  10. data/lib/celitech/http/response.rb +35 -0
  11. data/lib/celitech/models/bad_request.rb +47 -0
  12. data/lib/celitech/models/create_purchase_ok_response.rb +63 -0
  13. data/lib/celitech/models/create_purchase_ok_response_profile.rb +75 -0
  14. data/lib/celitech/models/create_purchase_ok_response_purchase.rb +111 -0
  15. data/lib/celitech/models/create_purchase_request.rb +155 -0
  16. data/lib/celitech/models/create_purchase_request_language.rb +14 -0
  17. data/lib/celitech/models/create_purchase_v2_ok_response.rb +63 -0
  18. data/lib/celitech/models/create_purchase_v2_ok_response_profile.rb +96 -0
  19. data/lib/celitech/models/create_purchase_v2_ok_response_purchase.rb +70 -0
  20. data/lib/celitech/models/create_purchase_v2_request.rb +156 -0
  21. data/lib/celitech/models/create_purchase_v2_request_language.rb +14 -0
  22. data/lib/celitech/models/destinations.rb +80 -0
  23. data/lib/celitech/models/device.rb +80 -0
  24. data/lib/celitech/models/edit_purchase_ok_response.rb +90 -0
  25. data/lib/celitech/models/edit_purchase_request.rb +90 -0
  26. data/lib/celitech/models/get_esim_device_ok_response.rb +52 -0
  27. data/lib/celitech/models/get_esim_history_ok_response.rb +52 -0
  28. data/lib/celitech/models/get_esim_history_ok_response_esim.rb +61 -0
  29. data/lib/celitech/models/get_esim_ok_response.rb +52 -0
  30. data/lib/celitech/models/get_esim_ok_response_esim.rb +116 -0
  31. data/lib/celitech/models/get_purchase_consumption_ok_response.rb +70 -0
  32. data/lib/celitech/models/grant_type.rb +10 -0
  33. data/lib/celitech/models/history.rb +70 -0
  34. data/lib/celitech/models/list_destinations_ok_response.rb +51 -0
  35. data/lib/celitech/models/list_packages_ok_response.rb +64 -0
  36. data/lib/celitech/models/list_purchases_ok_response.rb +64 -0
  37. data/lib/celitech/models/o_auth_token_request.rb +84 -0
  38. data/lib/celitech/models/o_auth_token_response.rb +60 -0
  39. data/lib/celitech/models/open_model.rb +30 -0
  40. data/lib/celitech/models/package.rb +111 -0
  41. data/lib/celitech/models/packages.rb +121 -0
  42. data/lib/celitech/models/purchases.rb +178 -0
  43. data/lib/celitech/models/purchases_esim.rb +51 -0
  44. data/lib/celitech/models/serializable.rb +63 -0
  45. data/lib/celitech/models/token_ok_response.rb +47 -0
  46. data/lib/celitech/models/top_up_esim_ok_response.rb +63 -0
  47. data/lib/celitech/models/top_up_esim_ok_response_profile.rb +51 -0
  48. data/lib/celitech/models/top_up_esim_ok_response_purchase.rb +111 -0
  49. data/lib/celitech/models/top_up_esim_request.rb +145 -0
  50. data/lib/celitech/models/unauthorized.rb +47 -0
  51. data/lib/celitech/models/union_type.rb +12 -0
  52. data/lib/celitech/serializers/form_serializer.rb +41 -0
  53. data/lib/celitech/serializers/json_serializer.rb +41 -0
  54. data/lib/celitech/services/destinations.rb +59 -0
  55. data/lib/celitech/services/e_sim.rb +108 -0
  56. data/lib/celitech/services/i_frame.rb +59 -0
  57. data/lib/celitech/services/o_auth.rb +49 -0
  58. data/lib/celitech/services/packages.rb +82 -0
  59. data/lib/celitech/services/purchases.rb +195 -0
  60. data/lib/celitech/validator.rb +42 -0
  61. data/lib/celitech/version.rb +5 -0
  62. data/lib/celitech.rb +64 -0
  63. metadata +162 -0
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ module Models
5
+ class TopUpEsimRequest
6
+ include ::Celitech::Models::Serializable
7
+ include ::Celitech::Models::OpenModel
8
+
9
+ def self.wire_keys
10
+ {
11
+ iccid: 'iccid',
12
+ data_limit_in_gb: 'dataLimitInGB',
13
+ start_date: 'startDate',
14
+ end_date: 'endDate',
15
+ duration: 'duration',
16
+ email: 'email',
17
+ reference_id: 'referenceId',
18
+ email_brand: 'emailBrand',
19
+ start_time: 'startTime',
20
+ end_time: 'endTime',
21
+ }.freeze
22
+ end
23
+
24
+ def iccid
25
+ val = @iccid
26
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
27
+ end
28
+
29
+ def data_limit_in_gb
30
+ val = @data_limit_in_gb
31
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
32
+ end
33
+
34
+ def start_date
35
+ val = @start_date
36
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
37
+ end
38
+
39
+ def end_date
40
+ val = @end_date
41
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
42
+ end
43
+
44
+ def duration
45
+ val = @duration
46
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
47
+ end
48
+
49
+ def email
50
+ val = @email
51
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
52
+ end
53
+
54
+ def reference_id
55
+ val = @reference_id
56
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
57
+ end
58
+
59
+ def email_brand
60
+ val = @email_brand
61
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
62
+ end
63
+
64
+ def start_time
65
+ val = @start_time
66
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
67
+ end
68
+
69
+ def end_time
70
+ val = @end_time
71
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
72
+ end
73
+
74
+ attr_reader :additional_properties
75
+
76
+ def initialize(
77
+ iccid: ::Celitech::Models::UNSET,
78
+ data_limit_in_gb: ::Celitech::Models::UNSET,
79
+ start_date: ::Celitech::Models::UNSET,
80
+ end_date: ::Celitech::Models::UNSET,
81
+ duration: ::Celitech::Models::UNSET,
82
+ email: ::Celitech::Models::UNSET,
83
+ reference_id: ::Celitech::Models::UNSET,
84
+ email_brand: ::Celitech::Models::UNSET,
85
+ start_time: ::Celitech::Models::UNSET,
86
+ end_time: ::Celitech::Models::UNSET,
87
+ additional_properties: {}
88
+ )
89
+ @iccid = iccid
90
+ @data_limit_in_gb = data_limit_in_gb
91
+ @start_date = start_date
92
+ @end_date = end_date
93
+ @duration = duration
94
+ @email = email
95
+ @reference_id = reference_id
96
+ @email_brand = email_brand
97
+ @start_time = start_time
98
+ @end_time = end_time
99
+ @additional_properties = additional_properties
100
+ end
101
+
102
+ def self.from_hash(hash)
103
+ return unless hash
104
+
105
+ new(
106
+ iccid: hash.fetch('iccid', ::Celitech::Models::UNSET),
107
+ data_limit_in_gb: hash.fetch('dataLimitInGB', ::Celitech::Models::UNSET),
108
+ start_date: hash.fetch('startDate', ::Celitech::Models::UNSET),
109
+ end_date: hash.fetch('endDate', ::Celitech::Models::UNSET),
110
+ duration: hash.fetch('duration', ::Celitech::Models::UNSET),
111
+ email: hash.fetch('email', ::Celitech::Models::UNSET),
112
+ reference_id: hash.fetch('referenceId', ::Celitech::Models::UNSET),
113
+ email_brand: hash.fetch('emailBrand', ::Celitech::Models::UNSET),
114
+ start_time: hash.fetch('startTime', ::Celitech::Models::UNSET),
115
+ end_time: hash.fetch('endTime', ::Celitech::Models::UNSET),
116
+ additional_properties:
117
+ hash.except('iccid', 'dataLimitInGB', 'startDate', 'endDate', 'duration', 'email', 'referenceId', 'emailBrand', 'startTime', 'endTime'),
118
+ )
119
+ end
120
+
121
+ def attributes
122
+ {
123
+ iccid: @iccid,
124
+ data_limit_in_gb: @data_limit_in_gb,
125
+ start_date: @start_date,
126
+ end_date: @end_date,
127
+ duration: @duration,
128
+ email: @email,
129
+ reference_id: @reference_id,
130
+ email_brand: @email_brand,
131
+ start_time: @start_time,
132
+ end_time: @end_time,
133
+ }
134
+ end
135
+
136
+ def validate!
137
+ Validator.validate_string!(@iccid, 'iccid', min_length: 18, max_length: 22) unless @iccid.equal?(::Celitech::Models::UNSET)
138
+ end
139
+
140
+ def open_model_extras
141
+ @additional_properties
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ module Models
5
+ class Unauthorized
6
+ include ::Celitech::Models::Serializable
7
+ include ::Celitech::Models::OpenModel
8
+
9
+ def self.wire_keys
10
+ {
11
+ message: 'message',
12
+ }.freeze
13
+ end
14
+
15
+ def message
16
+ val = @message
17
+ val.equal?(::Celitech::Models::UNSET) ? nil : val
18
+ end
19
+
20
+ attr_reader :additional_properties
21
+
22
+ def initialize(message: ::Celitech::Models::UNSET, additional_properties: {})
23
+ @message = message
24
+ @additional_properties = additional_properties
25
+ end
26
+
27
+ def self.from_hash(hash)
28
+ return unless hash
29
+
30
+ new(
31
+ message: hash.fetch('message', ::Celitech::Models::UNSET),
32
+ additional_properties: hash.except('message'),
33
+ )
34
+ end
35
+
36
+ def attributes
37
+ {
38
+ message: @message,
39
+ }
40
+ end
41
+
42
+ def open_model_extras
43
+ @additional_properties
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ module Models
5
+ # Marker module for oneOf/anyOf union wrapper classes.
6
+ #
7
+ # Including classes hold one typed variant in +#value+.
8
+ # Serializers check for this module to delegate serialization to the wrapped value.
9
+ module UnionType
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module Celitech
6
+ module Serializers
7
+ # Serializes models or hashes to form-encoded and multipart bodies.
8
+ module Form
9
+ # Encodes a model or hash as application/x-www-form-urlencoded.
10
+ def self.to_urlencoded(value)
11
+ URI.encode_www_form(flatten(value))
12
+ end
13
+
14
+ # Configures a Net::HTTP request for multipart/form-data.
15
+ def self.set_multipart(request, value)
16
+ request.set_form(
17
+ flatten(value).map { |k, v| [k.to_s, v.to_s] },
18
+ 'multipart/form-data',
19
+ )
20
+ end
21
+
22
+ private_class_method def self.flatten(value)
23
+ value = value.value if value.is_a?(Models::UnionType)
24
+ if value.is_a?(Models::Serializable)
25
+ declared = value.class.wire_keys.filter_map do |attr, key|
26
+ val = value.attributes[attr]
27
+ # Form encoding cannot represent null; both UNSET and explicit nil are omitted.
28
+ # Unlike JSON (which serializes nil as null), form bodies have no null concept.
29
+ [key, val] unless val.nil? || val.equal?(Models::UNSET)
30
+ end
31
+ extras = value.is_a?(Models::OpenModel) ? value.open_model_extras.compact.to_a : []
32
+ declared + extras
33
+ elsif value.is_a?(Hash)
34
+ value.compact.to_a
35
+ else
36
+ [['body', value.to_s]]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Celitech
6
+ module Serializers
7
+ # Serializes models to JSON-ready hashes and deserializes hashes back to models.
8
+ module Json
9
+ # Converts a model, array, or primitive to a plain Ruby hash/value
10
+ # suitable for JSON.generate / .to_json.
11
+ def self.serialize(value)
12
+ return value.map { |item| serialize(item) } if value.is_a?(Array)
13
+ # Typed maps (additionalProperties): serialize values, keeping the dynamic string keys.
14
+ return value.transform_values { |item| serialize(item) } if value.is_a?(Hash)
15
+ return serialize(value.value) if value.is_a?(Models::UnionType)
16
+ return serialize_model(value) if value.is_a?(Models::Serializable)
17
+
18
+ value
19
+ end
20
+
21
+ # Builds a model instance from a plain hash (already JSON.parsed).
22
+ # Returns the raw data unchanged when no model class is provided.
23
+ def self.deserialize(data, model_class = nil)
24
+ return data unless model_class
25
+
26
+ model_class.from_hash(data)
27
+ end
28
+
29
+ private_class_method def self.serialize_model(model)
30
+ result = model.class.wire_keys.each_with_object({}) do |(attr, key), hash|
31
+ val = model.attributes[attr]
32
+ next if val.equal?(Models::UNSET)
33
+
34
+ hash[key] = serialize(val)
35
+ end
36
+ model.open_model_extras.each { |k, v| result[k] = serialize(v) unless result.key?(k) } if model.is_a?(Models::OpenModel)
37
+ result
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ class Destinations
5
+ NO_OVERRIDE = Object.new.freeze
6
+ private_constant :NO_OVERRIDE
7
+
8
+ def initialize(connection, config = nil, token_manager: nil)
9
+ @connection = connection
10
+ @config = config
11
+ @token_manager = token_manager
12
+ end
13
+
14
+ # List Destinations
15
+ def list_destinations(config: NO_OVERRIDE)
16
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
17
+ conn = resolve_connection(config)
18
+ response = conn.get('/destinations', {}, headers)
19
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
20
+ result = ::Celitech::Models::ListDestinationsOkResponse.from_hash(response.body)
21
+ result&.validate! if resolve_config(config)&.enable_response_validation
22
+ result
23
+ rescue APIError => e
24
+ case e.status
25
+ when 400
26
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
27
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
28
+ when 401
29
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
30
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
31
+ end
32
+ raise
33
+ end
34
+
35
+ private
36
+
37
+ def config_headers(_override_config)
38
+ return {} unless @token_manager
39
+
40
+ @token_manager.headers
41
+ end
42
+
43
+ def resolve_connection(override_config)
44
+ cfg = override_config.equal?(NO_OVERRIDE) ? @config : override_config
45
+ return @connection unless cfg&.base_url || cfg&.timeout || cfg&.retry_config
46
+
47
+ HTTP::Connection.new(
48
+ cfg.base_url || @connection.base_url,
49
+ @connection.default_headers,
50
+ timeout: cfg.timeout || @connection.timeout,
51
+ retry_config: cfg.retry_config || @connection.retry_config,
52
+ )
53
+ end
54
+
55
+ def resolve_config(override_config)
56
+ override_config.equal?(NO_OVERRIDE) ? @config : override_config
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ class ESim
5
+ NO_OVERRIDE = Object.new.freeze
6
+ private_constant :NO_OVERRIDE
7
+
8
+ def initialize(connection, config = nil, token_manager: nil)
9
+ @connection = connection
10
+ @config = config
11
+ @token_manager = token_manager
12
+ end
13
+
14
+ # Get eSIM
15
+ def get_esim(iccid:, config: NO_OVERRIDE)
16
+ query_params = {
17
+ 'iccid' => iccid,
18
+ }.compact
19
+ query_params = query_params.merge(resolve_config(config)&.additional_query_parameters || {})
20
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
21
+ Validator.validate_string!(iccid, 'iccid', min_length: 18, max_length: 22)
22
+ conn = resolve_connection(config)
23
+ response = conn.get('/esim', query_params, headers)
24
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
25
+ result = ::Celitech::Models::GetEsimOkResponse.from_hash(response.body)
26
+ result&.validate! if resolve_config(config)&.enable_response_validation
27
+ result
28
+ rescue APIError => e
29
+ case e.status
30
+ when 400
31
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
32
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
33
+ when 401
34
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
35
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
36
+ end
37
+ raise
38
+ end
39
+
40
+ # Get eSIM Device
41
+ def get_esim_device(iccid:, config: NO_OVERRIDE)
42
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
43
+ Validator.validate_string!(iccid, 'iccid', min_length: 18, max_length: 22)
44
+ conn = resolve_connection(config)
45
+ response = conn.get("/esim/#{iccid}/device", {}, headers)
46
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
47
+ result = ::Celitech::Models::GetEsimDeviceOkResponse.from_hash(response.body)
48
+ result&.validate! if resolve_config(config)&.enable_response_validation
49
+ result
50
+ rescue APIError => e
51
+ case e.status
52
+ when 400
53
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
54
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
55
+ when 401
56
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
57
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
58
+ end
59
+ raise
60
+ end
61
+
62
+ # Get eSIM History
63
+ def get_esim_history(iccid:, config: NO_OVERRIDE)
64
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
65
+ Validator.validate_string!(iccid, 'iccid', min_length: 18, max_length: 22)
66
+ conn = resolve_connection(config)
67
+ response = conn.get("/esim/#{iccid}/history", {}, headers)
68
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
69
+ result = ::Celitech::Models::GetEsimHistoryOkResponse.from_hash(response.body)
70
+ result&.validate! if resolve_config(config)&.enable_response_validation
71
+ result
72
+ rescue APIError => e
73
+ case e.status
74
+ when 400
75
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
76
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
77
+ when 401
78
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
79
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
80
+ end
81
+ raise
82
+ end
83
+
84
+ private
85
+
86
+ def config_headers(_override_config)
87
+ return {} unless @token_manager
88
+
89
+ @token_manager.headers
90
+ end
91
+
92
+ def resolve_connection(override_config)
93
+ cfg = override_config.equal?(NO_OVERRIDE) ? @config : override_config
94
+ return @connection unless cfg&.base_url || cfg&.timeout || cfg&.retry_config
95
+
96
+ HTTP::Connection.new(
97
+ cfg.base_url || @connection.base_url,
98
+ @connection.default_headers,
99
+ timeout: cfg.timeout || @connection.timeout,
100
+ retry_config: cfg.retry_config || @connection.retry_config,
101
+ )
102
+ end
103
+
104
+ def resolve_config(override_config)
105
+ override_config.equal?(NO_OVERRIDE) ? @config : override_config
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ class IFrame
5
+ NO_OVERRIDE = Object.new.freeze
6
+ private_constant :NO_OVERRIDE
7
+
8
+ def initialize(connection, config = nil, token_manager: nil)
9
+ @connection = connection
10
+ @config = config
11
+ @token_manager = token_manager
12
+ end
13
+
14
+ # Generate Token
15
+ def token(config: NO_OVERRIDE)
16
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
17
+ conn = resolve_connection(config)
18
+ response = conn.post('/iframe/token', nil, headers: headers)
19
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
20
+ result = ::Celitech::Models::TokenOkResponse.from_hash(response.body)
21
+ result&.validate! if resolve_config(config)&.enable_response_validation
22
+ result
23
+ rescue APIError => e
24
+ case e.status
25
+ when 400
26
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
27
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
28
+ when 401
29
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
30
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
31
+ end
32
+ raise
33
+ end
34
+
35
+ private
36
+
37
+ def config_headers(_override_config)
38
+ return {} unless @token_manager
39
+
40
+ @token_manager.headers
41
+ end
42
+
43
+ def resolve_connection(override_config)
44
+ cfg = override_config.equal?(NO_OVERRIDE) ? @config : override_config
45
+ return @connection unless cfg&.base_url || cfg&.timeout || cfg&.retry_config
46
+
47
+ HTTP::Connection.new(
48
+ cfg.base_url || @connection.base_url,
49
+ @connection.default_headers,
50
+ timeout: cfg.timeout || @connection.timeout,
51
+ retry_config: cfg.retry_config || @connection.retry_config,
52
+ )
53
+ end
54
+
55
+ def resolve_config(override_config)
56
+ override_config.equal?(NO_OVERRIDE) ? @config : override_config
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ class OAuth
5
+ NO_OVERRIDE = Object.new.freeze
6
+ private_constant :NO_OVERRIDE
7
+
8
+ def initialize(connection, config = nil, token_manager: nil)
9
+ @connection = connection
10
+ @config = config
11
+ @token_manager = token_manager
12
+ end
13
+
14
+ def get_access_token(body:, config: NO_OVERRIDE)
15
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
16
+ body.validate! if body.respond_to?(:validate!)
17
+ conn = resolve_connection(config)
18
+ response = conn.post('/oauth2/token', body, content_type: 'application/x-www-form-urlencoded', headers: headers)
19
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
20
+ result = ::Celitech::Models::OAuthTokenResponse.from_hash(response.body)
21
+ result&.validate! if resolve_config(config)&.enable_response_validation
22
+ result
23
+ end
24
+
25
+ private
26
+
27
+ def config_headers(_override_config)
28
+ return {} unless @token_manager
29
+
30
+ @token_manager.headers
31
+ end
32
+
33
+ def resolve_connection(override_config)
34
+ cfg = override_config.equal?(NO_OVERRIDE) ? @config : override_config
35
+ return @connection unless cfg&.base_url || cfg&.timeout || cfg&.retry_config
36
+
37
+ HTTP::Connection.new(
38
+ cfg.base_url || @connection.base_url,
39
+ @connection.default_headers,
40
+ timeout: cfg.timeout || @connection.timeout,
41
+ retry_config: cfg.retry_config || @connection.retry_config,
42
+ )
43
+ end
44
+
45
+ def resolve_config(override_config)
46
+ override_config.equal?(NO_OVERRIDE) ? @config : override_config
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Celitech
4
+ class Packages
5
+ NO_OVERRIDE = Object.new.freeze
6
+ private_constant :NO_OVERRIDE
7
+
8
+ def initialize(connection, config = nil, token_manager: nil)
9
+ @connection = connection
10
+ @config = config
11
+ @token_manager = token_manager
12
+ end
13
+
14
+ # List Packages
15
+ def list_packages(
16
+ destination: nil,
17
+ data_limit_in_gb: nil,
18
+ start_date: nil,
19
+ end_date: nil,
20
+ after_cursor: nil,
21
+ limit: nil,
22
+ start_time: nil,
23
+ end_time: nil,
24
+ include_unlimited: nil,
25
+ config: NO_OVERRIDE
26
+ )
27
+ query_params = {
28
+ 'destination' => destination,
29
+ 'dataLimitInGB' => data_limit_in_gb,
30
+ 'startDate' => start_date,
31
+ 'endDate' => end_date,
32
+ 'afterCursor' => after_cursor,
33
+ 'limit' => limit,
34
+ 'startTime' => start_time,
35
+ 'endTime' => end_time,
36
+ 'includeUnlimited' => include_unlimited,
37
+ }.compact
38
+ query_params = query_params.merge(resolve_config(config)&.additional_query_parameters || {})
39
+ headers = config_headers(config).merge(resolve_config(config)&.additional_headers || {})
40
+ conn = resolve_connection(config)
41
+ response = conn.get('/packages', query_params, headers)
42
+ return response if !config.equal?(NO_OVERRIDE) && config&.return_raw
43
+ result = ::Celitech::Models::ListPackagesOkResponse.from_hash(response.body)
44
+ result&.validate! if resolve_config(config)&.enable_response_validation
45
+ result
46
+ rescue APIError => e
47
+ case e.status
48
+ when 400
49
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::BadRequest.from_hash(e.body) : nil
50
+ raise ::Celitech::BadRequestError.new(e.status, parsed, e.body, e.headers)
51
+ when 401
52
+ parsed = e.body.is_a?(Hash) ? ::Celitech::Models::Unauthorized.from_hash(e.body) : nil
53
+ raise ::Celitech::UnauthorizedError.new(e.status, parsed, e.body, e.headers)
54
+ end
55
+ raise
56
+ end
57
+
58
+ private
59
+
60
+ def config_headers(_override_config)
61
+ return {} unless @token_manager
62
+
63
+ @token_manager.headers
64
+ end
65
+
66
+ def resolve_connection(override_config)
67
+ cfg = override_config.equal?(NO_OVERRIDE) ? @config : override_config
68
+ return @connection unless cfg&.base_url || cfg&.timeout || cfg&.retry_config
69
+
70
+ HTTP::Connection.new(
71
+ cfg.base_url || @connection.base_url,
72
+ @connection.default_headers,
73
+ timeout: cfg.timeout || @connection.timeout,
74
+ retry_config: cfg.retry_config || @connection.retry_config,
75
+ )
76
+ end
77
+
78
+ def resolve_config(override_config)
79
+ override_config.equal?(NO_OVERRIDE) ? @config : override_config
80
+ end
81
+ end
82
+ end