Explorar o código

Added support for PATCH, PUT, DELETE

Simon Grossenbacher %!s(int64=8) %!d(string=hai) anos
pai
achega
d2152c346f
Modificáronse 6 ficheiros con 182 adicións e 80 borrados
  1. 35 2
      Example Config.rdproj
  2. BIN=BIN
      RESTDebugger.exe
  3. 17 17
      prj/RESTDebugger.dproj
  4. 6 6
      prj/RESTDebugger.stat
  5. 23 14
      src/_frmMain.dfm
  6. 101 41
      src/_frmMain.pas

+ 35 - 2
Example Config.rdproj

@@ -1,7 +1,7 @@
 {
 	"Presets": {
-		"Presets": "GET: Simple GET\r\nGET: GET Extra Headers\r\nGET: Use Basic Auth Example\r\nGET: Example URL Parameters\r\nPOST: Json Post Data\r\n",
-		"LastUsed": 4
+		"Presets": "GET: Simple GET\r\nGET: GET Extra Headers\r\nGET: Use Basic Auth Example\r\nGET: Example URL Parameters\r\nPOST: Json Post Data\r\nDELETE: Delete Data\r\nPUT: Json Put Data\r\nPATCH: Json Patch Data\r\n",
+		"LastUsed": 5
 	},
 	"Preset_0": {
 		"BaseURL": "https://httpbin.org/get",
@@ -57,5 +57,38 @@
 		"UseBasicAuth": false,
 		"AuthUsername": "",
 		"AuthPassword": ""
+	},
+	"Preset_5": {
+		"BaseURL": "http://jsonplaceholder.typicode.com/posts/1",
+		"Method": 4,
+		"URLParams": "",
+		"PostData": "",
+		"ExtraHeaders": "",
+		"PostDataContentType": "application/json",
+		"UseBasicAuth": false,
+		"AuthUsername": "",
+		"AuthPassword": ""
+	},
+	"Preset_6": {
+		"BaseURL": "https://httpbin.org/put",
+		"Method": 3,
+		"URLParams": "",
+		"PostData": "{\r\n\t\"Demo\": \"Demo Data\"\r\n}",
+		"ExtraHeaders": "",
+		"PostDataContentType": "application/json",
+		"UseBasicAuth": false,
+		"AuthUsername": "",
+		"AuthPassword": ""
+	},
+	"Preset_7": {
+		"BaseURL": "https://httpbin.org/patch",
+		"Method": 2,
+		"URLParams": "",
+		"PostData": "{\r\n\t\"Demo\": \"Demo Data\"\r\n}",
+		"ExtraHeaders": "",
+		"PostDataContentType": "application/json",
+		"UseBasicAuth": false,
+		"AuthUsername": "",
+		"AuthPassword": ""
 	}
 }

BIN=BIN
RESTDebugger.exe


+ 17 - 17
prj/RESTDebugger.dproj

@@ -197,27 +197,12 @@
                         <Overwrite>true</Overwrite>
                     </Platform>
                 </DeployFile>
-                <DeployClass Name="DependencyModule">
-                    <Platform Name="Win32">
-                        <Operation>0</Operation>
-                        <Extensions>.dll;.bpl</Extensions>
-                    </Platform>
+                <DeployClass Name="ProjectiOSDeviceResourceRules">
                     <Platform Name="iOSDevice64">
                         <Operation>1</Operation>
-                        <Extensions>.dylib</Extensions>
-                    </Platform>
-                    <Platform Name="OSX32">
-                        <RemoteDir>Contents\MacOS</RemoteDir>
-                        <Operation>1</Operation>
-                        <Extensions>.dylib</Extensions>
                     </Platform>
                     <Platform Name="iOSDevice32">
                         <Operation>1</Operation>
-                        <Extensions>.dylib</Extensions>
-                    </Platform>
-                    <Platform Name="iOSSimulator">
-                        <Operation>1</Operation>
-                        <Extensions>.dylib</Extensions>
                     </Platform>
                 </DeployClass>
                 <DeployClass Name="ProjectOSXResource">
@@ -596,12 +581,27 @@
                         <Operation>1</Operation>
                     </Platform>
                 </DeployClass>
-                <DeployClass Name="ProjectiOSDeviceResourceRules">
+                <DeployClass Name="DependencyModule">
+                    <Platform Name="Win32">
+                        <Operation>0</Operation>
+                        <Extensions>.dll;.bpl</Extensions>
+                    </Platform>
                     <Platform Name="iOSDevice64">
                         <Operation>1</Operation>
