GET and POST functioning
This commit is contained in:
@ -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<string> CallApiAsync(
|
||||
string url,
|
||||
bool formaOutput = true,
|
||||
HttpAction action = HttpAction.GET
|
||||
string content,
|
||||
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)
|
||||
{
|
||||
string json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
@ -4,5 +4,6 @@ namespace PostmanCloneLibrary;
|
||||
|
||||
public enum HttpAction
|
||||
{
|
||||
GET
|
||||
GET,
|
||||
POST
|
||||
}
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
|
||||
|
||||
namespace PostmanCloneLibrary
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
118
PostmanCloneUI/Dashboard.Designer.cs
generated
118
PostmanCloneUI/Dashboard.Designer.cs
generated
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user