Skip to content

Commit 8ef8257

Browse files
committed
RUBYAPI-40 add #reso_api
1 parent 03d44a0 commit 8ef8257

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

lib/spark_api.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def self.client(opts={})
3939
Thread.current[:spark_api_client] ||= SparkApi::Client.new(opts)
4040
end
4141

42+
def self.reso_client(opts={})
43+
opts = opts.dup
44+
opts[:version] = "Reso/OData"
45+
opts[:middleware] = :reso_api
46+
47+
Thread.current[:reso_api_client] ||= SparkApi::Client.new(opts)
48+
end
49+
4250
def self.method_missing(method, *args, &block)
4351
return super unless (client.respond_to?(method))
4452
client.send(method, *args, &block)
@@ -47,6 +55,7 @@ def self.method_missing(method, *args, &block)
4755
def self.reset
4856
reset_configuration
4957
Thread.current[:spark_api_client] = nil
58+
Thread.current[:reso_api_client] = nil
5059
end
5160

5261
end

lib/spark_api/authentication/api_auth.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def authenticate
1919
sig = sign("#{@client.api_secret}ApiKey#{@client.api_key}")
2020
SparkApi.logger.debug { "Authenticating to #{@client.endpoint}" }
2121
start_time = Time.now
22-
request_path = "/#{@client.version}/session?ApiKey=#{@client.api_key}&ApiSig=#{sig}"
22+
request_path = "#{SparkApi::Configuration::DEFAULT_SESSION_PATH}?ApiKey=#{@client.api_key}&ApiSig=#{sig}"
2323
resp = @client.connection(true).post request_path, ""
2424
request_time = Time.now - start_time
2525
SparkApi.logger.info { "[#{(request_time * 1000).to_i}ms] Api: POST #{request_path}" }

lib/spark_api/configuration.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Configuration
1111
# valid configuration options
1212
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
1313
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14-
:auth_endpoint, :callback, :compress, :timeout].freeze
14+
:auth_endpoint, :callback, :compress, :timeout, :middleware].freeze
1515
OAUTH2_KEYS = [:authorization_uri, :access_uri, :client_id, :client_secret,
1616
# Requirements for authorization_code grant type
1717
:redirect_uri,
@@ -36,12 +36,14 @@ module Configuration
3636
DEFAULT_AUTHORIZATION_URI = 'https://sparkplatform.com/oauth2'
3737
DEFAULT_VERSION = 'v1'
3838
DEFAULT_ACCESS_URI = "#{DEFAULT_ENDPOINT}/#{DEFAULT_VERSION}/oauth2/grant"
39+
DEFAULT_SESSION_PATH = "/#{DEFAULT_VERSION}/session"
3940
DEFAULT_USER_AGENT = "Spark API Ruby Gem #{VERSION}"
4041
DEFAULT_SSL = true
4142
DEFAULT_SSL_VERIFY = true
4243
DEFAULT_OAUTH2 = nil
4344
DEFAULT_COMPRESS = false
4445
DEFAULT_TIMEOUT = 5 # seconds
46+
DEFAULT_MIDDLEWARE = :spark_api
4547

4648
X_SPARK_API_USER_AGENT = "X-SparkApi-User-Agent"
4749

@@ -75,6 +77,7 @@ def reset_configuration
7577
self.version = DEFAULT_VERSION
7678
self.compress = DEFAULT_COMPRESS
7779
self.timeout = DEFAULT_TIMEOUT
80+
self.middleware = DEFAULT_MIDDLEWARE
7881
self
7982
end
8083
end

lib/spark_api/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def connection(force_ssl = false)
2323
end
2424

2525
conn = Faraday.new(opts) do |conn|
26-
conn.response :spark_api
26+
conn.response self.middleware
2727
conn.options[:timeout] = self.timeout
2828
conn.adapter Faraday.default_adapter
2929
end

lib/spark_api/faraday_middleware.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ def decompress_body(env)
8686
end
8787

8888
end
89-
Faraday::Response.register_middleware :spark_api => FaradayMiddleware
9089

90+
class ResoFaradayMiddleware < FaradayMiddleware
91+
92+
def on_complete(env)
93+
94+
body = decompress_body(env)
95+
body = MultiJson.decode(body)
96+
97+
if body["D"]
98+
super(env)
99+
return
100+
end
101+
102+
env[:body] = body
103+
end
104+
105+
end
106+
107+
Faraday::Response.register_middleware :spark_api => FaradayMiddleware
108+
Faraday::Response.register_middleware :reso_api => ResoFaradayMiddleware
91109
end

0 commit comments

Comments
 (0)