+                        <Extensions>.dylib</Extensions>
+                    </Platform>
+                    <Platform Name="OSX32">
+                        <RemoteDir>Contents\MacOS</RemoteDir>
+                        <Operation>1</Operation>
+                        <Extensions>.dylib</Extensions>
                     </Platform>
                     <Platform Name="iOSDevice32">
                         <Operation>1</Operation>
+                        <Extensions>.dylib</Extensions>
+                    </Platform>
+                    <Platform Name="iOSSimulator">
+                        <Operation>1</Operation>
+                        <Extensions>.dylib</Extensions>
                     </Platform>
                 </DeployClass>
                 <ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>

+ 6 - 6
prj/RESTDebugger.stat

@@ -1,10 +1,10 @@
 [Stats]
-EditorSecs=9426
-DesignerSecs=2782
-InspectorSecs=1766
-CompileSecs=374269
-OtherSecs=3176
+EditorSecs=10677
+DesignerSecs=2966
+InspectorSecs=1781
+CompileSecs=404903
+OtherSecs=3131
 StartTime=27.10.2016 18:27:37
 RealKeys=0
 EffectiveKeys=0
-DebugSecs=6939
+DebugSecs=8114

+ 23 - 14
src/_frmMain.dfm

@@ -4,7 +4,7 @@ object frmMain: TfrmMain
   BorderIcons = [biSystemMenu, biMinimize]
   Caption = 'REST Debugger'
   ClientHeight = 930
-  ClientWidth = 731
+  ClientWidth = 725
   Color = clBtnFace
   Constraints.MinHeight = 930
   Constraints.MinWidth = 720
@@ -25,16 +25,17 @@ object frmMain: TfrmMain
   object Panel1: TPanel
     Left = 0
     Top = 0
-    Width = 731
+    Width = 725
     Height = 76
     Align = alTop
     BevelOuter = bvNone
     Color = clWhite
     ParentBackground = False
     TabOrder = 2
+    ExplicitWidth = 731
     object Label1: TLabel
       Left = 25
-      Top = 31
+      Top = 41
       Width = 86
       Height = 13
       Caption = 'REST Debugger'
@@ -46,8 +47,8 @@ object frmMain: TfrmMain
       ParentFont = False
     end
     object Image: TImage
-      Left = 955
-      Top = 24
+      Left = 682
+      Top = 33
       Width = 32
       Height = 32
       AutoSize = True
@@ -80,16 +81,17 @@ object frmMain: TfrmMain
     object dxBarDockControl1: TdxBarDockControl
       Left = 0
       Top = 0
-      Width = 731
+      Width = 725
       Height = 23
       Align = dalTop
       BarManager = dxBarManager
+      ExplicitWidth = 731
     end
   end
   object dxStatusBar: TdxStatusBar
     Left = 0
     Top = 910
-    Width = 731
+    Width = 725
     Height = 20
     Panels = <
       item
@@ -112,27 +114,30 @@ object frmMain: TfrmMain
     Font.Height = -11
     Font.Name = 'Tahoma'
     Font.Style = []
+    ExplicitWidth = 731
   end
   object cxPageControl1: TcxPageControl
     Left = 0
     Top = 448
-    Width = 731
+    Width = 725
     Height = 462
     Align = alClient
     TabOrder = 3
     Properties.ActivePage = tabContent
     Properties.CustomButtons.Buttons = <>
+    ExplicitWidth = 731
     ClientRectBottom = 460
     ClientRectLeft = 2
-    ClientRectRight = 729
+    ClientRectRight = 723
     ClientRectTop = 26
     object tabContent: TcxTabSheet
       Caption = 'Response Content'
       ImageIndex = 0
+      ExplicitWidth = 727
       object EditorContent: TBCEditor
         Left = 0
         Top = 0
-        Width = 727
+        Width = 721
         Height = 434
         Cursor = crIBeam
         ActiveLine.Indicator.Visible = False
@@ -222,15 +227,17 @@ object frmMain: TfrmMain
           FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00
           FF00}
         WordWrap.Indicator.MaskColor = clFuchsia
+        ExplicitWidth = 727
       end
     end
     object tabHeaders: TcxTabSheet
       Caption = 'Response Headers'
       ImageIndex = 1
