├── src ├── SimpleRestRequest.inc ├── SimpleRestRequest.Interfaces.pas └── SimpleRestRequest.pas ├── README.md ├── LICENSE └── .gitignore /src/SimpleRestRequest.inc: -------------------------------------------------------------------------------- 1 | {$define HASJSONDATAOBJCTS}// Desabilitar caso não tenha ou não deseje utilizar a unit https://github.com/ahausladen/JsonDataObjects#json-data-objects -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleRESTRequest 2 | Biblioteca para Requisições REST de forma simples no Delphi 3 | 4 | ```delphi 5 | uses 6 | SimpleRestRequest, 7 | SimpleRestRequest.Interfaces; 8 | 9 | var 10 | LRequest: iSimpleRestResquest; 11 | begin 12 | LRequest := TSimpleRestRequest.New 13 | .BaseURL('http://seusite.com') 14 | .ContentType('application/json') 15 | .Get; 16 | 17 | if LRequest.StatusCode = 200 then 18 | Result := LRequest.Return; // JSON 19 | end; 20 | ``` 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Thulio Bittencourt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/SimpleRestRequest.Interfaces.pas: -------------------------------------------------------------------------------- 1 | unit SimpleRestRequest.Interfaces; 2 | 3 | interface 4 | 5 | {$I SimpleRestRequest.inc} 6 | 7 | uses 8 | {$ifdef HASJSONDATAOBJCTS}JsonDataObjects,{$endif} 9 | {$IF CompilerVersion > 22} 10 | System.JSON, 11 | {$IFEND} 12 | IdSSLOpenSSL; 13 | 14 | type 15 | iSimpleRestRequest = interface 16 | function StatusCode : Integer; 17 | function Password ( aValue : string) : iSimpleRestRequest; 18 | function Username ( aValue : string) : iSimpleRestRequest; 19 | function IOHandler( aValue : TIdSSLIOHandlerSocketOpenSSL) : iSimpleRestRequest; 20 | function AddHeaders ( aKey : String; aValue : String) : iSimpleRestRequest; 21 | function ContentType (aValue : String) : iSimpleRestRequest; 22 | function Connection (aValue : String) : iSimpleRestRequest; 23 | function UserAgent (aValue : String) : iSimpleRestRequest; 24 | function HandleRedirects ( aValue : Boolean ) : iSimpleRestRequest; 25 | function BaseURL (aValue : String) : iSimpleRestRequest; 26 | function Body (aValue : String) : iSimpleRestRequest; overload; 27 | {$IF CompilerVersion > 22} 28 | function Body (aValue : TJsonObject) : iSimpleRestRequest; overload; 29 | {$IFEND} 30 | {$ifdef HASJSONDATAOBJCTS} 31 | function Body (aValue : TJDOJsonObject) : iSimpleRestRequest; overload; 32 | {$endif} 33 | function Post : iSimpleRestRequest; 34 | function Get : iSimpleRestRequest; 35 | function Delete : iSimpleRestRequest; 36 | function Put : iSimpleRestRequest; 37 | function Return : String; 38 | end; 39 | 40 | implementation 41 | 42 | end. 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Uncomment these types if you want even more clean repository. But be careful. 2 | # It can make harm to an existing project source. Read explanations below. 3 | # 4 | # Resource files are binaries containing manifest, project icon and version info. 5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 6 | #*.res 7 | # 8 | # Type library file (binary). In old Delphi versions it should be stored. 9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 10 | #*.tlb 11 | # 12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 13 | # Uncomment this if you are not using diagrams or use newer Delphi version. 14 | #*.ddp 15 | # 16 | # Visual LiveBindings file. Added in Delphi XE2. 17 | # Uncomment this if you are not using LiveBindings Designer. 18 | #*.vlb 19 | # 20 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 21 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 22 | #*.deployproj 23 | # 24 | # C++ object files produced when C/C++ Output file generation is configured. 25 | # Uncomment this if you are not using external objects (zlib library for example). 26 | #*.obj 27 | # 28 | 29 | # Delphi compiler-generated binaries (safe to delete) 30 | *.exe 31 | *.dll 32 | *.bpl 33 | *.bpi 34 | *.dcp 35 | *.so 36 | *.apk 37 | *.drc 38 | *.map 39 | *.dres 40 | *.rsm 41 | *.tds 42 | *.dcu 43 | *.lib 44 | *.a 45 | *.o 46 | *.ocx 47 | 48 | # Delphi autogenerated files (duplicated info) 49 | *.cfg 50 | *.hpp 51 | *Resource.rc 52 | 53 | # Delphi local files (user-specific info) 54 | *.local 55 | *.identcache 56 | *.projdata 57 | *.tvsconfig 58 | *.dsk 59 | 60 | # Delphi history and backups 61 | __history/ 62 | __recovery/ 63 | *.~* 64 | 65 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 66 | *.stat 67 | 68 | # Boss dependency manager vendor folder https://github.com/HashLoad/boss 69 | modules/ 70 | -------------------------------------------------------------------------------- /src/SimpleRestRequest.pas: -------------------------------------------------------------------------------- 1 | unit SimpleRestRequest; 2 | 3 | interface 4 | 5 | {$I SimpleRestRequest.inc} 6 | 7 | uses 8 | SimpleRestRequest.Interfaces, 9 | IdBaseComponent, 10 | IdComponent, 11 | IdTCPConnection, 12 | IdTCPClient, 13 | IdHTTP, 14 | {$ifdef HASJSONDATAOBJCTS}JsonDataObjects,{$endif} 15 | {$IF CompilerVersion > 22} 16 | System.JSON, System.Classes, 17 | {$ELSE} 18 | Classes, 19 | {$IFEND} 20 | IdSSLOpenSSL; 21 | 22 | type 23 | TSimpleRestRequest = class(TInterfacedObject, iSimpleRestRequest) 24 | private 25 | FIdHTTP : TIdHTTP; 26 | FIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL; 27 | FBaseURL : String; 28 | FStreamSend : TStringStream; 29 | FReturn : String; 30 | function IsBasicAuthentication : Boolean; 31 | public 32 | constructor Create; 33 | destructor Destroy; override; 34 | class function New : iSimpleRestRequest; 35 | function StatusCode : Integer; 36 | function Password ( aValue : string) : iSimpleRestRequest; 37 | function Username ( aValue : string) : iSimpleRestRequest; 38 | function IOHandler( aValue : TIdSSLIOHandlerSocketOpenSSL) : iSimpleRestRequest; 39 | function AddHeaders ( aKey : String; aValue : String) : iSimpleRestRequest; 40 | function ContentType (aValue : String) : iSimpleRestRequest; 41 | function Connection (aValue : String) : iSimpleRestRequest; 42 | function UserAgent (aValue : String) : iSimpleRestRequest; 43 | function HandleRedirects ( aValue : Boolean ) : iSimpleRestRequest; 44 | function BaseURL (aValue : String) : iSimpleRestRequest; 45 | function Body(aValue: String): iSimpleRestRequest; overload; 46 | {$IF CompilerVersion > 22} 47 | function Body (aValue : TJsonObject) : iSimpleRestRequest; overload; 48 | {$IFEND} 49 | {$ifdef HASJSONDATAOBJCTS} 50 | function Body (aValue : TJDOJsonObject) : iSimpleRestRequest; overload; 51 | {$endif} 52 | function Post : iSimpleRestRequest; 53 | function Get : iSimpleRestRequest; 54 | function Delete : iSimpleRestRequest; 55 | function Put : iSimpleRestRequest; 56 | function Return : String; 57 | end; 58 | 59 | implementation 60 | 61 | { TSimpleRestRequest } 62 | 63 | function TSimpleRestRequest.AddHeaders(aKey, 64 | aValue: String): iSimpleRestRequest; 65 | begin 66 | Result := Self; 67 | FIdHTTP.Request.CustomHeaders.AddValue(aKey, aValue); 68 | end; 69 | 70 | function TSimpleRestRequest.BaseURL(aValue: String): iSimpleRestRequest; 71 | begin 72 | Result := Self; 73 | FBaseURL := aValue; 74 | end; 75 | 76 | {$ifdef HASJSONDATAOBJCTS} 77 | function TSimpleRestRequest.Body(aValue: TJDOJsonObject): iSimpleRestRequest; 78 | begin 79 | Result := Self; 80 | FStreamSend := TStringStream.Create(aValue.ToJSON); 81 | end; 82 | {$endif} 83 | 84 | function TSimpleRestRequest.Body(aValue: String): iSimpleRestRequest; 85 | begin 86 | Result := Self; 87 | FStreamSend := TStringStream.Create(aValue); 88 | end; 89 | 90 | {$IF CompilerVersion > 22} 91 | function TSimpleRestRequest.Body(aValue: TJsonObject): iSimpleRestRequest; 92 | begin 93 | Result := Self; 94 | FStreamSend := TStringStream.Create(aValue.ToJSON); 95 | end; 96 | {$IFEND} 97 | 98 | function TSimpleRestRequest.Connection(aValue: String): iSimpleRestRequest; 99 | begin 100 | Result := Self; 101 | FIdHTTP.Request.Connection := aValue; 102 | end; 103 | 104 | function TSimpleRestRequest.ContentType(aValue: String): iSimpleRestRequest; 105 | begin 106 | Result := Self; 107 | FIdHTTP.Request.ContentType := aValue; 108 | end; 109 | 110 | constructor TSimpleRestRequest.Create; 111 | begin 112 | FIdHTTP := TIdHTTP.Create(nil); 113 | FIdHTTP.Request.Connection := 'Keep-Alive'; 114 | FIdHTTP.Request.UserAgent := 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'; 115 | FIdHTTP.HandleRedirects := true; 116 | end; 117 | 118 | function TSimpleRestRequest.Delete: iSimpleRestRequest; 119 | var 120 | FStreamResult : TStringStream; 121 | begin 122 | Result := Self; 123 | FStreamResult := TStringStream.Create; 124 | try 125 | // FIdHTTP.Delete(FBaseURL, FStreamResult); 126 | FReturn := FStreamResult.DataString; 127 | finally 128 | FStreamResult.Free; 129 | end; 130 | end; 131 | 132 | destructor TSimpleRestRequest.Destroy; 133 | begin 134 | if Assigned(FStreamSend) then 135 | FStreamSend.Free; 136 | 137 | FIdHTTP.Free; 138 | inherited; 139 | end; 140 | 141 | function TSimpleRestRequest.Get: iSimpleRestRequest; 142 | var 143 | FStreamResult : TStringStream; 144 | begin 145 | Result := Self; 146 | FStreamResult := TStringStream.Create; 147 | try 148 | FIdHTTP.Get(FBaseURL, FStreamResult); 149 | FReturn := FStreamResult.DataString; 150 | finally 151 | FStreamResult.Free; 152 | end; 153 | end; 154 | 155 | function TSimpleRestRequest.HandleRedirects( 156 | aValue: Boolean): iSimpleRestRequest; 157 | begin 158 | Result := Self; 159 | FIdHTTP.HandleRedirects := aValue; 160 | end; 161 | 162 | function TSimpleRestRequest.IOHandler( 163 | aValue: TIdSSLIOHandlerSocketOpenSSL): iSimpleRestRequest; 164 | begin 165 | Result := Self; 166 | FIdSSLIOHandlerSocket := aValue; 167 | FIdHTTP.IOHandler := FIdSSLIOHandlerSocket; 168 | end; 169 | 170 | function TSimpleRestRequest.IsBasicAuthentication: Boolean; 171 | begin 172 | Result := (FIdHTTP.Request.Password <> '') and (FIdHTTP.Request.Username <> ''); 173 | end; 174 | 175 | class function TSimpleRestRequest.New: iSimpleRestRequest; 176 | begin 177 | Result := Self.Create; 178 | end; 179 | 180 | function TSimpleRestRequest.Password(aValue: string): iSimpleRestRequest; 181 | begin 182 | Result := Self; 183 | FIdHTTP.Request.Password := aValue; 184 | FIdHTTP.Request.BasicAuthentication := IsBasicAuthentication; 185 | end; 186 | 187 | function TSimpleRestRequest.Post: iSimpleRestRequest; 188 | begin 189 | Result := Self; 190 | FReturn := FIdHTTP.Post(FBaseURL, FStreamSend); 191 | end; 192 | 193 | function TSimpleRestRequest.Put: iSimpleRestRequest; 194 | var 195 | FStreamResult : TStringStream; 196 | begin 197 | Result := Self; 198 | FStreamResult := TStringStream.Create; 199 | try 200 | if not Assigned(FStreamSend) then 201 | FStreamSend := TStringStream.Create; 202 | 203 | FIdHTTP.Put(FBaseURL, FStreamSend, FStreamResult); 204 | FReturn := FStreamResult.DataString; 205 | 206 | finally 207 | FStreamResult.Free; 208 | end; 209 | 210 | end; 211 | 212 | function TSimpleRestRequest.Return: String; 213 | begin 214 | Result := FReturn; 215 | end; 216 | 217 | function TSimpleRestRequest.StatusCode: Integer; 218 | begin 219 | Result := FIdHTTP.ResponseCode; 220 | end; 221 | 222 | function TSimpleRestRequest.UserAgent(aValue: String): iSimpleRestRequest; 223 | begin 224 | Result := Self; 225 | FIdHTTP.Request.UserAgent := aValue; 226 | end; 227 | 228 | function TSimpleRestRequest.Username(aValue: string): iSimpleRestRequest; 229 | begin 230 | Result := Self; 231 | FIdHTTP.Request.Username := aValue; 232 | FIdHTTP.Request.BasicAuthentication := IsBasicAuthentication; 233 | end; 234 | 235 | end. 236 | --------------------------------------------------------------------------------