1
- require 'rest_client '
1
+ require 'faraday '
2
2
require 'yajl'
3
3
4
4
module Mailjet
5
5
class Connection
6
6
7
- attr_accessor :adapter , :public_operations , :read_only , :perform_api_call , :read_timeout , :open_timeout
7
+ attr_accessor :adapter , :public_operations , :read_only , :perform_api_call , :api_key , :secret_key , :options
8
8
alias :read_only? :read_only
9
9
10
10
def []( suburl , &new_block )
11
- broken_url = url . split ( "/" )
11
+ broken_url = uri . path . split ( "/" )
12
12
if broken_url . include? ( "contactslist" ) && broken_url . include? ( "managemanycontacts" ) && broken_url . last . to_i > 0
13
- self . class . new ( url , options [ :user ] , options [ :password ] , options )
13
+ self . class . new ( uri , api_key , secret_key , options )
14
14
else
15
- self . class . new ( concat_urls ( url , suburl ) , options [ :user ] , options [ :password ] , options )
15
+ self . class . new ( concat_urls ( suburl ) , api_key , secret_key , options )
16
16
end
17
17
end
18
18
19
19
def initialize ( end_point , api_key , secret_key , options = { } )
20
- # #charles proxy
21
- # RestClient.proxy = "http://127.0.0.1:8888"
22
- # #
23
- # #Output for debugging
24
- # RestClient.log =
25
- # Object.new.tap do |proxy|
26
- # def proxy.<<(message)
27
- # Rails.logger.info message
28
- # end
29
- # end
30
- # #
31
- adapter_class = options [ :adapter_class ] || RestClient ::Resource
20
+ self . options = options
21
+ self . api_key = api_key
22
+ self . secret_key = secret_key
32
23
self . public_operations = options [ :public_operations ] || [ ]
33
24
self . read_only = options [ :read_only ]
34
- self . read_timeout = options [ :read_timeout ]
35
- self . open_timeout = options [ :open_timeout ]
36
- # self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, :verify_ssl => false, content_type: 'application/json'))
37
- self . adapter = adapter_class . new ( end_point , options . merge ( user : api_key , password : secret_key , content_type : 'application/json' , read_timeout : self . read_timeout , open_timeout : self . open_timeout ) )
25
+ self . adapter = Faraday . new ( end_point , ssl : { verify : false } ) do |conn |
26
+ conn . response :raise_error , include_request : true
27
+ conn . request :authorization , :basic , api_key , secret_key
28
+ conn . headers [ 'Content-Type' ] = 'application/json'
29
+ end
38
30
self . perform_api_call = options . key? ( :perform_api_call ) ? options [ :perform_api_call ] : true
39
31
end
40
32
@@ -54,34 +46,31 @@ def delete(additional_headers = {}, &block)
54
46
handle_api_call ( :delete , additional_headers , &block )
55
47
end
56
48
57
- def options
58
- self . adapter . options
59
- end
60
-
61
- def concat_urls ( *options )
62
- self . adapter . concat_urls ( *options )
49
+ def concat_urls ( suburl )
50
+ self . adapter . build_url ( suburl . to_s )
63
51
end
64
52
65
- def url
66
- self . adapter . url
53
+ def uri
54
+ self . adapter . build_url
67
55
end
68
56
69
57
private
70
58
71
59
def handle_api_call ( method , additional_headers = { } , payload = { } , &block )
72
- formatted_payload = ( additional_headers [ :content_type ] == : json) ? Yajl ::Encoder . encode ( payload ) : payload
60
+ formatted_payload = ( additional_headers [ "Content-Type" ] == 'application/ json' ) ? Yajl ::Encoder . encode ( payload ) : payload
73
61
raise Mailjet ::MethodNotAllowed unless method_allowed ( method )
74
62
75
63
if self . perform_api_call
76
64
if [ :get , :delete ] . include? ( method )
77
- @adapter . send ( method , additional_headers , &block )
65
+ @adapter . send ( method , nil , additional_headers [ :params ] , &block )
78
66
else
79
- @adapter . send ( method , formatted_payload , additional_headers , &block )
67
+ @adapter . send ( method , nil , formatted_payload , additional_headers , &block )
80
68
end
81
69
else
82
70
return Yajl ::Encoder . encode ( { 'Count' => 0 , 'Data' => [ mock_api_call : true ] , 'Total' => 0 } )
83
71
end
84
- rescue RestClient ::Exception => e
72
+
73
+ rescue Faraday ::Error => e
85
74
handle_exception ( e , additional_headers , formatted_payload )
86
75
end
87
76
@@ -91,41 +80,41 @@ def method_allowed(method)
91
80
end
92
81
93
82
def handle_exception ( e , additional_headers , payload = { } )
94
- return e . http_body if e . http_headers [ :content_type ] . include? ( "text/plain" )
83
+ return e . response_body if e . response_headers [ :content_type ] . include? ( "text/plain" )
95
84
96
85
params = additional_headers [ :params ] || { }
97
86
formatted_payload = ( additional_headers [ :content_type ] == :json ) ? Yajl ::Parser . parse ( payload ) : payload
98
87
params = params . merge! ( formatted_payload ) if formatted_payload . is_a? ( Hash )
99
88
100
- http_body = if e . http_headers [ :content_type ] . include? ( "application/json" )
101
- e . http_body
89
+ response_body = if e . response_headers [ :content_type ] . include? ( "application/json" )
90
+ e . response_body
102
91
else
103
92
"{}"
104
93
end
105
94
106
- if sent_invalid_email? ( e . http_body , @adapter . url )
107
- return e . http_body
95
+ if sent_invalid_email? ( e . response_body , @adapter . build_url )
96
+ return e . response_body
108
97
else
109
98
raise communication_error ( e )
110
99
end
111
100
end
112
101
113
102
def communication_error ( e )
114
103
if e . respond_to? ( :response ) && e . response
115
- return case e . response . code
104
+ return case e . response_status
116
105
when Unauthorized ::CODE
117
- Unauthorized . new ( e . message , e . response )
106
+ Unauthorized . new ( e . message , e )
118
107
when BadRequest ::CODE
119
- BadRequest . new ( e . message , e . response )
108
+ BadRequest . new ( e . message , e )
120
109
else
121
- CommunicationError . new ( e . message , e . response )
110
+ CommunicationError . new ( e . message , e )
122
111
end
123
112
end
124
113
CommunicationError . new ( e . message )
125
114
end
126
115
127
- def sent_invalid_email? ( error_http_body , url )
128
- return false unless url . include? ( 'v3.1/send' )
116
+ def sent_invalid_email? ( error_http_body , uri )
117
+ return false unless uri . path . include? ( 'v3.1/send' )
129
118
return unless error_http_body
130
119
131
120
parsed_body = Yajl ::Parser . parse ( error_http_body )
@@ -138,6 +127,5 @@ def sent_invalid_email?(error_http_body, url)
138
127
end
139
128
140
129
class MethodNotAllowed < StandardError
141
-
142
130
end
143
131
end
0 commit comments