+      ExplicitWidth = 727
       object EditorResponseHeaders: TBCEditor
         Left = 0
         Top = 0
-        Width = 727
+        Width = 721
         Height = 434
         Cursor = crIBeam
         ActiveLine.Indicator.Visible = False
@@ -319,17 +326,19 @@ object frmMain: TfrmMain
           FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00
           FF00}
         WordWrap.Indicator.MaskColor = clFuchsia
+        ExplicitWidth = 727
       end
     end
   end
   object Panel2: TPanel
     Left = 0
     Top = 76
-    Width = 731
+    Width = 725
     Height = 372
     Align = alTop
     BevelOuter = bvNone
     TabOrder = 0
+    ExplicitWidth = 731
     object Label2: TLabel
       Left = 8
       Top = 16
@@ -914,8 +923,8 @@ object frmMain: TfrmMain
     Request.Ranges.Units = 'bytes'
     Request.Ranges = <>
     HTTPOptions = []
-    Left = 20
-    Top = 30
+    Left = 75
+    Top = 65
   end
   object SSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL
     MaxLineAction = maException

+ 101 - 41
src/_frmMain.pas

@@ -14,6 +14,9 @@ uses
   cxButtons, BCEditor.Editor.Base, BCEditor.Editor, dxSkinscxPCPainter,
   dxBarBuiltInMenu, cxPC, System.ImageList, Vcl.ImgList, cxCheckBox;
 
+type
+  TMethodType = (mtGET, mtPOST, mtPATCH, mtPUT, mtDELETE);
+
 type
   TfrmMain = class(TForm)
     OpenDialog: TOpenDialog;
@@ -157,6 +160,9 @@ type
     procedure DeletePreset(const AID: Integer);
     procedure LoadPreset;
     procedure SavePreset;
+    procedure ShowError(const AErrorText: string);
+    procedure DoGETRequest(const AMethodType: TMethodType);
+    procedure DoPOSTRequest(const AMethodType: TMethodType);
   public
     { Public-Deklarationen }
   end;
@@ -329,7 +335,13 @@ end;
 
 procedure TfrmMain.comboMethodPropertiesChange(Sender: TObject);
 begin
-  tabPostData.TabVisible := comboMethod.ItemIndex = 1;
+  tabPostData.TabVisible := (comboMethod.ItemIndex = 1) or (comboMethod.ItemIndex = 2) or (comboMethod.ItemIndex = 3);
+
+  case comboMethod.ItemIndex of
+    1: tabPostData.Caption := 'Post Data';
+    2: tabPostData.Caption := 'Patch Data';
+    3: tabPostData.Caption := 'Put Data';
+  end;
 
   if (comboMethod.ItemIndex = 0) and (PageControlParams.ActivePage = nil) then
   begin
@@ -366,7 +378,7 @@ begin
           except
             on E: Exception do
             begin
-              MessageDlg(E.Message, mtError, [mbOK], 0);
+              ShowError(E.Message);
             end;
           end;
           FActiveEditor.Lines.Clear;
@@ -385,7 +397,7 @@ begin
           except
             on E: Exception do
             begin
-              MessageDlg(E.Message, mtError, [mbOK], 0);
+              ShowError(E.Message);
             end;
           end;
           FActiveEditor.Lines.Clear;
@@ -416,6 +428,64 @@ begin
   comboPresets.ItemIndex := comboPresets.Properties.Items.Count - 1;
 end;
 
+procedure TfrmMain.DoGETRequest(const AMethodType: TMethodType);
+var
+  dmy: string;
+begin
+  try
+    case AMethodType of
+      mtGET: dmy := HTTP.Get(edtFullURL.Text);
+      mtDELETE: dmy := HTTP.Delete(edtFullURL.Text);
+    end;
+
+    if HTTP.ResponseCode <> 200 then
+    begin
+      ShowError(HTTP.ResponseText);
+    end;
+  except
+    on E: Exception do
+    begin
+      ShowError(E.Message);
+    end;
+  end;
+
+  EditorContent.Lines.Text := dmy;
+  EditorResponseHeaders.Lines.Text := HTTP.Response.RawHeaders.Text;
+end;
+
+procedure TfrmMain.DoPOSTRequest(const AMethodType: TMethodType);
+var
+  dmy: string;
+  strstream: TStringStream;
+begin
+  strstream := TStringStream.Create(EditorPostData.Lines.Text, TEncoding.UTF8);
+  try
+    HTTP.Request.ContentType := comboPostContentType.Text;
+    try
+      case AMethodType of
+        mtPOST: dmy := HTTP.Post(edtFullURL.Text, strstream);
+        mtPATCH: dmy := HTTP.Patch(edtFullURL.Text, strstream);
+        mtPUT: dmy := HTTP.Put(edtFullURL.Text, strstream);
+      end;
+
+      if HTTP.ResponseCode <> 200 then
+      begin
+        ShowError(HTTP.ResponseText);
+      end;
+    except
+      on E: Exception do
+      begin
+        ShowError(E.Message);
+      end;
+    end;
+  finally
+    strstream.Free;
+  end;
+
+  EditorContent.Lines.Text := dmy;
+  EditorResponseHeaders.Lines.Text := HTTP.Response.RawHeaders.Text;
+end;
+
 procedure TfrmMain.dxBarButton6Click(Sender: TObject);
 begin
   Close;
