GET and POST functioning

This commit is contained in:
2024-06-05 11:34:55 +02:00
parent 004e9c87f9
commit c6db3d426b
5 changed files with 140 additions and 37 deletions

View File

@ -1,4 +1,5 @@
using System.Text.Json; using System.Text;
using System.Text.Json;
namespace PostmanCloneLibrary; namespace PostmanCloneLibrary;
@ -6,13 +7,42 @@ public class ApiAccess : IApiAccess
{ {
private readonly HttpClient _httpClient = new(); private readonly HttpClient _httpClient = new();
public async Task<string> CallApiAsync( public async Task<string> CallApiAsync(
string url, string url,
bool formaOutput = true, string content,
HttpAction action = HttpAction.GET HttpAction action = HttpAction.GET,
bool formaOutput = true
) )
{ {
var response = await _httpClient.GetAsync(url); StringContent stringContent = new(content, Encoding.UTF8, "application/json");
return await CallApiAsync(url, stringContent, action, formaOutput);
}
public async Task<string> CallApiAsync(
string url,
HttpContent? content = null,
HttpAction action = HttpAction.GET,
bool formaOutput = true
)
{
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) if (response.IsSuccessStatusCode)
{ {
string json = await response.Content.ReadAsStringAsync(); string json = await response.Content.ReadAsStringAsync();

View File

@ -4,5 +4,6 @@ namespace PostmanCloneLibrary;
public enum HttpAction public enum HttpAction
{ {
GET GET,
POST
} }

View File

@ -1,9 +1,11 @@
 
namespace PostmanCloneLibrary namespace PostmanCloneLibrary
{ {
public interface IApiAccess public interface IApiAccess
{ {
Task<string> CallApiAsync(string url, bool formaOutput, HttpAction action); Task<string> CallApiAsync(string url, string content, HttpAction action = HttpAction.GET, bool formaOutput = true);
Task<string> CallApiAsync(string url, HttpContent? content = null, HttpAction action = HttpAction.GET, bool formaOutput = true);
bool IsValidUrl(string url); bool IsValidUrl(string url);
} }
} }

View File

@ -32,11 +32,18 @@
apiLabel = new Label(); apiLabel = new Label();
apiText = new TextBox(); apiText = new TextBox();
callApi = new Button(); callApi = new Button();
resultsText = new TextBox();
statusStrip = new StatusStrip(); statusStrip = new StatusStrip();
systemStatus = new ToolStripStatusLabel(); 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(); statusStrip.SuspendLayout();
callData.SuspendLayout();
bodyTab.SuspendLayout();
resultsTab.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// formHeader // formHeader
@ -62,9 +69,9 @@
// //
apiText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; apiText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
apiText.BorderStyle = BorderStyle.FixedSingle; apiText.BorderStyle = BorderStyle.FixedSingle;
apiText.Location = new Point(95, 113); apiText.Location = new Point(210, 113);
apiText.Name = "apiText"; apiText.Name = "apiText";
apiText.Size = new Size(709, 39); apiText.Size = new Size(594, 39);
apiText.TabIndex = 2; apiText.TabIndex = 2;
// //
// callApi // callApi
@ -78,19 +85,6 @@
callApi.UseVisualStyleBackColor = true; callApi.UseVisualStyleBackColor = true;
callApi.Click += callApi_Click; 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
// //
statusStrip.BackColor = Color.White; statusStrip.BackColor = Color.White;
@ -109,14 +103,72 @@
systemStatus.Size = new Size(62, 25); systemStatus.Size = new Size(62, 25);
systemStatus.Text = "Ready"; systemStatus.Text = "Ready";
// //
// resultLabel // httpVerbSelection
// //
resultLabel.AutoSize = true; httpVerbSelection.DropDownStyle = ComboBoxStyle.DropDownList;
resultLabel.Location = new Point(36, 180); httpVerbSelection.FormattingEnabled = true;
resultLabel.Name = "resultLabel"; httpVerbSelection.Items.AddRange(new object[] { "GET", "POST", "PUT", "DELETE" });
resultLabel.Size = new Size(88, 32); httpVerbSelection.Location = new Point(95, 112);
resultLabel.TabIndex = 6; httpVerbSelection.Name = "httpVerbSelection";
resultLabel.Text = "Results"; 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 // Dashboard
// //
@ -124,9 +176,9 @@
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = Color.White; BackColor = Color.White;
ClientSize = new Size(903, 551); ClientSize = new Size(903, 551);
Controls.Add(resultLabel); Controls.Add(callData);
Controls.Add(httpVerbSelection);
Controls.Add(statusStrip); Controls.Add(statusStrip);
Controls.Add(resultsText);
Controls.Add(callApi); Controls.Add(callApi);
Controls.Add(apiText); Controls.Add(apiText);
Controls.Add(apiLabel); Controls.Add(apiLabel);
@ -138,6 +190,11 @@
Text = "Postman Clone by Tim Corey"; Text = "Postman Clone by Tim Corey";
statusStrip.ResumeLayout(false); statusStrip.ResumeLayout(false);
statusStrip.PerformLayout(); statusStrip.PerformLayout();
callData.ResumeLayout(false);
bodyTab.ResumeLayout(false);
bodyTab.PerformLayout();
resultsTab.ResumeLayout(false);
resultsTab.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
@ -148,9 +205,14 @@
private Label apiLabel; private Label apiLabel;
private TextBox apiText; private TextBox apiText;
private Button callApi; private Button callApi;
private TextBox resultsText;
private StatusStrip statusStrip; private StatusStrip statusStrip;
private Label resultLabel;
private ToolStripStatusLabel systemStatus; private ToolStripStatusLabel systemStatus;
private ComboBox httpVerbSelection;
private TabControl callData;
private TabPage bodyTab;
private TabPage resultsTab;
private TabPage tabPage1;
private TextBox bodyText;
private TextBox resultsText;
} }
} }

View File

@ -8,6 +8,7 @@ public partial class Dashboard : Form
public Dashboard() public Dashboard()
{ {
InitializeComponent(); InitializeComponent();
httpVerbSelection.SelectedItem = "GET";
} }
private async void callApi_Click(object sender, EventArgs e) private async void callApi_Click(object sender, EventArgs e)
@ -21,11 +22,18 @@ public partial class Dashboard : Form
return; return;
} }
HttpAction action;
if (Enum.TryParse(httpVerbSelection.SelectedItem!.ToString(), out action) == false)
{
systemStatus.Text = "Invalid Http Verb";
return;
}
try try
{ {
resultsText.Text = await _apiAccess.CallApiAsync(apiText.Text, bodyText.Text, action);
callData.SelectedTab = resultsTab;
resultsText.Text = await _apiAccess.CallApiAsync(apiText.Text); resultsTab.Focus();
systemStatus.Text = "Ready"; systemStatus.Text = "Ready";
} }