Testing your Class Job

Retrieve the proxy for your JSON-RPC API

It is not a must, but if you plan to write a client in a language for whom DMSContainer provides a proxy, you can speed up your work.

Go to https://localhost/info and download your proxy for your language.

Testing the JSON-RPC interface of the beeper job

The JSON-RPC interface can be consumed from all the languages and tools able to send an HTTP request. Let’s see how to use it from a Python script and a Delphi program.

Python script

This is a Python script which invokes the JSON-RPC method published by out sample job. NOTE: The script requires the package requests installable using the pip tool. More information here

import requests
import json
import urllib3

# DMSContainer is published by default under HTTPS using a self signed certificate,

# so we need to ignore the warnings, or in production, replace the 

# certificate with a valid one.


urllib3.disable_warnings()

def main():
    first_number = second_number = ''
    while not first_number.isnumeric():
        first_number = input("First number: ")
    while not second_number.isnumeric():
        second_number = input("Second number: ")
    url = "https://localhost/calculatorrpc"
    headers = {"content-type": "application/json", "accept": "application/json"}
    payload = {"method": "sum", "params": [int(first_number), int(second_number)], "jsonrpc": "2.0", "id": 1}
    response = requests.post(url, json=payload, headers=headers, verify=False).json()
    result = response['result']
    print(f"{first_number} + {second_number} = {result}")


if __name__ == "__main__":
    main()

This is an example of a script launch.

C:\> python rpc_calculator_client.py

First number: 3
Second number: 4
3 + 4 = 7

Delphi program

This is a Delphi project which invokes the JSON-RPC method published by out sample job.

unit MainFormU;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, 
  System.Variants, System.Classes, Vcl.Controls, Vcl.Forms, 
  Vcl.StdCtrls, Vcl.ExtCtrls, System.Net.URLClient;

type
  TMainForm = class(TForm)
    edtFirstNum: TLabeledEdit;
    edtSecondNum: TLabeledEdit;
    btnDoSum: TButton;
    lblResult: TLabel;
    procedure btnDoSumClick(Sender: TObject);
  private
    procedure ValidateCertificateEvent(
    	const Sender: TObject; 
    	const ARequest: TURLRequest;
      	const Certificate: TCertificate; 
      	var Accepted: Boolean);
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  System.NetEncoding, System.IOUtils, 
  MVCFramework.JSONRPC, MVCFramework.JSONRPC.Client;

{$R *.dfm}

procedure TMainForm.btnDoSumClick(Sender: TObject);
var
  lReq: IJSONRPCRequest;
  lResp: IJSONRPCResponse;
  lRPCExecutor: IMVCJSONRPCExecutor;
begin
  lRPCExecutor := TMVCJSONRPCExecutor.Create('https://localhost/calculatorrpc');
  lRPCExecutor.SetOnValidateServerCertificate(ValidateCertificateEvent);
  lReq := TJSONRPCRequest.Create(1234, 'sum');
  lReq.Params.Add(StrToInt(edtFirstNum.Text));
  lReq.Params.Add(StrToInt(edtSecondNum.Text));
  lResp := lRPCExecutor.ExecuteRequest(lReq);
  lblResult.Caption := 'Result = ' + IntToStr(lResp.Result.AsInteger);
end;

procedure TMainForm.ValidateCertificateEvent(const Sender: TObject;
  const ARequest: TURLRequest; const Certificate: TCertificate;
  var Accepted: Boolean);
begin
  Accepted := True; // do not check certificate
end;

end.

This is an example of a program session.