@@ -597,7 +667,7 @@ end;
 
 procedure TfrmMain.FormResize(Sender: TObject);
 begin
-  Image.Left := Width - 70;
+  Image.Left := Width - 45;
 end;
 
 procedure TfrmMain.FormShow(Sender: TObject);
@@ -783,7 +853,7 @@ begin
           except
             on E: Exception do
             begin
-              MessageDlg(E.Message, mtError, [mbOK], 0);
+              ShowError(E.Message);
             end;
           end;
           FActiveEditor.Lines.Clear;
@@ -802,7 +872,7 @@ begin
           except
             on E: Exception do
             begin
-              MessageDlg(E.Message, mtError, [mbOK], 0);
+              ShowError(E.Message);
             end;
           end;
           FActiveEditor.Lines.Clear;
@@ -817,9 +887,7 @@ end;
 
 procedure TfrmMain.Run;
 var
-  dmy: string;
   tick: Cardinal;
-  strstream: TStringStream;
 begin
   EditorContent.Lines.Clear;
   EditorResponseHeaders.Lines.Clear;
@@ -846,45 +914,32 @@ begin
     if comboMethod.ItemIndex = 0 then
     begin
       // GET
-      try
-        dmy := HTTP.Get(edtFullURL.Text);
-        if HTTP.ResponseCode <> 200 then
-        begin
-          MessageDlg(HTTP.ResponseText, mtError, [mbOK], 0);
-        end;
-      except
-        on E: Exception do
-        begin
-          MessageDlg(E.Message, mtError, [mbOK], 0);
-        end;
-      end;
+      DoGETRequest(mtGET);
     end
     else
     if comboMethod.ItemIndex = 1 then
     begin
       // POST
-      strstream := TStringStream.Create(EditorPostData.Lines.Text, TEncoding.UTF8);
-      try
-        HTTP.Request.ContentType := comboPostContentType.Text;
-        try
-          dmy := HTTP.Post(edtFullURL.Text, strstream);
-          if HTTP.ResponseCode <> 200 then
-          begin
-            MessageDlg(HTTP.ResponseText, mtError, [mbOK], 0);
-          end;
-        except
-          on E: Exception do
-          begin
-            MessageDlg(E.Message, mtError, [mbOK], 0);
-          end;
-        end;
-      finally
-        strstream.Free;
-      end;
+      DoPOSTRequest(mtPOST);
+    end
+    else
+    if comboMethod.ItemIndex = 2 then
+    begin
+      // PATCH
+      DoPOSTRequest(mtPATCH);
+    end
+    else
+    if comboMethod.ItemIndex = 3 then
+    begin
+      // PUT
+      DoPOSTRequest(mtPUT);
+    end
+    else
+    if comboMethod.ItemIndex = 4 then
+    begin
+      // DELETE
+      DoGETRequest(mtDELETE);
     end;
-
-    EditorContent.Lines.Text := dmy;
-    EditorResponseHeaders.Lines.Text := HTTP.Response.RawHeaders.Text;
   finally
     dxStatusBar.Panels[1].Text := FormatFloat('Response Time: 0, ms', GetTickCount - tick);
     Screen.Cursor := crDefault;
@@ -946,4 +1001,9 @@ begin
   end;
 end;
 
+procedure TfrmMain.ShowError(const AErrorText: string);
+begin
+  MessageDlg(AErrorText, mtError, [mbOK], 0);
+end;
+
 end.