Skip to content

Commit

Permalink
refactor: add modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackfaded committed Aug 29, 2024
1 parent 003f366 commit 21dd701
Show file tree
Hide file tree
Showing 142 changed files with 21,395 additions and 20,860 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,44 @@ gem install PCP-server-Ruby-SDK

### General

To use this SDK you need to construct a `CommunicatorConfiguration` which encapsulate everything needed to connect to the PAYONE Commerce Platform.
To use this SDK you need to construct a `PCPServerSDK::CommunicatorConfiguration` which encapsulate everything needed to connect to the PAYONE Commerce Platform.

```rb
require 'pcp-server-ruby-sdk'

api_key = ENV['API_KEY']
api_secret = ENV['API_SECRET']

communicator_configuration = CommunicatorConfiguration.new(
communicator_configuration = PCPServerSDK::CommunicatorConfiguration.new(
api_key,
api_secret,
'https://api.preprod.commerce.payone.com'
)
```

With the configuration you can create an API client for each reource you want to interact with. For example to create a commerce case you can use the `CommerceCaseApiClient`.
With the configuration you can create an API client for each reource you want to interact with. For example to create a commerce case you can use the `PCPServerSDK::Endpoints::`.

```rb
require 'pcp-server-ruby-sdk'

client = CommerceCaseApiClient.new(communicator_configuration)
client = PCPServerSDK::Endpoints::.new(communicator_configuration)
```

All payloads and reponses are availabe as ruby classes within the `pcp-server-ruby-sdk` package. The serialization and deserialization is handled by the SDK internally. For example, to create an empty commerce case you can pass a `CreateCommerceCaseRequest` instance:
All payloads and reponses are availabe as ruby classes within the `pcp-server-ruby-sdk` package. The serialization and deserialization is handled by the SDK internally. For example, to create an empty commerce case you can pass a `PCPServerSDK::Models::CreateCommerceCaseRequest` instance:

```rb
createCommerceCaseRequest = CreateCommerceCaseRequest.new
createCommerceCaseRequest = PCPServerSDK::Models::CreateCommerceCaseRequest.new
createCommerceCaseResponse = client.create_commerce_case_request('merchant_id', createCommerceCaseRequest);
```

