Ejemplo n.º 1
0
        private void CallTheApi()
        {
            if (txtUrl.Text.HasValue() && !txtUrl.Text.EndsWith("/"))
            {
                txtUrl.Text += "/";
            }

            if (cboPath.Text.HasValue() && !cboPath.Text.StartsWith("/"))
            {
                cboPath.Text = "/" + cboPath.Text;
            }

            var context = new WebApiRequestContext
            {
                PublicKey         = txtPublicKey.Text,
                SecretKey         = txtSecretKey.Text,
                Url               = txtUrl.Text + (radioOdata.Checked ? "odata/" : "api/") + txtVersion.Text + cboPath.Text,
                HttpMethod        = cboMethod.Text,
                HttpAcceptType    = radioJson.Checked ? ApiConsumer.JsonAcceptType : ApiConsumer.XmlAcceptType,
                AdditionalHeaders = cboHeaders.Text
            };

            if (cboQuery.Text.HasValue())
            {
                context.Url = string.Format("{0}?{1}", context.Url, cboQuery.Text);
            }

            if (!context.IsValid)
            {
                "Please enter Public-Key, Secret-Key, URL and method.".Box(MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Debug.WriteLine(context.ToString());
                return;
            }

            var           apiConsumer    = new ApiConsumer();
            var           response       = new WebApiConsumerResponse();
            var           sb             = new StringBuilder();
            StringBuilder requestContent = null;
            Dictionary <string, object> multiPartData = null;

            lblRequest.Text = "Request: " + context.HttpMethod + " " + context.Url;
            lblRequest.Refresh();

            // Create multipart form data.
            if (cboFileUpload.Text.HasValue())
            {
                try
                {
                    var fileUploadModel = JsonConvert.DeserializeObject(cboFileUpload.Text, typeof(FileUploadModel)) as FileUploadModel;
                    multiPartData = apiConsumer.CreateMultipartData(fileUploadModel);
                }
                catch
                {
                    cboFileUpload.RemoveCurrent();
                    cboFileUpload.Text = string.Empty;
                    "File upload data is invalid.".Box(MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            var webRequest = apiConsumer.StartRequest(context, cboContent.Text, multiPartData, out requestContent);

            txtRequest.Text = requestContent.ToString();

            var result = apiConsumer.ProcessResponse(webRequest, response, folderBrowserDialog1);

            lblResponse.Text = "Response: " + response.Status;
            sb.Append(response.Headers);

            if (result && response.Content.HasValue())
            {
                if (radioJson.Checked && radioOdata.Checked)
                {
                    var customers = response.TryParseCustomers();
                    if (customers != null)
                    {
                        sb.AppendLine("Parsed {0} customer(s):".FormatInvariant(customers.Count));
                        customers.ForEach(x => sb.AppendLine(x.ToString()));
                        sb.Append("\r\n");
                    }
                }
            }

            sb.Append(response.Content);
            txtResponse.Text = sb.ToString();

            cboPath.InsertRolled(cboPath.Text, 64);
            cboQuery.InsertRolled(cboQuery.Text, 64);
            cboContent.InsertRolled(cboContent.Text, 64);
            cboHeaders.InsertRolled(cboHeaders.Text, 64);
            cboFileUpload.InsertRolled(cboFileUpload.Text, 64);
        }