Home       Servicebereich  Projekte  Kontakt  

LWsystems bietet Support und Dienstleistungen zur Integration von OpenGroupware.org in Ihre IT-Infrastruktur.
Rufen Sie uns einfach an!

05451 - 9363 845 oder 05403 - 5556

XML-RPC


German version

How to use the OpenGroupware.org XML-RPC API

OpenGroupware.org contains a smart XML-RPC API to perform almost all possible actions non-interactive from a program or a script. This description will show, how to use the OpenGroupware.org XML-RPC API with Perl or Python.

Traps

Create users from script

If you have not ever been work with XML-RPC I will show you a short example how to create OpenGroupware users from script with XML-RPC.

This is a Perl expample wich uses the SOAP::Lite module to do the hard word. With the use of this module, remote processing with XML-RPC is very easy. If you prefer the Python programming language you should use the xmlrpclib module. With Python 2.2 and below you should use a patched version of this library which supports HTTP Basic Authentication. It can be downloaded from http://developer.skyrix.com/02_skyrix/xmlrpc/xmlrpclib.py.

The example script

Das XML-RPC Beispiel ist zweistufig aufgebaut. Eine zentrale Klasse stellt die Verbindung zum XML-RPC Server her. Von dieser Klasse erben alle Klassen, die auf einzelnen Methoden des XML-RPC API zugreifen wollen.

The example script consists of two parts. The constructor of the main class OGo connects the XML-RPC daemon. All subclasses could inherit the constructor from this class.

Main Class

    package OGo;

    use XMLRPC::Lite;
    use Carp;
    use strict;


    sub new {
        my $this = shift;
        my $pass = shift;

        croak "Missing OGo root password\n" unless $pass;

        my $class = ref($this) || $this;
        my $self = {};
        bless $self, $class;

        $self->{'ogo'} = XMLRPC::Lite
            ->proxy("http://root:$pass\@localhost:20001/x/xmlrpc");

        return $self;
    }


    ### EOP ###
    1;

The attribute 'ogo' contains the XMLRPC::Lite object. All calls to the server are method calls of this object.

Functional Class

The funcitonal class creates a user in OpenGroupware. It will derived from the OGo class defined above. The call method calls the remote method on the server via XML-RPC .

    package OGo::User;

    use Carp;
    use strict;

    use base qw(OGo);

    sub create {
        my ($self,$login,$pass,$name,$firstname) = @_;

        croak "Missing login name\n";

        $data = {
                 login     => $login
                 firstname => $firstname,
                 name      => $name,
               };

        my $account = $self->_insert($data);
        $self->_setPassword($account->{'number'},$pass);
    }


    sub _insert {
        my $self = shift;
        my $data = shift;
        $self->call('account.insert',$data);
    }


    sub _setPassword {
        my ($self,$uid,$pass) = @_;

        my $salt = join '',
                   ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
        $cpass = crypt $pass,$salt;

        $self->call('account.setPassword',$uid,$pass,'true');
    }


    ### EOP ###
    1;

Next to this module you could write modules for arbritary uses. The module provides the interface to other programes and encapsulate the XML-RPC handling. In our example a little programm loads the modules and creates the OGo user.

Example program to use the modules

Das kleine Script erstellt einen Benutzer. Es könnte weitere spezifische Klassen einbinden, die das XML-RPC API abstrahieren und deren Funktionalität über für den automatisierten Zugriff auf OGo Daten nutzen.

The small script creates an OGo user. It could access some other classes to abstract other methods and data structures of the OGo XML-RPC interface. Further examples will shown at the documentation of the RPC::Ogo module.

    #!/usr/bin/perl
    #
    #
    use OGo::User;

    # create user in OGo via XML-RPC
    my $ogo =  OGo::User->new($ogorootpw);
    $ogo->create($login,$userpass,$name,$firstname);


Python example

With python, the XML-RPC handling ist similar simple as in Perl.

    #!/usr/bin/env python2.2

    import xmlrpclib

    server = xmlrpclib.Server('http://localhost:20001/x/xmlrpc',
                              login='root',password='secret')

    result = server.account.insert (
                   {
                   'login'         : "martin",
                   'firstname'     : "Martin",
                   'name'          : "Werthmöller",
                   }
           )

< zurück  | weiter >