The models directly map to the API as described in [PAYONE Commerce Platform API Reference](https://docs.payone.com/pcp/commerce-platform-api). For an in depth example you can take a look at the [demo app](#demo-app).

### Error Handling

When making a request any client may throw a `ApiException`. There two subtypes of this exception:
When making a request any client may throw a `PCPServerSDK::Errors::ApiException`. There two subtypes of this exception:

- `ApiErrorReponseException`: This exception is thrown when the API returns an well-formed error response. The given errors are deserialized into `APIError` objects which are availble via the `get_errors` method on the exception. They usually contain useful information about what is wrong in your request or the state of the resource.
- `ApiResponseRetrievalException`: This exception is a catch-all exception for any error that cannot be turned into a helpful error response. This includes malformed responses or unknown responses.
- `PCPServerSDK::Errors::ApiErrorReponseException`: This exception is thrown when the API returns an well-formed error response. The given errors are deserialized into `PCPServerSDK::Models::APIError` objects which are availble via the `get_errors` method on the exception. They usually contain useful information about what is wrong in your request or the state of the resource.
- `PCPServerSDK::Errors::ApiResponseRetrievalException`: This exception is a catch-all exception for any error that cannot be turned into a helpful error response. This includes malformed responses or unknown responses.

Network errors are not wrap, you can should handle the standard `IOExeption`.

Expand Down
4 changes: 4 additions & 0 deletions lib/PCP-server-Ruby-SDK/communicator_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module PCPServerSDK
class CommunicatorConfiguration
attr_reader :api_key, :api_secret, :host

# The constructor
# @param [String] api_key
# @param [String] api_secret
# @param [String] host
def initialize(api_key, api_secret, host)
@api_key = api_key
@api_secret = api_secret
Expand Down
22 changes: 15 additions & 7 deletions lib/PCP-server-Ruby-SDK/endpoints/base_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,37 @@ def get_config
@config
end

# Make an API call
# @param [String] url
# @param [Hash] request_init
def make_api_call(url, request_init)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == 'https'

headers = @request_header_generator.generate_additional_request_headers(url, request_init || {})
modified_request = @request_header_generator.generate_additional_request_headers(url, request_init || {})

request = build_http_request(uri, request_init, headers)
request = build_http_request(uri, modified_request)
response = http.request(request)

body = response.body

begin
parsed = JSON.parse(body)
rescue StandardError => e
raise ApiResponseRetrievalException.new(response.code.to_i, parsed, e)
raise PCPServerSDK::Errors::ApiResponseRetrievalException.new(response.code.to_i, parsed, e)
end

unless response.is_a?(Net::HTTPSuccess)
if is_error_response(body)
raise ApiErrorResponseException.new(response.code.to_i, body, parsed['errors'] || [])
begin
parsedError = deserialize_json(parsed, PCPServerSDK::Models::ErrorResponse)
raise PCPServerSDK::Errors::ApiErrorResponseException.new(response.code.to_i, body, parsedError.errors)
rescue StandardError => e
raise PCPServerSDK::Errors::ApiResponseRetrievalException.new(response.code.to_i, parsed, e)
end
else
raise ApiResponseRetrievalException.new(response.code.to_i, body)
raise PCPServerSDK::Errors::ApiResponseRetrievalException.new(response.code.to_i, body)
end
end

Expand All @@ -67,7 +75,7 @@ def deserialize_json(json, klass)

private

def build_http_request(uri, request_init, headers)
def build_http_request(uri, request_init)
method = request_init[:method].to_s.upcase

case method
Expand All @@ -85,7 +93,7 @@ def build_http_request(uri, request_init, headers)
raise ArgumentError, "Unsupported HTTP method: #{method}"
end

headers[:headers].each do |key, value|
request_init[:headers].each do |key, value|
req[key] = value
end

Expand Down
18 changes: 9 additions & 9 deletions lib/PCP-server-Ruby-SDK/endpoints/checkout_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def initialize(config)
# Create a checkout
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param payload [CreateCheckoutRequest] The checkout request
# @return [CreateCheckoutResponse] The checkout response
# @param payload [PCPServerSDK::Models::CreateCheckoutRequest] The checkout request
# @return [PCPServerSDK::Models::CreateCheckoutResponse] The checkout response
def create_checkout_request(merchant_id, commerce_case_id, payload)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?
raise TypeError, COMMERCE_CASE_ID_REQUIRED_ERROR if commerce_case_id.nil? || commerce_case_id.empty?
Expand All @@ -33,14 +33,14 @@ def create_checkout_request(merchant_id, commerce_case_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CreateCheckoutResponse)
deserialize_json(response, PCPServerSDK::Models::CreateCheckoutResponse)
end

# Get a checkout
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @return [CheckoutResponse] The checkout response
# @return [PCPServerSDK::Models::CheckoutResponse] The checkout response
def get_checkout_request(merchant_id, commerce_case_id, checkout_id)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?
raise TypeError, COMMERCE_CASE_ID_REQUIRED_ERROR if commerce_case_id.nil? || commerce_case_id.empty?
Expand All @@ -54,13 +54,13 @@ def get_checkout_request(merchant_id, commerce_case_id, checkout_id)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CheckoutResponse)
deserialize_json(response, PCPServerSDK::Models::CheckoutResponse)
end

# Get checkouts
# @param merchant_id [String] The merchant identifier
# @param query_params [GetCheckoutsQuery] The query parameters
# @return [CheckoutsResponse] The checkouts response
# @param query_params [PCPServerSDK::Models::GetCheckoutsQuery] The query parameters
# @return [PCPServerSDK::Models::CheckoutsResponse] The checkouts response
def get_checkouts_request(merchant_id, query_params = nil)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?

Expand All @@ -76,14 +76,14 @@ def get_checkouts_request(merchant_id, query_params = nil)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CheckoutsResponse)
deserialize_json(response, PCPServerSDK::Models::CheckoutsResponse)
end

# Update a checkout
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @param payload [UpdateCheckoutRequest] The checkout request
# @param payload [PCPServerSDK::Models::UpdateCheckoutRequest] The checkout request
# @return [nil]
def update_checkout_request(merchant_id, commerce_case_id, checkout_id, payload)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?
Expand Down
18 changes: 9 additions & 9 deletions lib/PCP-server-Ruby-SDK/endpoints/commerce_case_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def initialize(config)

# Create a commerce case
# @param merchant_id [String] The merchant identifier
# @param payload [CreateCommerceCaseRequest] The commerce case request
# @return [CreateCommerceCaseResponse] The commerce case response
# @param payload [PCPServerSDK::Models::CreateCommerceCaseRequest] The commerce case request
# @return [PCPServerSDK::Models::CreateCommerceCaseResponse] The commerce case response
def create_commerce_case_request(merchant_id, payload)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?

Expand All @@ -30,13 +30,13 @@ def create_commerce_case_request(merchant_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CreateCommerceCaseResponse)
deserialize_json(response, PCPServerSDK::Models::CreateCommerceCaseResponse)
end

