Added json support to UserAgent, replaced faraday with UserAgent.

This commit is contained in:
Martin Edenhofer 2015-03-23 01:31:30 +01:00
parent 8abe250de7
commit 15577498b0
8 changed files with 174 additions and 73 deletions

12
Gemfile
View file

@ -58,15 +58,6 @@ gem 'net-ldap'
gem 'writeexcel' gem 'writeexcel'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
# event machine # event machine
gem 'eventmachine' gem 'eventmachine'
gem 'em-websocket' gem 'em-websocket'
@ -86,9 +77,6 @@ group :development, :test do
# UI tests w/ Selenium # UI tests w/ Selenium
gem 'selenium-webdriver' gem 'selenium-webdriver'
# rest client lib for unit tests
gem 'faraday'
# livereload on template changes (html, js, css) # livereload on template changes (html, js, css)
gem 'guard', '>= 2.2.2', :require => false gem 'guard', '>= 2.2.2', :require => false
gem 'guard-livereload', :require => false gem 'guard-livereload', :require => false

View file

@ -117,6 +117,7 @@ add a avatar
# fetch image # fetch image
response = UserAgent.get( response = UserAgent.get(
data[:url], data[:url],
{},
{ {
:open_timeout => 4, :open_timeout => 4,
:read_timeout => 6, :read_timeout => 6,

View file

@ -17,7 +17,9 @@ class GeoIp::ZammadGeoIp
begin begin
response = UserAgent.get( response = UserAgent.get(
"#{host}#{url}", "#{host}#{url}",
{},
{ {
:json => true,
:open_timeout => 2, :open_timeout => 2,
:read_timeout => 4, :read_timeout => 4,
}, },
@ -26,7 +28,7 @@ class GeoIp::ZammadGeoIp
raise "ERROR: #{response.code.to_s}/#{response.body}" raise "ERROR: #{response.code.to_s}/#{response.body}"
end end
data = JSON.parse( response.body ) data = response.data
# compat. map # compat. map
if data && data['country_code2'] if data && data['country_code2']

View file

@ -55,6 +55,7 @@ module Import::OTRS2
puts 'GET: ' + url puts 'GET: ' + url
response = UserAgent.get( response = UserAgent.get(
url, url,
{},
{ {
:open_timeout => 6, :open_timeout => 6,
:read_timeout => 60, :read_timeout => 60,

View file

@ -43,18 +43,23 @@ create/update/delete index
return SearchIndexBackend.remove( data[:name] ) return SearchIndexBackend.remove( data[:name] )
end end
puts "# curl -X PUT \"#{url}\" -d '#{data[:data].to_json}'" puts "# curl -X PUT \"#{url}\" \\"
#puts "-d '#{data[:data].to_json}'"
conn = connection( url ) response = UserAgent.put(
response = conn.put do |req| url,
req.url url data[:data],
req.headers['Content-Type'] = 'application/json' {
req.body = data[:data].to_json :json => true,
end :open_timeout => 5,
puts "# #{response.status.to_s}" :read_timeout => 14,
:user => Setting.get('es_user'),
:password => Setting.get('es_password'),
}
)
puts "# #{response.code.to_s}"
return true if response.success? return true if response.success?
data = JSON.parse( response.body ) raise response.body
raise data.inspect
end end
=begin =begin
@ -70,18 +75,23 @@ add new object to search index
url = build_url( type, data['id'] ) url = build_url( type, data['id'] )
return if !url return if !url
puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'" puts "# curl -X POST \"#{url}\" \\"
#puts "-d '#{data.to_json}'"
conn = connection( url ) response = UserAgent.post(
response = conn.post do |req| url,
req.url url data,
req.headers['Content-Type'] = 'application/json' {
req.body = data.to_json :json => true,
end :open_timeout => 5,
puts "# #{response.status.to_s}" :read_timeout => 14,
:user => Setting.get('es_user'),
:password => Setting.get('es_password'),
}
)
puts "# #{response.code.to_s}"
return true if response.success? return true if response.success?
data = JSON.parse( response.body ) raise response.body
raise data.inspect
end end
=begin =begin
@ -100,13 +110,18 @@ remove whole data from index
puts "# curl -X DELETE \"#{url}\"" puts "# curl -X DELETE \"#{url}\""
conn = connection( url ) response = UserAgent.delete(
response = conn.delete( url ) url,
puts "# #{response.status.to_s}" {
return false if !response.success? :open_timeout => 5,
data = JSON.parse( response.body ) :read_timeout => 14,
# raise data.inspect :user => Setting.get('es_user'),
return { :data => data, :response => response } :password => Setting.get('es_password'),
}
)
#puts "# #{response.code.to_s}"
return true if response.success?
#raise response.body
end end
=begin =begin
@ -177,19 +192,27 @@ return search result
} }
data['query']['bool']['must'].push condition data['query']['bool']['must'].push condition
puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'" puts "# curl -X POST \"#{url}\" \\"
#puts " -d'#{data.to_json}'"
conn = connection( url ) response = UserAgent.get(
response = conn.get do |req| url,
req.headers['Content-Type'] = 'application/json' data,
req.body = data.to_json {
end :json => true,
puts "# #{response.status.to_s}" :open_timeout => 5,
data = JSON.parse( response.body ) :read_timeout => 14,
:user => Setting.get('es_user'),
:password => Setting.get('es_password'),
}
)
puts "# #{response.code.to_s}"
if !response.success? if !response.success?
return [] return []
# raise data.inspect # raise data.inspect
end end
data = response.data
ids = [] ids = []
return ids if !data return ids if !data
@ -239,14 +262,4 @@ return true if backend is configured
url url
end end
def self.connection( url )
conn = Faraday.new( :url => url, :request => { :open_timeout => 5, :timeout => 10 } )
user = Setting.get('es_user')
pw = Setting.get('es_password')
if user && !user.empty? && pw && !pw.empty?
conn.basic_auth( user, pw )
end
conn
end
end end

View file

@ -15,6 +15,9 @@ get http/https calls
result = UserAgent.get( result = UserAgent.get(
'http://host/some_dir/some_file?param1=123', 'http://host/some_dir/some_file?param1=123',
{
:param1 => 'some value',
},
{ {
:open_timeout => 2, :open_timeout => 2,
:read_timeout => 4, :read_timeout => 4,
@ -27,7 +30,7 @@ returns
=end =end
def self.get(url, options = {}, count = 10) def self.get(url, params = {}, options = {}, count = 10)
uri = URI.parse(url) uri = URI.parse(url)
http = get_http(uri, options) http = get_http(uri, options)
@ -37,10 +40,13 @@ returns
# http basic auth (if needed) # http basic auth (if needed)
request = set_basic_auth(request, options) request = set_basic_auth(request, options)
# set params
request = set_params(request, params, options)
# start http call # start http call
begin begin
response = http.request(request) response = http.request(request)
return process(response, uri, count, options) return process(response, uri, count, params, options)
rescue Exception => e rescue Exception => e
return Result.new( return Result.new(
:error => e.inspect, :error => e.inspect,
@ -80,7 +86,7 @@ returns
request = Net::HTTP::Post.new( uri, {'User-Agent' => 'Zammad User Agent'} ) request = Net::HTTP::Post.new( uri, {'User-Agent' => 'Zammad User Agent'} )
# set params # set params
request.set_form_data( params ) request = set_params(request, params, options)
# http basic auth (if needed) # http basic auth (if needed)
request = set_basic_auth(request, options) request = set_basic_auth(request, options)
@ -88,7 +94,7 @@ returns
# start http call # start http call
begin begin
response = http.request(request) response = http.request(request)
return process(response, uri, count, options) return process(response, uri, count, params, options)
rescue Exception => e rescue Exception => e
return Result.new( return Result.new(
:error => e.inspect, :error => e.inspect,
@ -128,7 +134,7 @@ returns
request = Net::HTTP::Put.new( uri, {'User-Agent' => 'Zammad User Agent'} ) request = Net::HTTP::Put.new( uri, {'User-Agent' => 'Zammad User Agent'} )
# set params # set params
request.set_form_data( params ) request = set_params(request, params, options)
# http basic auth (if needed) # http basic auth (if needed)
request = set_basic_auth(request, options) request = set_basic_auth(request, options)
@ -136,7 +142,7 @@ returns
# start http call # start http call
begin begin
response = http.request(request) response = http.request(request)
return process(response, uri, count, options) return process(response, uri, count, params, options)
rescue Exception => e rescue Exception => e
return Result.new( return Result.new(
:error => e.inspect, :error => e.inspect,
@ -177,7 +183,7 @@ returns
# start http call # start http call
begin begin
response = http.request(request) response = http.request(request)
return process(response, uri, count, options) return process(response, uri, count, {}, options)
rescue Exception => e rescue Exception => e
return Result.new( return Result.new(
:error => e.inspect, :error => e.inspect,
@ -219,7 +225,7 @@ returns
when /ftp/ when /ftp/
ftp(uri, options) ftp(uri, options)
when /http|https/ when /http|https/
get( url, options ) get( url, {}, options )
end end
end end
@ -249,7 +255,20 @@ returns
request request
end end
def self.process(response, uri, count, options = {}) def self.set_params(request, params, options)
if options[:json]
request.add_field("Content-Type", "application/json")
if !params.empty?
request.body = params.to_json
end
else
if !params.empty?
request.set_form_data( params )
end
end
request
end
def self.process(response, uri, count, params, options)
if !response if !response
return Result.new( return Result.new(
:error => "Can't connect to #{uri.to_s}, got no response!", :error => "Can't connect to #{uri.to_s}, got no response!",
@ -281,10 +300,15 @@ returns
when Net::HTTPRedirection when Net::HTTPRedirection
raise "Too many redirections for the original URL, halting." if count <= 0 raise "Too many redirections for the original URL, halting." if count <= 0
url = response['location'] url = response['location']
return get(url, options, count - 1) return get(url, params, options, count - 1)
when Net::HTTPOK when Net::HTTPOK
data = nil
if options[:json] && response.body
data = JSON.parse( response.body )
end
return Result.new( return Result.new(
:data => data,
:body => response.body, :body => response.body,
:content_type => response['Content-Type'], :content_type => response['Content-Type'],
:success => true, :success => true,
@ -344,6 +368,7 @@ returns
def initialize(options) def initialize(options)
@success = options[:success] @success = options[:success]
@body = options[:body] @body = options[:body]
@data = options[:data]
@code = options[:code] @code = options[:code]
@content_type = options[:content_type] @content_type = options[:content_type]
@error = options[:error] @error = options[:error]
@ -357,6 +382,9 @@ returns
def body def body
@body @body
end end
def data
@data
end
def code def code
@code @code
end end

View file

@ -18,6 +18,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"get"/) assert(result.body =~ /"get"/)
assert(result.body =~ /"123"/) assert(result.body =~ /"123"/)
assert(result.body =~ /"text\/plain"/)
# get / 404 # get / 404
result = UserAgent.get( result = UserAgent.get(
@ -41,6 +42,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"post"/) assert(result.body =~ /"post"/)
assert(result.body =~ /"some value"/) assert(result.body =~ /"some value"/)
assert(result.body =~ /"application\/x-www-form-urlencoded"/)
# post / 404 # post / 404
result = UserAgent.post( result = UserAgent.post(
@ -67,6 +69,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"put"/) assert(result.body =~ /"put"/)
assert(result.body =~ /"some value"/) assert(result.body =~ /"some value"/)
assert(result.body =~ /"application\/x-www-form-urlencoded"/)
# put / 404 # put / 404
result = UserAgent.put( result = UserAgent.put(
@ -89,6 +92,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal('200', result.code) assert_equal('200', result.code)
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"delete"/) assert(result.body =~ /"delete"/)
assert(result.body =~ /"text\/plain"/)
# delete / 404 # delete / 404
result = UserAgent.delete( result = UserAgent.delete(
@ -106,6 +110,7 @@ class UserAgentTest < ActiveSupport::TestCase
# get / 200 # get / 200
result = UserAgent.get( result = UserAgent.get(
"#{host}/test_basic_auth/get/1?submitted=123", "#{host}/test_basic_auth/get/1?submitted=123",
{},
{ {
:user => 'basic_auth_user', :user => 'basic_auth_user',
:password => 'test123', :password => 'test123',
@ -117,10 +122,12 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"get"/) assert(result.body =~ /"get"/)
assert(result.body =~ /"123"/) assert(result.body =~ /"123"/)
assert(result.body =~ /"text\/plain"/)
# get / 401 # get / 401
result = UserAgent.get( result = UserAgent.get(
"#{host}/test_basic_auth/get/1?submitted=123", "#{host}/test_basic_auth/get/1?submitted=123",
{},
{ {
:user => 'basic_auth_user_not_existing', :user => 'basic_auth_user_not_existing',
:password => 'test<>123', :password => 'test<>123',
@ -148,6 +155,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"post"/) assert(result.body =~ /"post"/)
assert(result.body =~ /"some value"/) assert(result.body =~ /"some value"/)
assert(result.body =~ /"application\/x-www-form-urlencoded"/)
# post / 401 # post / 401
result = UserAgent.post( result = UserAgent.post(
@ -182,6 +190,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"put"/) assert(result.body =~ /"put"/)
assert(result.body =~ /"some value"/) assert(result.body =~ /"some value"/)
assert(result.body =~ /"application\/x-www-form-urlencoded"/)
# put / 401 # put / 401
result = UserAgent.put( result = UserAgent.put(
@ -213,6 +222,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal('200', result.code) assert_equal('200', result.code)
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"delete"/) assert(result.body =~ /"delete"/)
assert(result.body =~ /"text\/plain"/)
# delete / 401 # delete / 401
result = UserAgent.delete( result = UserAgent.delete(
@ -241,6 +251,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"get"/) assert(result.body =~ /"get"/)
assert(result.body =~ /"abc"/) assert(result.body =~ /"abc"/)
assert(result.body =~ /"text\/plain"/)
# get / 301 # get / 301
@ -257,6 +268,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"get"/) assert(result.body =~ /"get"/)
assert(result.body =~ /"abc"/) assert(result.body =~ /"abc"/)
assert(result.body =~ /"text\/plain"/)
# get / 401 # get / 401
@ -286,6 +298,7 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(String, result.body.class) assert_equal(String, result.body.class)
assert(result.body =~ /"get"/) assert(result.body =~ /"get"/)
assert(result.body =~ /"123"/) assert(result.body =~ /"123"/)
assert(result.body =~ /"text\/plain"/)
# ftp / 200 # ftp / 200
result = UserAgent.request( result = UserAgent.request(
@ -378,6 +391,7 @@ class UserAgentTest < ActiveSupport::TestCase
# get / timeout # get / timeout
result = UserAgent.get( result = UserAgent.get(
"#{host}/test/get/3?submitted=123", "#{host}/test/get/3?submitted=123",
{},
{ {
:open_timeout => 1, :open_timeout => 1,
:read_timeout => 1, :read_timeout => 1,
@ -405,4 +419,60 @@ class UserAgentTest < ActiveSupport::TestCase
assert_equal(NilClass, result.body.class) assert_equal(NilClass, result.body.class)
end end
# check
test 'check json' do
# get / 200
result = UserAgent.get(
"#{host}/test/get/1",
{
:submitted => { :key => 'some value ' }
},
{
:json => true,
}
)
assert(result)
assert_equal(true, result.success?)
assert_equal('200', result.code)
assert_equal(String, result.body.class)
assert(result.body =~ /"content_type_requested"/)
assert(result.body =~ /"application\/json"/)
assert_equal('some value ', result.data['submitted']['key'])
# get / 401
result = UserAgent.get(
"#{host}/test/not_existing",
{
:submitted => { :key => 'some value ' }
},
{
:json => true,
}
)
assert(result)
assert_equal(false, result.success?)
assert_equal('404', result.code)
assert_equal(NilClass, result.body.class)
assert(!result.data)
# post / 200
result = UserAgent.post(
"#{host}/test/post/1",
{
:submitted => { :key => 'some value ' }
},
{
:json => true,
}
)
assert(result)
assert_equal(true, result.success?)
assert_equal('200', result.code)
assert_equal(String, result.body.class)
assert(result.body =~ /"content_type_requested"/)
assert(result.body =~ /"application\/json"/)
assert_equal('some value ', result.data['submitted']['key'])
end
end end

View file

@ -231,7 +231,9 @@ class RestTest < ActiveSupport::TestCase
response = UserAgent.get( response = UserAgent.get(
"#{ENV['BROWSER_URL']}#{url}", "#{ENV['BROWSER_URL']}#{url}",
{},
{ {
:json => true,
:user => user, :user => user,
:password => pw, :password => pw,
} }
@ -239,11 +241,7 @@ class RestTest < ActiveSupport::TestCase
#puts 'URL: ' + url #puts 'URL: ' + url
#puts response.code.to_s #puts response.code.to_s
#puts response.body.to_s #puts response.body.to_s
data = nil return { :data => response.data, :response => response }
if response.body
data = JSON.parse( response.body )
end
return { :data => data, :response => response }
end end
end end