3
3
require 'faraday_middleware'
4
4
5
5
module ShipEngine
6
- class PlatformClient
6
+ class InternalClient
7
7
attr_accessor :connection
8
8
9
9
def initialize ( api_key :, base_url : 'https://simengine.herokuapp.com/jsonrpc' , adapter : Faraday . default_adapter )
@@ -15,18 +15,17 @@ def initialize(api_key:, base_url: 'https://simengine.herokuapp.com/jsonrpc', ad
15
15
end
16
16
end
17
17
18
- def assert_no_platform_errors ( response )
19
- # puts response.inspect
20
- body = response . body
21
- error = body [ 'error' ] || { }
22
- data = error [ 'data' ]
23
- return unless data
24
- raise ShipEngine ::Exceptions ::ShipEngineErrorDetailed . new ( body [ 'id' ] , error [ 'message' ] , data ) unless data . nil?
18
+ def assert_shipengine_rpc_success ( body )
19
+ error , request_id = body . values_at ( 'error' , 'request_id' )
20
+ if error
21
+ message , data = error . values_at ( 'message' , 'data' )
22
+ source , type , code = data . values_at ( 'source' , 'type' , 'code' )
23
+ raise ShipEngine ::Exceptions ::ShipEngineErrorDetailed . new ( request_id , message , source , type , code )
25
24
end
26
25
end
27
26
28
27
# create jsonrpc request has
29
- def create_jsonrpc_request_body ( method , params )
28
+ def build_jsonrpc_request_body ( method , params )
30
29
{
31
30
jsonrpc : '2.0' ,
32
31
id : '123' ,
@@ -36,11 +35,13 @@ def create_jsonrpc_request_body(method, params)
36
35
end
37
36
38
37
def make_request ( method , params )
39
- response = @connection . send ( :post , nil , create_jsonrpc_request_body ( method , params ) )
40
- assert_no_platform_errors ( response )
41
- response . body
42
- # throw an error if status code is 400 or above.
43
- # Faraday does not throw errors for 400s -- only 500s!
38
+ response = @connection . send ( :post , nil , build_jsonrpc_request_body ( method , params ) )
39
+ body = response . body
40
+ assert_shipengine_rpc_success ( body )
41
+
42
+ body
43
+ # throw an error if status code is 400 or above.
44
+ # Faraday does not throw errors for 400s -- only 500s!
44
45
rescue Faraday ::Error => e
45
46
raise ShipEngine ::Exceptions ::ShipEngineError , e . message
46
47
end
0 commit comments