# Get a commerce case
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @return [CommerceCaseResponse] The commerce case response
# @return [PCPServerSDK::Models::CommerceCaseResponse] The commerce case response
def get_commerce_case_request(merchant_id, commerce_case_id)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?
raise TypeError, COMMERCE_CASE_ID_REQUIRED_ERROR if commerce_case_id.nil? || commerce_case_id.empty?
Expand All @@ -49,13 +49,13 @@ def get_commerce_case_request(merchant_id, commerce_case_id)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CommerceCaseResponse)
deserialize_json(response, PCPServerSDK::Models::CommerceCaseResponse)
end

# Get commerce cases
# @param merchant_id [String] The merchant identifier
# @param query_params [GetCommerceCasesQuery] The query parameters
# @return [Array<CommerceCaseResponse>] The commerce cases
# @param query_params [PCPServerSDK::Models::GetCommerceCasesQuery] The query parameters
# @return [Array<PCPServerSDK::Models::CommerceCaseResponse>] The commerce cases
def get_commerce_cases_request(merchant_id, query_params = nil)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?

Expand All @@ -71,13 +71,13 @@ def get_commerce_cases_request(merchant_id, query_params = nil)
}

request = make_api_call(url.to_s, request_init)
request.map { |r| deserialize_json(r, CommerceCaseResponse) }
request.map { |r| deserialize_json(r, PCPServerSDK::Models::CommerceCaseResponse) }
end

# Update a commerce case
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param customer [Customer] The customer
# @param customer [PCPServerSDK::Models::Customer] The customer
# @return [nil]
def update_commerce_case_request(merchant_id, commerce_case_id, customer)
raise TypeError, MERCHANT_ID_REQUIRED_ERROR if merchant_id.nil? || merchant_id.empty?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def initialize(config)
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @param payload [OrderRequest] The order request
# @return [OrderResponse] The order response
# @param payload [PCPServerSDK::Models::OrderRequest] The order request
# @return [PCPServerSDK::Models::OrderResponse] The order response
def create_order(merchant_id, commerce_case_id, checkout_id, payload)
validate_ids(merchant_id, commerce_case_id, checkout_id)

Expand All @@ -32,15 +32,15 @@ def create_order(merchant_id, commerce_case_id, checkout_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, OrderResponse)
deserialize_json(response, PCPServerSDK::Models::OrderResponse)
end

# Deliver an order
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @param payload [DeliverRequest] The deliver request
# @return [DeliverResponse] The deliver response
# @param payload [PCPServerSDK::Models::DeliverRequest] The deliver request
# @return [PCPServerSDK::Models::DeliverResponse] The deliver response
def deliver_order(merchant_id, commerce_case_id, checkout_id, payload)
validate_ids(merchant_id, commerce_case_id, checkout_id)

Expand All @@ -53,15 +53,15 @@ def deliver_order(merchant_id, commerce_case_id, checkout_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, DeliverResponse)
deserialize_json(response, PCPServerSDK::Models::DeliverResponse)
end

# Return an order
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @param payload [ReturnRequest] The return request
# @return [ReturnResponse] The return response
# @param payload [PCPServerSDK::Models::ReturnRequest] The return request
# @return [PCPServerSDK::Models::ReturnResponse] The return response
def return_order(merchant_id, commerce_case_id, checkout_id, payload)
validate_ids(merchant_id, commerce_case_id, checkout_id)

Expand All @@ -74,15 +74,15 @@ def return_order(merchant_id, commerce_case_id, checkout_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, ReturnResponse)
deserialize_json(response, PCPServerSDK::Models::ReturnResponse)
end

# Cancel an order
# @param merchant_id [String] The merchant identifier
# @param commerce_case_id [String] The commerce case identifier
# @param checkout_id [String] The checkout identifier
# @param payload [CancelRequest] The cancel request
# @return [CancelResponse] The cancel response
# @param payload [PCPServerSDK::Models::CancelRequest] The cancel request
# @return [PCPServerSDK::Models::CancelResponse] The cancel response
def cancel_order(merchant_id, commerce_case_id, checkout_id, payload)
validate_ids(merchant_id, commerce_case_id, checkout_id)

Expand All @@ -95,7 +95,7 @@ def cancel_order(merchant_id, commerce_case_id, checkout_id, payload)
}

response = make_api_call(url.to_s, request_init)
deserialize_json(response, CancelResponse)
deserialize_json(response, PCPServerSDK::Models::CancelResponse)
end

private
Expand Down
Loading

0 comments on commit 21dd701

Please sign in to comment.