From 541ec847c31e243415908bb1d5091a785459aaf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 9 May 2024 14:50:25 +0200 Subject: [PATCH] Working Get -reading from api --- PostmanCloneLibrary/ApiAccess.cs | 47 +++++++++++ PostmanCloneLibrary/Class1.cs | 7 -- PostmanCloneLibrary/Enums.cs | 8 ++ PostmanCloneLibrary/IApiAccess.cs | 9 +++ PostmanCloneUI/Dashboard.Designer.cs | 113 ++++++++++++++++++++++++++- PostmanCloneUI/Dashboard.cs | 37 ++++++++- PostmanCloneUI/Dashboard.resx | 3 + 7 files changed, 211 insertions(+), 13 deletions(-) create mode 100644 PostmanCloneLibrary/ApiAccess.cs delete mode 100644 PostmanCloneLibrary/Class1.cs create mode 100644 PostmanCloneLibrary/Enums.cs create mode 100644 PostmanCloneLibrary/IApiAccess.cs diff --git a/PostmanCloneLibrary/ApiAccess.cs b/PostmanCloneLibrary/ApiAccess.cs new file mode 100644 index 0000000..81ffb77 --- /dev/null +++ b/PostmanCloneLibrary/ApiAccess.cs @@ -0,0 +1,47 @@ +using System.Text.Json; + +namespace PostmanCloneLibrary; + +public class ApiAccess : IApiAccess +{ + + private readonly HttpClient _httpClient = new(); + public async Task 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(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; + } +} diff --git a/PostmanCloneLibrary/Class1.cs b/PostmanCloneLibrary/Class1.cs deleted file mode 100644 index 9afebc1..0000000 --- a/PostmanCloneLibrary/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace PostmanCloneLibrary -{ - public class Class1 - { - - } -} diff --git a/PostmanCloneLibrary/Enums.cs b/PostmanCloneLibrary/Enums.cs new file mode 100644 index 0000000..4f7b84a --- /dev/null +++ b/PostmanCloneLibrary/Enums.cs @@ -0,0 +1,8 @@ + + +namespace PostmanCloneLibrary; + +public enum HttpAction +{ + GET +} diff --git a/PostmanCloneLibrary/IApiAccess.cs b/PostmanCloneLibrary/IApiAccess.cs new file mode 100644 index 0000000..ddfe5e3 --- /dev/null +++ b/PostmanCloneLibrary/IApiAccess.cs @@ -0,0 +1,9 @@ + +namespace PostmanCloneLibrary +{ + public interface IApiAccess + { + Task CallApiAsync(string url, bool formaOutput, HttpAction action); + bool IsValidUrl(string url); + } +} \ No newline at end of file diff --git a/PostmanCloneUI/Dashboard.Designer.cs b/PostmanCloneUI/Dashboard.Designer.cs index aca89a8..09e2b36 100644 --- a/PostmanCloneUI/Dashboard.Designer.cs +++ b/PostmanCloneUI/Dashboard.Designer.cs @@ -28,20 +28,129 @@ /// 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(); // + // 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 // AutoScaleDimensions = new SizeF(13F, 32F); 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); - Margin = new Padding(6, 6, 6, 6); + Margin = new Padding(6); Name = "Dashboard"; + StartPosition = FormStartPosition.CenterScreen; Text = "Postman Clone by Tim Corey"; + statusStrip.ResumeLayout(false); + statusStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #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; } } diff --git a/PostmanCloneUI/Dashboard.cs b/PostmanCloneUI/Dashboard.cs index 1a1c6db..9d0d74e 100644 --- a/PostmanCloneUI/Dashboard.cs +++ b/PostmanCloneUI/Dashboard.cs @@ -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(); + } + + 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) { - InitializeComponent(); + 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"; } } } diff --git a/PostmanCloneUI/Dashboard.resx b/PostmanCloneUI/Dashboard.resx index af32865..876ecdd 100644 --- a/PostmanCloneUI/Dashboard.resx +++ b/PostmanCloneUI/Dashboard.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file