User Tools

Site Tools


userspace:webservice_corner:ruby-rpc

Table of Contents

Ruby JSON-RPC

This is an example of a JSON-RPC send from Ruby code.

Dependencies

Use gem to install json first.

gem install json

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.

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([],packageVersion:"2.1").each do |result|
  puts result
end
 
# Install Y where X is installed
opsirpc.productOnClient_getHashes(['clientId'], {productId: 'X', installationStatus: "installed"}).each do |match|
  opsirpc.setProductActionRequestWithDependencies('Y', match['clientId'], 'setup')
end
userspace/webservice_corner/ruby-rpc.txt · Last modified: 2021/08/23 08:37 (external edit)