From c6db3d426b74fa75085f721fa39c9392abf6610b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 5 Jun 2024 11:34:55 +0200 Subject: [PATCH] GET and POST functioning --- PostmanCloneLibrary/ApiAccess.cs | 38 ++++++++- PostmanCloneLibrary/Enums.cs | 3 +- PostmanCloneLibrary/IApiAccess.cs | 4 +- PostmanCloneUI/Dashboard.Designer.cs | 118 ++++++++++++++++++++------- PostmanCloneUI/Dashboard.cs | 14 +++- 5 files changed, 140 insertions(+), 37 deletions(-) diff --git a/PostmanCloneLibrary/ApiAccess.cs b/PostmanCloneLibrary/ApiAccess.cs index 81ffb77..c87826b 100644 --- a/PostmanCloneLibrary/ApiAccess.cs +++ b/PostmanCloneLibrary/ApiAccess.cs @@ -1,4 +1,5 @@ -using System.Text.Json; +using System.Text; +using System.Text.Json; namespace PostmanCloneLibrary; @@ -6,13 +7,42 @@ public class ApiAccess : IApiAccess { private readonly HttpClient _httpClient = new(); + public async Task CallApiAsync( string url, - bool formaOutput = true, - HttpAction action = HttpAction.GET + string content, + HttpAction action = HttpAction.GET, + bool formaOutput = true + ) + { + StringContent stringContent = new(content, Encoding.UTF8, "application/json"); + return await CallApiAsync(url, stringContent, action, formaOutput); + } + public async Task CallApiAsync( + string url, + HttpContent? content = null, + HttpAction action = HttpAction.GET, + bool formaOutput = true ) { - var response = await _httpClient.GetAsync(url); + HttpResponseMessage response; + + switch (action) + { + case HttpAction.GET: + response = await _httpClient.GetAsync(url); + break; + case HttpAction.POST: + response = await _httpClient.PostAsync(url, content); + break; + default: + throw new ArgumentOutOfRangeException(nameof(action), action, null); + + } + + + //response = await _httpClient.GetAsync(url); + if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); diff --git a/PostmanCloneLibrary/Enums.cs b/PostmanCloneLibrary/Enums.cs index 4f7b84a..bc291c2 100644 --- a/PostmanCloneLibrary/Enums.cs +++ b/PostmanCloneLibrary/Enums.cs @@ -4,5 +4,6 @@ namespace PostmanCloneLibrary; public enum HttpAction { - GET + GET, + POST } diff --git a/PostmanCloneLibrary/IApiAccess.cs b/PostmanCloneLibrary/IApiAccess.cs index ddfe5e3..30463a4 100644 --- a/PostmanCloneLibrary/IApiAccess.cs +++ b/PostmanCloneLibrary/IApiAccess.cs @@ -1,9 +1,11 @@  + namespace PostmanCloneLibrary { public interface IApiAccess { - Task CallApiAsync(string url, bool formaOutput, HttpAction action); + Task CallApiAsync(string url, string content, HttpAction action = HttpAction.GET, bool formaOutput = true); + Task CallApiAsync(string url, HttpContent? content = null, HttpAction action = HttpAction.GET, bool formaOutput = true); bool IsValidUrl(string url); } } \ No newline at end of file diff --git a/PostmanCloneUI/Dashboard.Designer.cs b/PostmanCloneUI/Dashboard.Designer.cs index 09e2b36..4a34647 100644 --- a/PostmanCloneUI/Dashboard.Designer.cs +++ b/PostmanCloneUI/Dashboard.Designer.cs @@ -32,11 +32,18 @@ apiLabel = new Label(); apiText = new TextBox(); callApi = new Button(); - resultsText = new TextBox(); statusStrip = new StatusStrip(); systemStatus = new ToolStripStatusLabel(); - resultLabel = new Label(); + httpVerbSelection = new ComboBox(); + callData = new TabControl(); + bodyTab = new TabPage(); + resultsTab = new TabPage(); + resultsText = new TextBox(); + bodyText = new TextBox(); statusStrip.SuspendLayout(); + callData.SuspendLayout(); + bodyTab.SuspendLayout(); + resultsTab.SuspendLayout(); SuspendLayout(); // // formHeader @@ -62,9 +69,9 @@ // apiText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; apiText.BorderStyle = BorderStyle.FixedSingle; - apiText.Location = new Point(95, 113); + apiText.Location = new Point(210, 113); apiText.Name = "apiText"; - apiText.Size = new Size(709, 39); + apiText.Size = new Size(594, 39); apiText.TabIndex = 2; // // callApi @@ -78,19 +85,6 @@ callApi.UseVisualStyleBackColor = true; callApi.Click += callApi_Click; // - // resultsText - // - resultsText.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; - resultsText.BackColor = Color.White; - resultsText.BorderStyle = BorderStyle.FixedSingle; - resultsText.Location = new Point(36, 215); - resultsText.Multiline = true; - resultsText.Name = "resultsText"; - resultsText.ReadOnly = true; - resultsText.ScrollBars = ScrollBars.Both; - resultsText.Size = new Size(843, 240); - resultsText.TabIndex = 4; - // // statusStrip // statusStrip.BackColor = Color.White; @@ -109,14 +103,72 @@ systemStatus.Size = new Size(62, 25); systemStatus.Text = "Ready"; // - // resultLabel + // httpVerbSelection // - resultLabel.AutoSize = true; - resultLabel.Location = new Point(36, 180); - resultLabel.Name = "resultLabel"; - resultLabel.Size = new Size(88, 32); - resultLabel.TabIndex = 6; - resultLabel.Text = "Results"; + httpVerbSelection.DropDownStyle = ComboBoxStyle.DropDownList; + httpVerbSelection.FormattingEnabled = true; + httpVerbSelection.Items.AddRange(new object[] { "GET", "POST", "PUT", "DELETE" }); + httpVerbSelection.Location = new Point(95, 112); + httpVerbSelection.Name = "httpVerbSelection"; + httpVerbSelection.Size = new Size(109, 40); + httpVerbSelection.TabIndex = 7; + // + // callData + // + callData.Controls.Add(bodyTab); + callData.Controls.Add(resultsTab); + callData.Location = new Point(48, 182); + callData.Name = "callData"; + callData.SelectedIndex = 0; + callData.Size = new Size(832, 336); + callData.TabIndex = 8; + // + // bodyTab + // + bodyTab.Controls.Add(bodyText); + bodyTab.Location = new Point(4, 41); + bodyTab.Name = "bodyTab"; + bodyTab.Padding = new Padding(3); + bodyTab.Size = new Size(824, 291); + bodyTab.TabIndex = 0; + bodyTab.Text = "Body"; + bodyTab.UseVisualStyleBackColor = true; + // + // resultsTab + // + resultsTab.Controls.Add(resultsText); + resultsTab.Location = new Point(4, 41); + resultsTab.Name = "resultsTab"; + resultsTab.Padding = new Padding(3); + resultsTab.Size = new Size(824, 291); + resultsTab.TabIndex = 1; + resultsTab.Text = "Results"; + resultsTab.UseVisualStyleBackColor = true; + // + // resultsText + // + resultsText.BackColor = Color.White; + resultsText.BorderStyle = BorderStyle.FixedSingle; + resultsText.Dock = DockStyle.Fill; + resultsText.Location = new Point(3, 3); + resultsText.Multiline = true; + resultsText.Name = "resultsText"; + resultsText.ReadOnly = true; + resultsText.ScrollBars = ScrollBars.Both; + resultsText.Size = new Size(818, 285); + resultsText.TabIndex = 5; + // + // bodyText + // + bodyText.BackColor = Color.White; + bodyText.BorderStyle = BorderStyle.FixedSingle; + bodyText.Dock = DockStyle.Fill; + bodyText.Location = new Point(3, 3); + bodyText.Multiline = true; + bodyText.Name = "bodyText"; + bodyText.ScrollBars = ScrollBars.Both; + bodyText.Size = new Size(818, 285); + bodyText.TabIndex = 5; // // Dashboard // @@ -124,9 +176,9 @@ AutoScaleMode = AutoScaleMode.Font; BackColor = Color.White; ClientSize = new Size(903, 551); - Controls.Add(resultLabel); + Controls.Add(callData); + Controls.Add(httpVerbSelection); Controls.Add(statusStrip); - Controls.Add(resultsText); Controls.Add(callApi); Controls.Add(apiText); Controls.Add(apiLabel); @@ -138,6 +190,11 @@ Text = "Postman Clone by Tim Corey"; statusStrip.ResumeLayout(false); statusStrip.PerformLayout(); + callData.ResumeLayout(false); + bodyTab.ResumeLayout(false); + bodyTab.PerformLayout(); + resultsTab.ResumeLayout(false); + resultsTab.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -148,9 +205,14 @@ private Label apiLabel; private TextBox apiText; private Button callApi; - private TextBox resultsText; private StatusStrip statusStrip; - private Label resultLabel; private ToolStripStatusLabel systemStatus; + private ComboBox httpVerbSelection; + private TabControl callData; + private TabPage bodyTab; + private TabPage resultsTab; + private TabPage tabPage1; + private TextBox bodyText; + private TextBox resultsText; } } diff --git a/PostmanCloneUI/Dashboard.cs b/PostmanCloneUI/Dashboard.cs index 9d0d74e..8194c71 100644 --- a/PostmanCloneUI/Dashboard.cs +++ b/PostmanCloneUI/Dashboard.cs @@ -8,6 +8,7 @@ public partial class Dashboard : Form public Dashboard() { InitializeComponent(); + httpVerbSelection.SelectedItem = "GET"; } private async void callApi_Click(object sender, EventArgs e) @@ -21,11 +22,18 @@ public partial class Dashboard : Form return; } + HttpAction action; + if (Enum.TryParse(httpVerbSelection.SelectedItem!.ToString(), out action) == false) + { + systemStatus.Text = "Invalid Http Verb"; + return; + } + try { - - - resultsText.Text = await _apiAccess.CallApiAsync(apiText.Text); + resultsText.Text = await _apiAccess.CallApiAsync(apiText.Text, bodyText.Text, action); + callData.SelectedTab = resultsTab; + resultsTab.Focus(); systemStatus.Text = "Ready"; }