Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/besasoftware/besasoap

Delphi SOAP Client Component
https://github.com/besasoftware/besasoap

Last synced: about 1 month ago
JSON representation

Delphi SOAP Client Component

Lists

README

        

# BesaSoap
BesaSoap is written using new features of the Delphi language, represents C# or Java like native class support, nullable data types and custom attributes. The BesaSoap library is designed to help programmers develop faster and more native web service client applications.

BesaSoap is running Delphi XE2 and Up.

##Installation
Open your releated delphi package then compile and install.

##

###Nullable Types

* NullableString
* NullableWideString
* NullableAnsiString
* NullableBoolean
* NullableByte
* NullableCardinal
* NullableChar
* NullableCurrency
* NullableDateTime
* NullableDouble
* NullableExtended
* NullableGuid
* NullableInt64
* NullableInteger
* NullableNativeInt
* NullableShortInt
* NullableSingle
* NullableWord

Sample usage:
```pascal
var
Str:string;
StrN:NullableString;
begin
Str:='test';
StrN:='Nullable test';
Str:=StrN+' append';
StrN:=nil;
end;
```
###Serialization & Deserialization

With bsXMLSerializer you can serialize & deserialize your object.

```pascal
type
ArrayOfString=array of String;

[XmlRootAttribute('native_test','http://www.besasoftware.com')]
TNativeTest=class
[XmlElementAttribute('ShortInt')]
ShortInt_:ShortInt;
[XmlAttributeAttribute('SmallInt')]
SmallInt_:SmallInt;
LongInt_:LongInt;
Integer_:Integer;
Int64_:Int64;
Byte_:Byte;
Word_:Word;
LongWord_:LongWord;
Cardinal_:Cardinal;
UInt64_:UInt64;
//
Single_:Single;
Double_:Double;
Real_:Real;
Extended_:Extended;
Comp_:Comp;
Currency_:Currency;
//
Char_ : Char;
WideChar_ : WideChar;
AnsiChar_ : AnsiChar;
ShortString_ : ShortString;
String_ : String;
//
Date_:TDate;
Time_:TTime;
TDateTime_:TDateTime;
//
NlbString:NullableString;
NullString:NullableString;
NlbInteger:NullableInteger;
NullInteger:NullableInteger;
//
[XmlArrayAttribute('ArrayString')]
ArrayString_: array of string;
ArrayOfString_:ArrayOfString;
end;
```
####Serialization
```pascal
procedure TForm1.bSerializeClick(Sender: TObject);
var
serializer : TbsXMLSerializer;
clazz: TNativeTest;
xml:string;
begin
serializer:=TbsXMLSerializer.Create;
clazz:=TNativeTest.Create;

clazz.ShortInt_:=1;
clazz.SmallInt_:=2;
clazz.LongInt_:=3;
clazz.Integer_:=4;
clazz.Int64_:=5;
clazz.Byte_:=6;
clazz.Word_:=7;
clazz.LongWord_:=8;
clazz.Cardinal_:=9;
clazz.UInt64_:=10;

clazz.Single_:=1.0001;
clazz.Double_:=1.0002;
clazz.Real_:=1.0003;
clazz.Extended_:=1.0004;
clazz.Comp_:=1.05;
clazz.Currency_:=1.0006;
//
clazz.Char_ :='A';
clazz.WideChar_ :='B';
clazz.AnsiChar_ :='C';
clazz.ShortString_ :='ABCD';
clazz.String_ :='defgh';
//
clazz.Date_:=Date;
clazz.Time_:=Time;
clazz.TDateTime_:=Now;
//
clazz.NlbString:='NullableString';
clazz.NullString:=nil;
clazz.NlbInteger:=2015;
clazz.NullInteger:=nil;
//

SetLength(clazz.ArrayString_,3);
clazz.ArrayString_[0]:='One';
clazz.ArrayString_[1]:='Two';
clazz.ArrayString_[2]:='Three';
SetLength(clazz.ArrayOfString_,3);
clazz.ArrayOfString_[0]:='OneOf';
clazz.ArrayOfString_[1]:='TwoOf';
clazz.ArrayOfString_[2]:='ThreeOf';

xml:=serializer.SerializeToString(clazz);
Memo1.Lines.Text:=xml;
clazz.Free;
serializer.Free;
end;
```
Generated xml:
```xml

1
3
4
5
6
7
8
9
10
1.00010001659393
1.0002
1.0003
1.0004
1
1.0006
A
B
C
ABCD
defgh
2015-08-09T00:00:00Z
1899-12-30T10:21:29.58Z
2015-08-09T10:21:29.58Z
NullableString

2015


One
Two
Three


OneOf
TwoOf
ThreeOf

```
####Deserialization
```pascal
var
serializer : TbsXMLSerializer;
clazz: TNativeTest;
xml:string;
begin
serializer:=TbsXMLSerializer.Create;
serializer.SetType(TypeInfo(TNativeTest));
clazz:=serializer.DeserializeFromString(Memo1.Lines.Text).AsType;
Memo1.Lines.Append('clazz.NlbString:'+ clazz.NlbString);
Memo1.Lines.Append('clazz.NullString.HasValue:'+ BoolToStr(clazz.NullString.HasValue,true));
clazz.Free;
serializer.Free;
end;
```
###Attribute Types

