Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Delphi VCL REST Pricing Client App Part 3

Posted on 29th Oct 2017 in Delphi < XE4


restserver.png

Background

This is a VCL Rest Pricing Client Application at a fundamental working level. This App can easily be incorporated into a functional Windows Ribbon Styled or Modern Windows 10 styled Application. This App will fast track your development should you lack experience in Rest Client Development.

A rest server application is live and this application will connect directly to the live server for testing.

Scope

The Application required the REST calls to be made in JSON.

The Application must be able to send a GET REST call to receive the actual SERVER Price List.

View in a Grid.

Must be able to save the Web-Site Pricelist directly to application’s LOCAL DB.

View the LOCAL DB Pricelist in Grid.

Perform the CRUD (Create, Read, Update, Delete) functions to the Local-DB Pricelist.

Must update the Web-Server Pricelist.

Required

  • Delphi IDE VCL with the latest INDY library loaded.
  • Superobject Library for JSON processing.
  • What Libraries are created?
  • Delete Rest Response library and
  • JSON Parser library

Application

This application requires a Database to operate so a simulated json.txt database  is used. The Clientdataset is used as the in-memory database to communicate to the DBgrids.

A pagecontrol is added to the mainform with two tabs:-

App(db) table – interacts with the local database

Webserver (data) table – interacts with the data in the rest server.

 

The Application will start with the Webserver data being updated in a grid. The purpose is to compare the local pricing with the Webserver pricing. This data can also be transferred to the App side pricing to edit change and send back to the webserver and then synchronise the webserver with the local database.

restreceivejsontab2.PNG

The App(db) table - has the CRUD functions where all the editing takes place.

clientdelphitab1.PNG

Two  libraries DatasetUtils.pas  and JsonDatasetConverter.pas are used for Dataset calls…

UnMarshalltoDataset, ClientDatasetToJson, CreateField and CreateDatasetField.

All data is converted to Json for database saving and for sending to the rest-server.

 saveclientdbtab1.PNG

 

Instead of creating a local DB parser to compare the two database tables (local and Server) which would require synchronising of changed data followed by making a multitude of PUT and POST calls to the Webserver to synchronise the data.

The easiest option is as follows:-

First Delete all the data in the REST Server Database. In Part 1 it has a REST call to Delete  all    …Delete /0  which truncates (clears the contents and sets the index key to 0) the Restserver Database table.

A call is made to first Delete all data followed by a call to POST the Edited local DB data to the now empty REST Server Database table.

Now both the Local Application Database and Rest Server Database are synchronised.

 On PostwebserverbtnClick, the data is saved to the simulated Database then the DBGrid is reloaded.

procedure Tfmmain.PostwebserverbtnClick(Sender: TObject);
var
  http: TIDHttp;
  response: string;
  jsonresponse: string;
  jsontoweb: Tstream;
begin
  // save to simulated DB before sending Post data
  SavetosimulatedDB;
  LoadfromsimulatedDB;
  begin
    http := TIDHttp.Create(nil);
    http.HandleRedirects := true;
    http.ReadTimeout := 5000;
    http.MaxAuthRetries := 0;
    http.HTTPOptions := [hoKeepOrigProtocol];
    { for using Header Authorization
      //Http.HTTPOptions := [hoInProcessAuth];
      // Http.Request.CustomHeaders.Clear;
      // Http.Request.CustomHeaders.AddStrings('');
      //http.Request.BasicAuthentication := true;
      // Http.Request.Username := '';
      // Http.Request.Password := ''; }
    http.Request.Accept := 'http';
    http.Request.ContentType := 'application/json';
    try
      // need to have delete/all in API for this to work and clear the id's
      response := http.Delete
        ('http://www.bayeseanblog.com/blog/public/demo/salesrest/public/api/pricing/delete/0');
      // Showmessage(response);
    except
      on E: EIdHTTPProtocolException do
        Showmessage('Could not Connect to the Internet - Connection error!');
      // connection with a client in a "peaceful" way
      on E: EIdConnClosedGracefully do
        Showmessage
          ('Could not connect to the Internet - Connection was closed gracefully!');
      // this exception class covers all the low level socket exceptions
      on E: EIdSocketError do
        Showmessage
          ('Could not Connect to the Internet - Connection socket error!');
      // this exception class covers all exceptions thrown by Indy library
      on E: EIdException do
        Showmessage
          ('Could not connect to the Internet - Connection error exception!');
      // all exceptions different from those listed above
      on E: Exception do
        Showmessage('Unable to connect to the Internet!');
    end;

    response := '';
    if not(ClientDataSet1.Active) then
      fmmain.ClientDataSet1.CreateDataSet;
    jsontoweb := TStringStream.Create(ClientDataSetToJSON(ClientDataSet1),
      System.SysUtils.TEncoding.UTF8);
    try
      // sends all data to rest api
      jsonresponse :=
        http.Post(
        'http://www.bayeseanblog.com/blog/public/demo/salesrest/public/api/pricing/add/all',
        jsontoweb);
    except
      on E: EIdHTTPProtocolException do
        Showmessage('Could not Connect to the Internet - Connection error!');
      // connection with a client in a "peaceful" way
      on E: EIdConnClosedGracefully do
        Showmessage
          ('Could not connect to the Internet - Connection was closed gracefully!');
      // this exception class covers all the low level socket exceptions
      on E: EIdSocketError do
        Showmessage
          ('Could not Connect to the Internet - Connection socket error!');
      // this exception class covers all exceptions thrown by Indy library
      on E: EIdException do
        Showmessage
          ('Could not connect to the Internet - Connection error exception!');
      // all exceptions different from those listed above
      on E: Exception do
        Showmessage('Unable to connect to the Internet!');
    end;
    Showmessage('Webserver has been updated');
  end;
  http.Free;
  jsonresponse := '';
end;

Download

Download the Code from Github.

 

Download Code

 

Testing

The VCL Application is set to call directly to the Live Webserver. Live calls to save all are as follows:-  'http://www.bayeseanblog.com/blog/public/demo/salesrest/public/api/pricing/add/all'

The Application should function  100% . You may have to deactivate your Antivirus should it block the calls.

 

Happy Coding… 


Be the First to Comment...

Add a Comment

9+5

Recent News

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction
Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App
Delphi FMX Leaflet Plotter using OSM Maps
C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4

Archives

October 2018

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button