Пример #1
0
        private async Task UpdateTokenHeaderIfNecessary(
            bool setControlsEnabled, bool focusPayloadTxtControl = false)
        {
            bool renewToken = true;

            if (this.tokenAcquiredTime != default)
            {
                TimeSpan age = DateTime.Now.Subtract(this.tokenAcquiredTime);
                renewToken = (age > TimeSpan.FromMinutes(42));
            }

            if ((config != null) && renewToken)
            {
                string clientId = this.config.ClientID;
                string secret   = this.config.Secret;
                string tenantId = this.config.TenantID;
                string scope    = $"{this.config.Scope}/.default";

                try
                {
                    if ((clientId != null) && (secret != null))
                    {
                        await this.GetAuthenticationHeader(
                            clientId, secret, tenantId, scope);
                    }
                    else
                    {
                        await this.GetAuthenticationHeader(
                            this.config.Scope,
                            "51f81489-12ee-4a9e-aaae-a2591f45987d",
                            this.config.Username,
                            this.config.Password,
                            tenantId
                            );
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"An Exception was thrown while acquiring the token.", Program.PROGRAM_TITLE,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    using (var pop = new TextViewerFrm("Exception Viewer", this.config, ex.ToString()))
                    {
                        pop.ShowDialog();

                        return;
                    }
                }
            }

            if (setControlsEnabled)
            {
                this.EnableControls(true);
            }

            if (focusPayloadTxtControl)
            {
                this.PayloadTxt.Focus();
            }
        }
Пример #2
0
 private void ShowResponeLbl_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(this.response))
     {
         using (var pop = new TextViewerFrm("Response Viewer", this.config, this.response))
         {
             pop.ShowDialog();
         }
     }
 }
Пример #3
0
        private void ShowTokenLbl_Click(object sender, EventArgs e)
        {
            if (this.tokenHeader != null)
            {
                string token = this.tokenHeader.ToString();

                using (var pop = new TextViewerFrm("Token Viewer", this.config, token))
                {
                    pop.ShowDialog();
                }
            }
        }
Пример #4
0
        private async void CallAPIBtn_Click(object sender, EventArgs e)
        {
            this.response = string.Empty;
            Uri uri = null;

            try
            {
                this.StatusStrip.BackColor = SystemColors.Control;
                this.EnableControls(false);
                this.StatusCodeLbl.Text         = STATUS_CODE_PREFIX;
                this.BytesReceivedLbl.Text      = BYTES_RECEIVED_PREFIX;
                this.ToolStripProgressBar.Style = ProgressBarStyle.Marquee;
                this.ToolStripProgressBar.MarqueeAnimationSpeed = 50;

                string txt = this.PreparePayload(this.PayloadTxt.Text);

                if (this.AutoFormatChk.Checked && ((string)this.MethodCmb.SelectedItem != "GET"))
                {
                    txt = JsonHelper.FormatIfJson(txt);
                }

                this.PayloadTxt.Text = txt;

                string path = $"{this.ControllerCmb.SelectedItem}/{this.EndpointCmb.SelectedItem}";
                if (!path.EndsWith("/"))
                {
                    path = $"{path}/";
                }
                uri = new Uri(new Uri($"{this.HostCmb.SelectedItem}"), path);
                string message = txt;

                await this.UpdateTokenHeaderIfNecessary(false);

                this.SetStatus($"Calling: {uri}");
                HttpResponseMessage response = await CallAsync(message, uri, this.tokenHeader);

                this.SetStatus($"Response received from: {uri}");

                this.StatusCodeLbl.Text = $"{STATUS_CODE_PREFIX} {response.StatusCode}";

                this.response = await response.Content.ReadAsStringAsync();

                this.BytesReceivedLbl.Text = $"{BYTES_RECEIVED_PREFIX} {this.response.Length}";

                this.StatusStrip.BackColor = response.IsSuccessStatusCode
                    ? Color.FromArgb(196, 255, 196)
                    : Color.FromArgb(255, 255, 196);
            }
            catch (Exception ex)
            {
                this.StatusStrip.BackColor = Color.LightPink;

                if (ex.InnerException is WebException webException)
                {
                    switch (webException.Status)
                    {
                    case WebExceptionStatus.ConnectFailure:
                    case WebExceptionStatus.ReceiveFailure:
                    case WebExceptionStatus.ConnectionClosed:
                    case WebExceptionStatus.KeepAliveFailure:
                    {
                        var inner = this.GetMostInnerException(ex);

                        this.SetStatus("EXCEPTION: " + inner.Message);

                        return;
                    }
                    }
                }

                using (var pop = new TextViewerFrm("Exception Viewer", this.config, ex.ToString()))
                {
                    pop.ShowDialog();
                }
                return;
            }
            finally
            {
                this.EnableControls(true);
                this.ToolStripProgressBar.Style = ProgressBarStyle.Blocks;
            }

            if (this.AutoOpenResponseChk.Checked && !string.IsNullOrEmpty(this.response))
            {
                using (var pop = new TextViewerFrm("Response Viewer", this.config, this.response))
                {
                    pop.ShowDialog();
                }
            }
        }