1- require 'rest_client '
1+ require 'faraday '
22require 'yajl'
33
44module Mailjet
55 class Connection
66
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
88 alias :read_only? :read_only
99
1010 def []( suburl , &new_block )
11- broken_url = url . split ( "/" )
11+ broken_url = uri . path . split ( "/" )
1212 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 )
1414 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 )
1616 end
1717 end
1818
1919 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
3223 self . public_operations = options [ :public_operations ] || [ ]
3324 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
3830 self . perform_api_call = options . key? ( :perform_api_call ) ? options [ :perform_api_call ] : true
3931 end
4032
@@ -54,34 +46,31 @@ def delete(additional_headers = {}, &block)
5446 handle_api_call ( :delete , additional_headers , &block )
5547 end
5648
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 )
6351 end
6452
65- def url
66- self . adapter . url
53+ def uri
54+ self . adapter . build_url
6755 end
6856
6957 private
7058
7159 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
7361 raise Mailjet ::MethodNotAllowed unless method_allowed ( method )
7462
7563 if self . perform_api_call
7664 if [ :get , :delete ] . include? ( method )
77- @adapter . send ( method , additional_headers , &block )
65+ @adapter . send ( method , nil , additional_headers [ :params ] , &block )
7866 else
79- @adapter . send ( method , formatted_payload , additional_headers , &block )
67+ @adapter . send ( method , nil , formatted_payload , additional_headers , &block )
8068 end
8169 else
8270 return Yajl ::Encoder . encode ( { 'Count' => 0 , 'Data' => [ mock_api_call : true ] , 'Total' => 0 } )
8371 end
84- rescue RestClient ::Exception => e
72+
73+ rescue Faraday ::Error => e
8574 handle_exception ( e , additional_headers , formatted_payload )
8675 end
8776
@@ -91,41 +80,41 @@ def method_allowed(method)
9180 end
9281
9382 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" )
9584
9685 params = additional_headers [ :params ] || { }
9786 formatted_payload = ( additional_headers [ :content_type ] == :json ) ? Yajl ::Parser . parse ( payload ) : payload
9887 params = params . merge! ( formatted_payload ) if formatted_payload . is_a? ( Hash )
9988
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
10291 else
10392 "{}"
10493 end
10594
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
10897 else
10998 raise communication_error ( e )
11099 end
111100 end
112101
113102 def communication_error ( e )
114103 if e . respond_to? ( :response ) && e . response
115- return case e . response . code
104+ return case e . response_status
116105 when Unauthorized ::CODE
117- Unauthorized . new ( e . message , e . response )
106+ Unauthorized . new ( e . message , e )
118107 when BadRequest ::CODE
119- BadRequest . new ( e . message , e . response )
108+ BadRequest . new ( e . message , e )
120109 else
121- CommunicationError . new ( e . message , e . response )
110+ CommunicationError . new ( e . message , e )
122111 end
123112 end
124113 CommunicationError . new ( e . message )
125114 end
126115
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' )
129118 return unless error_http_body
130119
131120 parsed_body = Yajl ::Parser . parse ( error_http_body )
@@ -138,6 +127,5 @@ def sent_invalid_email?(error_http_body, url)
138127 end
139128
140129 class MethodNotAllowed < StandardError
141-
142130 end
143131end
0 commit comments