lib/ppc/baidu.rb in ppc-1.0.6 vs lib/ppc/baidu.rb in ppc-1.3.0

- old
+ new

@@ -1,42 +1,51 @@ require 'ppc/baidu/account' require 'ppc/baidu/plan' require 'ppc/baidu/bulk' +require 'ppc/baidu/group' +require 'ppc/baidu/key' require 'ppc/baidu/report' require 'awesome_print' -require 'savon' +require 'net/http' +require 'net/https' +require 'json' +# require 'savon' module PPC class Baidu include ::PPC def initialize(params = {}) @service = params[:service] + 'Service' - @port_name = params[:service] + 'Service' @username = params[:username] @password = params[:password] @token = params[:token] - @client = Savon.new("https://api.baidu.com/sem/sms/v3/#{@service}?wsdl") @debug = params[:debug] || false end - def request(method,params = {}) - operation = make_operation(method) - operation.header = operation_header - operation.body = { - method+'Request' =>params + def request(method,params = {}, with_header = false) + uri = URI("https://api.baidu.com/json/sms/v3/#{@service}/#{method}") + http_body = { + header: request_header, + body: params + }.to_json + + http_header = { + 'Content-Type' => 'application/json; charset=UTF-8' } - print_debug(operation.body,'operation.body') if @debug - print_debug(operation.build,'operation.build') if @debug - response = operation.call.hash - process_response(response) - print_debug(response,'response') if @debug - response + + http = Net::HTTP.new(uri.host, 443) + http.set_debug_output $stderr + http.use_ssl = true + + response = http.post(uri.path, http_body, http_header) + response = (JSON.parse response.body) + # if not needed, only return body + return response['body'] unless with_header else response end def operations - @client.operations(@service,@service) end protected def download(params = {}) bulk = ::PPC::Baidu::Bulk.new({ @@ -88,21 +97,15 @@ return false end private - def make_operation(operation_name) - @client.operation(@service,@service,operation_name) - end - def operation_header + def request_header { - :AuthHeader=> - { - username: @username, - password: @password, - token: @token - } + username: @username, + password: @password, + token: @token } end def example(operation,with_header=false) operation = make_operation(operation)