Attribute Name | Description
------------ | -------------
XmlElementAttribute | Indicates that a public field or property represents an XML element when the serializer serializes or deserializes the object that contains it. In contrast if an XmlElementAttribute is not applied to such a field or property, the items in the array are encoded as a sequence of elements, nested under an element named after the field or property.
XmlAttributeAttribute | Specifies that the serializer must serialize the class member as an XML,attribute.
XmlTextAttribute | Indicates to the Serializer that the member must be treated as,XML,text when the class that contains it is serialized or deserialized.
XmlArrayAttribute | Specifies that the serializer must serialize a particular class member,as an array of XML elements.
XmlArrayItemAttribute | Represents an attribute that specifies the derived types that the Serializer can place in a serialized array.
XmlHolderAttribute | Specifies that the serializer must serialize the class as a Holder class. |

###SOAP
####Auhentication Types
BesaSoap, supports basic authentication and UsernameToken authentication.

####Generate WSDL proxy code

For importing wsdl you need the BesaSoap Importer Tool. You can download trial from http://www.besasoftware.com

For demo generated unit(http://www.webservicex.net/CurrencyConvertor.asmx?WSDL):
```pascal
// ************************************************************************ //
// Generated with BesaSoap WSDL Importer - http://www.besasoftware.com
// WSDL Address :http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
// Date Time :8.8.2015 20:08:14
// Importer Version :1.01.113
// ************************************************************************ //
unit CurrencyConvertor;

interface
uses
bsSOAPClient, bsNullable, bsAttribute;

type

// ************************************************************************ //
// Namespace : http://www.webserviceX.NET/
// binding : CurrencyConvertorSoap
// service : CurrencyConvertor
// port : CurrencyConvertorSoap
// URL : http://www.webservicex.net/CurrencyConvertor.asmx
// ************************************************************************ //
CurrencyConvertorSoap = interface(IInvokable)
['{A927F398-6AD1-4362-9A25-5D4775730267}']
function ConversionRate(FromCurrency: string; ToCurrency: string): Double; stdcall;
end;

function GetCurrencyConvertorSoap(Addr: string; BSSERVICE: TbsService): CurrencyConvertorSoap;

implementation

uses SysUtils;

function GetCurrencyConvertorSoap(Addr: string; BSSERVICE: TbsService): CurrencyConvertorSoap;
const
defURL = 'http://www.webservicex.net/CurrencyConvertor.asmx';
defSvc = 'CurrencyConvertor';
defPrt = 'CurrencyConvertorSoap';
defNS = 'http://www.webserviceX.NET/';
var
Service : TbsService;
begin
Result := nil;
if (Addr = '') then Addr := defURL;
if BSSERVICE = nil then
Service:= TbsService.Create(nil)
else
Service := BSSERVICE;

Service.URL:= defURL;
Service.ElementForm:=sfQualified;
Service.RegisterInterface( TypeInfo(CurrencyConvertorSoap), defNS);
Service.RegisterSoapAction('%OperationName%','http://www.webserviceX.NET/%OperationName%');
Result := (Service as CurrencyConvertorSoap);
end;

end.
```
Using soap methods:
```pascal
procedure TForm1.btnConversionRateClick(Sender: TObject);
var
Service : CurrencyConvertorSoap;
res: double;
begin
Service := GetCurrencyConvertorSoap('', bsCurrency);
res:=Service.ConversionRate(cbFromCurrency.Text,cbToCurrency.Text);
lblResult.Caption:='Rate :'+FloatToStr(res)
end;
```