Working Get -reading from api
This commit is contained in:
47
PostmanCloneLibrary/ApiAccess.cs
Normal file
47
PostmanCloneLibrary/ApiAccess.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PostmanCloneLibrary;
|
||||||
|
|
||||||
|
public class ApiAccess : IApiAccess
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly HttpClient _httpClient = new();
|
||||||
|
public async Task<string> CallApiAsync(
|
||||||
|
string url,
|
||||||
|
bool formaOutput = true,
|
||||||
|
HttpAction action = HttpAction.GET
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var response = await _httpClient.GetAsync(url);
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
string json = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
if (json != null && formaOutput)
|
||||||
|
{
|
||||||
|
var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
|
||||||
|
json = JsonSerializer.Serialize(jsonElement,
|
||||||
|
new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $"Error: {response.StatusCode}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsValidUrl(string url)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(url))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool output = Uri.TryCreate(url, UriKind.Absolute, out Uri uriOutput) &&
|
||||||
|
(uriOutput.Scheme == Uri.UriSchemeHttps);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +0,0 @@
|
|||||||
namespace PostmanCloneLibrary
|
|
||||||
{
|
|
||||||
public class Class1
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
8
PostmanCloneLibrary/Enums.cs
Normal file
8
PostmanCloneLibrary/Enums.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
namespace PostmanCloneLibrary;
|
||||||
|
|
||||||
|
public enum HttpAction
|
||||||
|
{
|
||||||
|
GET
|
||||||
|
}
|
||||||
9
PostmanCloneLibrary/IApiAccess.cs
Normal file
9
PostmanCloneLibrary/IApiAccess.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
namespace PostmanCloneLibrary
|
||||||
|
{
|
||||||
|
public interface IApiAccess
|
||||||
|
{
|
||||||
|
Task<string> CallApiAsync(string url, bool formaOutput, HttpAction action);
|
||||||
|
bool IsValidUrl(string url);
|
||||||
|
}
|
||||||
|
}
|
||||||
113
PostmanCloneUI/Dashboard.Designer.cs
generated
113
PostmanCloneUI/Dashboard.Designer.cs
generated
@ -28,20 +28,129 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
|
formHeader = new Label();
|
||||||
|
apiLabel = new Label();
|
||||||
|
apiText = new TextBox();
|
||||||
|
callApi = new Button();
|
||||||
|
resultsText = new TextBox();
|
||||||
|
statusStrip = new StatusStrip();
|
||||||
|
systemStatus = new ToolStripStatusLabel();
|
||||||
|
resultLabel = new Label();
|
||||||
|
statusStrip.SuspendLayout();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
|
// formHeader
|
||||||
|
//
|
||||||
|
formHeader.AutoSize = true;
|
||||||
|
formHeader.Font = new Font("Segoe UI", 26.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||||
|
formHeader.Location = new Point(31, 29);
|
||||||
|
formHeader.Name = "formHeader";
|
||||||
|
formHeader.Size = new Size(254, 47);
|
||||||
|
formHeader.TabIndex = 0;
|
||||||
|
formHeader.Text = "Postman Clone";
|
||||||
|
//
|
||||||
|
// apiLabel
|
||||||
|
//
|
||||||
|
apiLabel.AutoSize = true;
|
||||||
|
apiLabel.Location = new Point(36, 116);
|
||||||
|
apiLabel.Name = "apiLabel";
|
||||||
|
apiLabel.Size = new Size(53, 32);
|
||||||
|
apiLabel.TabIndex = 1;
|
||||||
|
apiLabel.Text = "API:";
|
||||||
|
//
|
||||||
|
// apiText
|
||||||
|
//
|
||||||
|
apiText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
||||||
|
apiText.BorderStyle = BorderStyle.FixedSingle;
|
||||||
|
apiText.Location = new Point(95, 113);
|
||||||
|
apiText.Name = "apiText";
|
||||||
|
apiText.Size = new Size(709, 39);
|
||||||
|
apiText.TabIndex = 2;
|
||||||
|
//
|
||||||
|
// callApi
|
||||||
|
//
|
||||||
|
callApi.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
|
||||||
|
callApi.Location = new Point(810, 111);
|
||||||
|
callApi.Name = "callApi";
|
||||||
|
callApi.Size = new Size(70, 42);
|
||||||
|
callApi.TabIndex = 3;
|
||||||
|
callApi.Text = "Go";
|
||||||
|
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;
|
||||||
|
statusStrip.Items.AddRange(new ToolStripItem[] { systemStatus });
|
||||||
|
statusStrip.Location = new Point(0, 521);
|
||||||
|
statusStrip.Name = "statusStrip";
|
||||||
|
statusStrip.Size = new Size(903, 30);
|
||||||
|
statusStrip.TabIndex = 5;
|
||||||
|
statusStrip.Text = "System Status";
|
||||||
|
//
|
||||||
|
// systemStatus
|
||||||
|
//
|
||||||
|
systemStatus.BackColor = Color.White;
|
||||||
|
systemStatus.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||||
|
systemStatus.Name = "systemStatus";
|
||||||
|
systemStatus.Size = new Size(62, 25);
|
||||||
|
systemStatus.Text = "Ready";
|
||||||
|
//
|
||||||
|
// resultLabel
|
||||||
|
//
|
||||||
|
resultLabel.AutoSize = true;
|
||||||
|
resultLabel.Location = new Point(36, 180);
|
||||||
|
resultLabel.Name = "resultLabel";
|
||||||
|
resultLabel.Size = new Size(88, 32);
|
||||||
|
resultLabel.TabIndex = 6;
|
||||||
|
resultLabel.Text = "Results";
|
||||||
|
//
|
||||||
// Dashboard
|
// Dashboard
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(13F, 32F);
|
AutoScaleDimensions = new SizeF(13F, 32F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(892, 466);
|
BackColor = Color.White;
|
||||||
|
ClientSize = new Size(903, 551);
|
||||||
|
Controls.Add(resultLabel);
|
||||||
|
Controls.Add(statusStrip);
|
||||||
|
Controls.Add(resultsText);
|
||||||
|
Controls.Add(callApi);
|
||||||
|
Controls.Add(apiText);
|
||||||
|
Controls.Add(apiLabel);
|
||||||
|
Controls.Add(formHeader);
|
||||||
Font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
Font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||||
Margin = new Padding(6, 6, 6, 6);
|
Margin = new Padding(6);
|
||||||
Name = "Dashboard";
|
Name = "Dashboard";
|
||||||
|
StartPosition = FormStartPosition.CenterScreen;
|
||||||
Text = "Postman Clone by Tim Corey";
|
Text = "Postman Clone by Tim Corey";
|
||||||
|
statusStrip.ResumeLayout(false);
|
||||||
|
statusStrip.PerformLayout();
|
||||||
ResumeLayout(false);
|
ResumeLayout(false);
|
||||||
|
PerformLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
private Label formHeader;
|
||||||
|
private Label apiLabel;
|
||||||
|
private TextBox apiText;
|
||||||
|
private Button callApi;
|
||||||
|
private TextBox resultsText;
|
||||||
|
private StatusStrip statusStrip;
|
||||||
|
private Label resultLabel;
|
||||||
|
private ToolStripStatusLabel systemStatus;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,39 @@
|
|||||||
namespace PostmanCloneUI
|
using PostmanCloneLibrary;
|
||||||
{
|
|
||||||
|
namespace PostmanCloneUI;
|
||||||
|
|
||||||
public partial class Dashboard : Form
|
public partial class Dashboard : Form
|
||||||
{
|
{
|
||||||
|
private readonly ApiAccess _apiAccess = new();
|
||||||
public Dashboard()
|
public Dashboard()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void callApi_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
systemStatus.Text = "Calling API...";
|
||||||
|
resultsText.Text = "";
|
||||||
|
// Validate the API URL
|
||||||
|
if (_apiAccess.IsValidUrl(apiText.Text) == false)
|
||||||
|
{
|
||||||
|
systemStatus.Text = "Invalid URL...";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
resultsText.Text = await _apiAccess.CallApiAsync(apiText.Text);
|
||||||
|
|
||||||
|
systemStatus.Text = "Ready";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
resultsText.Text = "Error: " + ex.Message;
|
||||||
|
systemStatus.Text = "Error";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -117,4 +117,7 @@
|
|||||||
<resheader name="writer">
|
<resheader name="writer">
|
||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</resheader>
|
||||||
|
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>17, 17</value>
|
||||||
|
</metadata>
|
||||||
</root>
|
</root>
|
||||||
Reference in New Issue
Block a user