Skip to content

Commit bdd6a6c

Browse files
committed
feat: Add connection utilities for improved API connection handling
1 parent 0574320 commit bdd6a6c

File tree

4 files changed

+71
-40
lines changed

4 files changed

+71
-40
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ end
7878
# Initialize with your Paystack secret key
7979
paystack = PaystackSdk::Client.new(secret_key: "sk_test_xxx")
8080

81+
# Or set the PAYSTACK_SECRET_KEY in your environment and do this instead
82+
paystack = PaystackSdk::Client.new # => This will dynamically fetch the secret key
83+
8184
# You can access the connection directly if needed
8285
connection = paystack.connection
8386
```

lib/paystack_sdk/client.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
# frozen_string_literal: true
22

33
require_relative "resources/transactions"
4+
require_relative "utils/connection_utils"
45

56
module PaystackSdk
67
# The `Client` class serves as the main entry point for interacting with the Paystack API.
78
# It initializes a connection to the Paystack API and provides access to various resources.
89
class Client
9-
# The base URL for the Paystack API.
10-
BASE_URL = "https://api.paystack.co"
10+
# Include connection utilities
11+
include Utils::ConnectionUtils
1112

1213
# @return [Faraday::Connection] The Faraday connection object used for API requests
1314
attr_reader :connection
1415

1516
# Initializes a new `Client` instance.
1617
#
17-
# @param secret_key [String] The secret API key for authenticating with the Paystack API.
18+
# @param connection [Faraday::Connection, nil] The Faraday connection object used for API requests.
19+
# If nil, a new connection will be created using the default API key.
20+
# @param secret_key [String, nil] Optional API key to use for creating a new connection.
21+
# Only used if connection is nil.
1822
#
19-
# @example
23+
# @example With an existing connection
24+
# connection = Faraday.new(...)
25+
# client = PaystackSdk::Client.new(connection)
26+
#
27+
# @example With an API key
2028
# client = PaystackSdk::Client.new(secret_key: "sk_test_xxx")
21-
def initialize(secret_key:)
22-
@connection = Faraday.new(url: BASE_URL) do |conn|
23-
conn.request :json
24-
conn.response :json, content_type: /\bjson$/
25-
conn.headers["Authorization"] = "Bearer #{secret_key}"
26-
conn.headers["Content-Type"] = "application/json"
27-
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
28-
conn.adapter Faraday.default_adapter
29-
end
29+
#
30+
# @example With default connection (requires PAYSTACK_SECRET_KEY environment variable)
31+
# client = PaystackSdk::Client.new
32+
def initialize(connection = nil, secret_key: nil)
33+
@connection = initialize_connection(connection, secret_key: secret_key)
3034
end
3135

3236
# Provides access to the `Transactions` resource.

lib/paystack_sdk/resources/base.rb

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
require_relative "../response"
44
require_relative "../client"
55
require_relative "../validations"
6+
require_relative "../utils/connection_utils"
67

78
module PaystackSdk
89
module Resources
910
# The `Base` class serves as a parent class for all resource classes in the SDK.
1011
# It provides shared functionality, such as handling API responses.
1112
class Base
12-
# Include validation methods
1313
include PaystackSdk::Validations
14+
include PaystackSdk::Utils::ConnectionUtils
1415

1516
# Initializes a new `Base` instance.
1617
#
@@ -29,36 +30,11 @@ class Base
2930
# @example With default connection (requires PAYSTACK_SECRET_KEY environment variable)
3031
# resource = PaystackSdk::Resources::SomeResource.new
3132
def initialize(connection = nil, secret_key: nil)
32-
@connection = if connection
33-
connection
34-
elsif secret_key
35-
create_connection(secret_key:)
36-
else
37-
# Try to get API key from environment variable
38-
env_secret_key = ENV["PAYSTACK_SECRET_KEY"]
39-
raise PaystackSdk::Error, "No connection or API key provided" unless env_secret_key
40-
41-
create_connection(secret_key: env_secret_key)
42-
end
33+
@connection = initialize_connection(connection, secret_key: secret_key)
4334
end
4435

4536
private
4637

47-
# Creates a new Faraday connection with the Paystack API.
48-
#
49-
# @param secret_key [String] The secret API key for authenticating with the Paystack API.
50-
# @return [Faraday::Connection] A configured Faraday connection.
51-
def create_connection(secret_key:)
52-
Faraday.new(url: PaystackSdk::Client::BASE_URL) do |conn|
53-
conn.request :json
54-
conn.response :json, content_type: /\bjson$/
55-
conn.headers["Authorization"] = "Bearer #{secret_key}"
56-
conn.headers["Content-Type"] = "application/json"
57-
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
58-
conn.adapter Faraday.default_adapter
59-
end
60-
end
61-
6238
# Handles the API response, wrapping it in a Response object.
6339
#
6440
# @param response [Faraday::Response] The response object returned by the Faraday connection.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module PaystackSdk
4+
module Utils
5+
# The `ConnectionUtils` module provides shared functionality for creating
6+
# and initializing API connections. This is used by both the Client class
7+
# and resource classes.
8+
module ConnectionUtils
9+
# The base URL for the Paystack API.
10+
BASE_URL = "https://api.paystack.co"
11+
12+
# Initializes a connection based on the provided parameters.
13+
#
14+
# @param connection [Faraday::Connection, nil] An existing connection object.
15+
# @param secret_key [String, nil] Optional API key to use for creating a new connection.
16+
# @return [Faraday::Connection] A connection object for API requests.
17+
# @raise [PaystackSdk::Error] If no connection or API key can be found.
18+
def initialize_connection(connection = nil, secret_key: nil)
19+
if connection
20+
connection
21+
elsif secret_key
22+
create_connection(secret_key:)
23+
else
24+
# Try to get API key from environment variable
25+
env_secret_key = ENV["PAYSTACK_SECRET_KEY"]
26+
raise PaystackSdk::Error, "No connection or API key provided" unless env_secret_key
27+
28+
create_connection(secret_key: env_secret_key)
29+
end
30+
end
31+
32+
# Creates a new Faraday connection with the Paystack API.
33+
#
34+
# @param secret_key [String] The secret API key for authenticating with the Paystack API.
35+
# @return [Faraday::Connection] A configured Faraday connection.
36+
def create_connection(secret_key:)
37+
Faraday.new(url: BASE_URL) do |conn|
38+
conn.request :json
39+
conn.response :json, content_type: /\bjson$/
40+
conn.headers["Authorization"] = "Bearer #{secret_key}"
41+
conn.headers["Content-Type"] = "application/json"
42+
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
43+
conn.adapter Faraday.default_adapter
44+
end
45+
end
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)