Giter Site home page Giter Site logo

eduals / delphi-rest-client-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fabriciocolombo/delphi-rest-client-api

0.0 1.0 0.0 1.35 MB

A Delphi REST client API to consume REST services written in any programming language.

License: Apache License 2.0

Batchfile 0.08% Pascal 97.96% Java 1.04% HTML 0.93%

delphi-rest-client-api's Introduction

Delphi REST Client API

A Delphi REST client API to consume REST services written in any programming language.

The API was tested in Delphi 7, XE, XE2, XE3, XE4 and XE7. It is also compatible with Mac OSX and iOS.

Connection Layer

There are a IHttpConnection interface to abstract the real Http conection. This interface currently have two implementations, using Indy 10, WinHTTP and WinInet.

Indy 9 does not handles HTTP response codes correctly, then if you are using Delphi 7, you must update your indy library to version 10 or use WinHttp (recommended). To disable indy support comment the compiler directive {.$DEFINE USE_INDY} in DelphiRest.inc file.

Serialization/Desserialization

The objects are transmitted in JSON format. To function properly, the object must be declared as follows, with public fields.

TPerson = class(TObject)
public 
  (* Reflect the server side object field names, for Java must be case-sensitive *)
  id: Integer;
  name: String;
  email: String;

  (* Static constructor *)
  class function NewFrom(Id: Integer; Name, EMail: String): TPerson;
end;

See more details about serialization here: Serialization

Samples

  • GET

     var
       vList : TList<TPerson>;
     begin
       vList := RestClient.Resource('http://localhost:8080/java-rest-server/rest/persons')
                          .Accept(RestUtils.MediaType_Json)
                          .Get<TList<TPerson>>();
    
  • GET ONE

     var
       vPerson : TPerson;
     begin
       vPerson := RestClient.Resource('http://localhost:8080/java-rest-server/rest/person/1')
                          .Accept(RestUtils.MediaType_Json)
                          .Get<TPerson>();
    
  • POST

     var
       vPerson : TPerson;
     begin
       vPerson := TPerson.NewFrom(123, 'Fabricio', '[email protected]');          
       RestClient.Resource('http://localhost:8080/java-rest-server/rest/person')
                 .Accept(RestUtils.MediaType_Json)
                 .ContentType(RestUtils.MediaType_Json)
                 .Post<TPerson>(vPerson);
    
  • PUT

     var
       vPerson : TPerson;
     begin
       vPerson := //Load person
       vPerson.Email := '[email protected]';
       RestClient.Resource('http://localhost:8080/java-rest-server/rest/person')
                 .Accept(RestUtils.MediaType_Json)
                 .ContentType(RestUtils.MediaType_Json)
                 .Put<TPerson>(vPerson);
    
  • DELETE

     var
       vPerson : TPerson;
     begin
       vPerson := //Load person
       RestClient.Resource('http://localhost:8080/java-rest-server/rest/person')
                 .Accept(RestUtils.MediaType_Json)
                 .ContentType(RestUtils.MediaType_Json)
                 .Delete(vPerson);
    
  • GET AS DATASET

The fields need be predefined.

    var
      vDataSet: TClientDataSet;
    begin
      vDataSet := TClientDataSet.Create(nil);
      try
        TDataSetUtils.CreateField(vDataSet, ftInteger, 'id');
        TDataSetUtils.CreateField(vDataSet, ftString, 'name', 100);
        TDataSetUtils.CreateField(vDataSet, ftString, 'email', 100);
        vDataSet.CreateDataSet;

       RestClient.Resource(CONTEXT_PATH + 'persons')
                  .Accept(RestUtils.MediaType_Json)
                  .GetAsDataSet(vDataSet);
      finally
        vDataSet.Free;
      end;
  • GET AS DYNAMIC DATASET

The fields are created dynamically according to the returned content.

    var
      vDataSet: TDataSet;
    begin
      vDataSet := RestClient.Resource(CONTEXT_PATH + 'persons')
                            .Accept(RestUtils.MediaType_Json)
                            .GetAsDataSet();        
      try
        //Do something
      finally
        vDataSet.Free;
      end;

Java Rest Server

The java project is only for test purpose and has built using Maven and Jersey, so it's needed have installed the JRE 6+ (Java Runtime Environment) and Maven 2 to build and run the application. The Maven bin directory must be included in Windows Path environment variable.

After install Java and Maven just run 'start-java-server.bat' to start the application and 'stop-java-server.bat' to shut down them.

When 'start-java-server.bat' is first run maven dependencies will be downloaded, and it may take a while.

License

The Delphi REST client API is released under version 2.0 of the Apache License.

delphi-rest-client-api's People

Contributors

alex-taylor-ioof avatar fabriciocolombo avatar frees avatar robertoschneiders avatar thomaserlang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.