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 [2015/02/06 10:27]
dkoch [Example]
userspace:webservice_corner:ruby-rpc [2021/08/23 08:37]
Line 1: Line 1:
-====== Ruby JSON-RPC ====== 
-This is an example of a JSON-RPC send from [[https://www.ruby-lang.org|Ruby]] code. 
  
-===== Dependencies  ===== 
-Use gem to install json first. 
-<code>gem install json</code> 
- 
-===== Example ===== 
-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' 
-require 'openssl'                                                                                                                                                                                                  
-                                                                                                                                                                                                                
-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                                                                                                                                                                                                             
-                                                                                                                                                                                                                
-  opsirpc = OpsiRPC.new('https://test:w00t@opsi-server:4447/rpc'                                                                                                                                               
-                                                                                                                                                                                                                
-# Examples: 
- 
-# Show all product ids 
-opsirpc.product_getObjects.each do |product|                                                                                                                                                                                      
-  puts product['id'                                                                                                                                                                                           
-end 
- 
-# Query only particular fields ( "type" in this example ) 
-# ( attributes: ) 
-opsirpc.product_getObjects("type").each do |result| 
-  puts result 
-end 
- 
-# Get products where packageVersion=2.1 
-# ( filter: ) 
-opsirpc.product_getObjects(nil,packageVersion:"2.1").each do |result| 
-  puts result 
-end 
- 
-# Install X where Y is installed 
-opsirpc.productOnClient_getHashes(['clientId'], {productId: 'X', installationStatus: "installed"}).each do |match| 
-  opsirpc.setProductActionRequestWithDependencies('Y', match['clientId'], 'setup') 
-end 
-</code> 
userspace/webservice_corner/ruby-rpc.txt ยท Last modified: 2021/08/23 08:37 (external edit)