BaaS with Kinvey and Delphi 10.1 Berlin




Here it's my Win64 example that uses Kinvey BaaS:

Add action, adds data into the collection and reload just brings the data back from the cloud and displays it in a listview component.
Here is the code behind it:
Add item:
| // Copyright (c) 2016, Jordi Corbilla | |
| // All rights reserved. | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // - Redistributions of source code must retain the above copyright notice, | |
| // this list of conditions and the following disclaimer. | |
| // - Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // - Neither the name of this library nor the names of its contributors may be | |
| // used to endorse or promote products derived from this software without | |
| // specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| // POSSIBILITY OF SUCH DAMAGE. | |
| function TKinveyRest.Add(jsonString: string) : boolean; | |
| var | |
| IdHTTP: TIdHTTP; | |
| IdIOHandler: TIdSSLIOHandlerSocketOpenSSL; | |
| response : string; | |
| JsonToSend: TStringStream; | |
| encodedHeader : string; | |
| begin | |
| JsonToSend := TStringStream.Create(jsonString); | |
| try | |
| IdIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); | |
| IdIOHandler.ReadTimeout := IdTimeoutInfinite; | |
| IdIOHandler.ConnectTimeout := IdTimeoutInfinite; | |
| IdHTTP := TIdHTTP.Create(nil); | |
| try | |
| IdHTTP.IOHandler := IdIOHandler; | |
| IdHTTP.Request.Connection := 'Keep-Alive'; | |
| IdIOHandler.SSLOptions.Method := sslvSSLv23; | |
| IdHTTP.Request.CustomHeaders.Clear; | |
| encodedHeader := TIdEncoderMIME.EncodeString(FOptions.AppId + ':' + FOptions.MasterSecret); | |
| IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'Basic ' + encodedHeader; | |
| IdHTTP.Request.CustomHeaders.Values['X-Kinvey-API-Version'] := '3'; | |
| IdHTTP.Request.ContentType := 'application/json'; | |
| response := IdHTTP.Post('https://baas.kinvey.com/appdata/'+FOptions.AppId+'/'+FOptions.Collection+'/', JsonToSend); | |
| response := response.Replace(AnsiChar(#10), ''); | |
| result := (response.Contains('creator')); | |
| finally | |
| IdHTTP.Free; | |
| end; | |
| finally | |
| IdIOHandler.Free; | |
| JsonToSend.Free; | |
| end; | |
| end; |
Load data:
| // Copyright (c) 2016, Jordi Corbilla | |
| // All rights reserved. | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // - Redistributions of source code must retain the above copyright notice, | |
| // this list of conditions and the following disclaimer. | |
| // - Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // - Neither the name of this library nor the names of its contributors may be | |
| // used to endorse or promote products derived from this software without | |
| // specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| // POSSIBILITY OF SUCH DAMAGE. | |
| function TKinveyRest.GetCollection: string; | |
| var | |
| IdHTTP: TIdHTTP; | |
| IdIOHandler: TIdSSLIOHandlerSocketOpenSSL; | |
| response : string; | |
| encodedHeader : string; | |
| begin | |
| try | |
| IdIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); | |
| IdIOHandler.ReadTimeout := IdTimeoutInfinite; | |
| IdIOHandler.ConnectTimeout := IdTimeoutInfinite; | |
| IdHTTP := TIdHTTP.Create(nil); | |
| try | |
| IdHTTP.IOHandler := IdIOHandler; | |
| IdHTTP.Request.Connection := 'Keep-Alive'; | |
| IdIOHandler.SSLOptions.Method := sslvSSLv23; | |
| IdHTTP.Request.CustomHeaders.Clear; | |
| encodedHeader := TIdEncoderMIME.EncodeString(FOptions.AppId + ':' + FOptions.MasterSecret); | |
| IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'Basic ' + encodedHeader; | |
| IdHTTP.Request.CustomHeaders.Values['X-Kinvey-API-Version'] := '3'; | |
| IdHTTP.Request.ContentType := 'application/json'; | |
| response := IdHTTP.Get('https://baas.kinvey.com/appdata/'+FOptions.AppId+'/'+FOptions.Collection+'/'); | |
| result := response; | |
| finally | |
| IdHTTP.Free; | |
| end; | |
| finally | |
| IdIOHandler.Free; | |
| end; | |
| end; |
As you can see, each method uses a POST/GET command to baas.kinvey url with some arguments, headers and options. I find this way really useful as you can clearly see what's going on and easily map what you could do via curl:
| #Example posting to Kinvey via curl | |
| >curl -H 'Content-Type: application/json' --user APPKEY:MASTERSECRET -X PUT -d '{"user":"user","password":"password","url","www.google.co.uk"}' https://baas.kinvey.com/appdata/APPKEY/Websites | |
| #Example reading the content of the collection | |
| >curl --user APPKEY:MASTERSECRET -X GET https://baas.kinvey.com/appdata/APPKEY/Websites/ -G --data-urlencode 'order={"user"}' |
If you want to run this example successfully you'll need to include libeay32.dll and ssleay32.dll from OpenSSL.
You can test this app and its mobile companion app using the source codehere.
Using Kinvey Provider component
Delphi 10.1 Berlin has a KinveyProvider component that we can use directly without having to worry about the request details. You'll see below that to do the same as the code sample above we will need just few lines of code:

The KinveyProvider needs the parameters:
- AppKey
- AppSecret
- MasterSecret
Get those from the Kinvey dashboard and off you go.
Then connect the BackEndStorage component to the KinveyProvider component.
Now, here is the code to Add items to the collection and to load the collection via components:
Load Data:
| // Copyright (c) 2016, Jordi Corbilla | |
| // All rights reserved. | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // - Redistributions of source code must retain the above copyright notice, | |
| // this list of conditions and the following disclaimer. | |
| // - Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // - Neither the name of this library nor the names of its contributors may be | |
| // used to endorse or promote products derived from this software without | |
| // specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| // POSSIBILITY OF SUCH DAMAGE. | |
| function TForm2.GetCollection() : TList<TUrls>; | |
| var | |
| list: TList<TUrls>; | |
| url: TUrls; | |
| begin | |
| try | |
| FBackendList := CreateUrlsList(BackendStorage1.Provider.ProviderID, BackendStorage1.Storage); | |
| list := TList<TUrls>.Create; | |
| try | |
| for url in FBackendList do | |
| list.Add(url); | |
| except | |
| list.Free; | |
| raise; | |
| end; | |
| finally | |
| result := list; | |
| end; | |
| end; | |
| function TForm2.CreateUrlsList(const AProviderID: string; const AStorage: TBackendStorageApi): TBackendObjectList<TUrls>; | |
| var | |
| LQuery: TArray<string>; | |
| LContactList: TBackendObjectList<TUrls>; | |
| begin | |
| LContactList := TBackendObjectList<TUrls>.Create; | |
| try | |
| LQuery := TArray<string>.Create(Format('order=%s', ['user'])); | |
| AStorage.QueryObjects<TUrls>('websites', LQuery, LContactList); | |
| except | |
| LContactList.Free; | |
| raise; | |
| end; | |
| Result := LContactList; | |
| end; |
Add Item:
| // Copyright (c) 2016, Jordi Corbilla | |
| // All rights reserved. | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // - Redistributions of source code must retain the above copyright notice, | |
| // this list of conditions and the following disclaimer. | |
| // - Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // - Neither the name of this library nor the names of its contributors may be | |
| // used to endorse or promote products derived from this software without | |
| // specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| // POSSIBILITY OF SUCH DAMAGE. | |
| procedure TForm2.btnAddClick(Sender: TObject); | |
| var | |
| url : TUrls; | |
| entity: TBackendEntityValue; | |
| begin | |
| url := TURls.Create(edit1.Text, edit2.Text, edit3.Text); | |
| BackendStorage1.Storage.CreateObject<TUrls>('websites', url, entity); | |
| end; |
As you can see now it's way simpler.
And here my android app up and running:

Now I have a Win64 app and an Android app that share the same back end usingKinvey BaaS.
And the data in Kinvey:

Although this article it's quite long I'm sure you will find it quite interesting if you still haven't played with these components and the cloud.
Note that this BackEnd service will cease to exist shortly (as the appkey is hardcoded in the android app and the source code is available).
Do not hesitate to contact me if you have any questions.
http://www.delphifeeds.com/go/s/134494
BaaS with Kinvey and Delphi 10.1 Berlin的更多相关文章
- Delphi 10.1 Berlin UTF8String Test
Delphi 10.1 Berlin UTF8String Test procedure TForm1.Button1Click(Sender: TObject); var s: UTF8String ...
- Delphi 10.1 Berlin 官方未列之修正
Delphi 10.1 Berlin 官方修正列表: Bug fix list for RAD Studio 10.1 Berlin Delphi 10.1 Berlin 官方未列之修正: 修正 iO ...
- delphi 10.1 berlin最新的开发框架:咏南中间件+咏南开发框架,购买后提供全部的源码
咏南中间件+咏南开发框架支持最新的delphi 10.1(berlin),老用户提供免费升级. 购买提供:中间件源码 附带福利(赠送): CS开发框架源码BS开发框架源码移动APP源码中间件集群源码二 ...
- Delphi 10.1 Berlin Starter Edition
Delphi 10.1 Berlin Starter Edition Embarcadero® Delphi 10.1 Berlin Starter is a great way to get sta ...
- Where is the ActiveX Project Type for Delphi 10.1 Berlin
n 10.1 Berlin the ActiveX project types are missing from the New Items Window under Delphi. They are ...
- delphi 10.1 Berlin 中使用自带的 Base64 编码
delphi 10.1 berlin版本中,有好几个 base64 编码的单元可以使用,例如 Indy, MessageDigest_5,还有 CnBase64,我现在使用自带的 System.Net ...
- delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决
delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...
- TNetHttpClient支持异步访问(Delphi 10.1 Berlin,红鱼儿的博客)
Delphi 10.1进一步改进自Delphi 10带来的Http访问控件TNetHttpClient,支持异步访问,同时增加ConnectionTimeout及ResponseTimeout两个超时 ...
- Delphi 10.1 Berlin 与 Delphi 10 Seattle 共存
以下安装环境是win7 64位 1. 安装Delphi10.1 Berlin 版本. 2.修改C:\Program Files (x86)\Embarcadero\Studio\18.0\cglm.i ...
随机推荐
- Javaweb整合mongo和kettle6.0的环境配置
为了编译能通过,maven需要加入仓库地址以及一些必须要的包的依赖情况: pentaho中央仓库: 在properties里面配置版本号: <kettle.version>6.0.0.0- ...
- C++ Primer 读书笔记: 第9章 顺序容器
第9章 顺序容器 引: 顺序容器: vector 支持快速随机访问 list 支持快速插入/删除 deque 双端队列 顺序容器适配器: stack 后进先出栈 queue 先进先出队列 priori ...
- CFILE追加写入文件
CFile file; file.Open(strName, CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate); ) { file.S ...
- vijos 1067 Warcraft III 守望者的烦恼 矩阵
题目链接 我们可以很容易的推出dp的式子, dp[i] = sigma(j : 1 to k) dp[i-j]. 但是n太大了, 没有办法直接算, 所以我们构造一个矩阵, 然后快速幂就好了. 就像这样 ...
- C#实现微信开发
C#实现微信开发前奏 不想废话,直接写了!因为是留给自己做随笔的,所以大神们看到别喷…… 1.必须有微信公众账号 2.你也可以申请测试微信号,链接给你 http://mp.weixin.qq.co ...
- 优盘(U 盘) 采用TLC, MLC, SLC芯片 的区别 与使用寿命
最近一直在看大家在讨论sandisk,pny,金士顿等大厂都开始用tlc的芯片问题,让大家基本都不敢用U盘存数据了按照之前的擦写参数TLC 1000次MLC 10000次SL ...
- 【虚拟化实战】容灾设计之四VPLEX
作者:范军 (Frank Fan) 新浪微博:@frankfan7 VPLEX等存储设备的出现,可以实现双活数据中心,最大程度的有效利用运算和存储资源. 在“容灾设计之三Stretched Clust ...
- Numpy之ndarray与matrix
1. ndarray对象 ndarray是numpy中的一个N维数组对象,可以进行矢量算术运算,它是一个通用的同构数据多维容器,即其中的所有元素必须是相同类型的. 可以使用array函数创建数组,每个 ...
- Mac 域名解析
1. 域名解析 如,解析 www.baidu.com 在终端输入, host www.baidu.com 题外话: 转载自:http://hoarn.blog.51cto.com/1642678/14 ...
- SVN使用Tips
1. 如果在本地删除了某个文件,在Cornerstone上的本地仓库会出现D的标志,并且文件不存在. 这时,只需要将该文件提交到服务器上,本地仓库就会清除了已删除的文件的标识,同时,服务器上也会自动删 ...