User Tools

Site Tools


userspace:webservice_corner:ruby-rpc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

userspace:webservice_corner:ruby-rpc [2014/06/20 09:28]
dkoch
userspace:webservice_corner:ruby-rpc [2021/08/23 08:37]
Line 1: Line 1:
-====== Ruby JSON-RPC ====== 
-This is an example for a json-rpc in Ruby. 
-Change **test:w00t** to match a valid user and password and **opsi-server** to your opsiconfd-server. 
  
-<code ruby> 
-require 'net/http'                                                                                                                                                                                              
-require 'uri'                                                                                                                                                                                                   
-require 'json'                                                                                                                                                                                                  
-                                                                                                                                                                                                                
-class OpsiRPC                                                                                                                                                                                                   
-  def initialize(service_url)                                                                                                                                                                                   
-    @uri = URI.parse(service_url)                                                                                                                                                                               
-  end                                                                                                                                                                                                           
-                                                                                                                                                                                                                
-  def method_missing(name, *args)                                                                                                                                                                               
-    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json                                                                                                                               
-    resp = JSON.parse( http_post_request(post_body) )                                                                                                                                                           
-    raise JSONRPCError, resp['error'] if resp['error'                                                                                                                                                         
-    resp['result'                                                                                                                                                                                             
-  end                                                                                                                                                                                                           
-                                                                                                                                                                                                                
-  def http_post_request(post_body)                                                                                                                                                                              
-    http    = Net::HTTP.new(@uri.host, @uri.port)                                                                                                                                                               
-    http.use_ssl = true                                                                                                                                                                                         
-    http.verify_mode = OpenSSL::SSL::VERIFY_NONE                                                                                                                                                                
-    request = Net::HTTP::Post.new(@uri.request_uri)                                                                                                                                                             
-    request.basic_auth(@uri.user, @uri.password)                                                                                                                                                                
-    request.content_type = 'application/json'                                                                                                                                                                   
-    request.body = post_body                                                                                                                                                                                    
-    http.request(request).body                                                                                                                                                                                  
-  end                                                                                                                                                                                                           
-                                                                                                                                                                                                                
-  class JSONRPCError < RuntimeError; end                                                                                                                                                                        
-end                                                                                                                                                                                                             
-                                                                                                                                                                                                                
-  h = OpsiRPC.new('https://test:w00t@opsi-server:4447/rpc'                                                                                                                                               
-                                                                                                                                                                                                                
-                                                                                                                                                                                                                
-prodcuts=h.product_getObjects                                                                                                                                                                                   
-prodcuts.each do |product|                                                                                                                                                                                      
-  puts product['id'                                                                                                                                                                                           
-end        
-</code> 
userspace/webservice_corner/ruby-rpc.txt ยท Last modified: 2021/08/23 08:37 (external edit)