コード例 #1
0
        public string Paste(PasteBinEntry entry, bool encodeData = true)
        {
            byte[] pastebinRawBytes;
            string data = string.Empty;

            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (string.IsNullOrEmpty(entry.Text))
            {
                throw new ArgumentException("The paste text must be set", "entry");
            }
            if (string.IsNullOrEmpty(_apiDevKey))
            {
                throw new ArgumentNullException("apiDevKey");
            }

            if (encodeData)
            {
                // Compress and Encode the Build data so it meets Pastebin's file limit
                pastebinRawBytes = Zip(entry.Text);
                data             = Convert.ToBase64String(pastebinRawBytes);
            }
            else
            {
                // Don't Compress and Encode the data (ie. it may be an error message and not Build data)
                data = entry.Text;
            }

            // Setup all parameters for the Pastebin call to create the Paste
            var parameters = GetBaseParameters();

            parameters[ApiParameters.Option]    = "paste";
            parameters[ApiParameters.PasteCode] = data;
            SetIfNotEmpty(parameters, ApiParameters.PasteName, entry.Title);
            SetIfNotEmpty(parameters, ApiParameters.PasteFormat, entry.Format);
            SetIfNotEmpty(parameters, ApiParameters.PastePrivate, entry.Private ? "1" : "0");
            SetIfNotEmpty(parameters, ApiParameters.PasteExpireDate, FormatExpireDate(entry.Expiration));
            SetIfNotEmpty(parameters, ApiParameters.UserKey, _apiUserKey);

            // Upload the data to Pastebin
            WebClient client = new WebClient();

            byte[] bytes = client.UploadValues(_apiPostUrl, parameters);
            string resp  = GetResponseText(bytes);

            if (resp.StartsWith("Bad API request"))
            {
                throw new PasteBinApiException(resp);
            }
            return(resp);
        }
コード例 #2
0
ファイル: Alerts.cs プロジェクト: MidwestSteeze/ChroniCalc
        public static void DisplayError(string message)
        {
            // Include information for sharing this Error, since Errors are something I'll want to know about and fix but don't want to throw an Exception and hard-stop the application
            const string ERROR_HEADER_OFFLINE = "An error has occurred.  Please post the following information to www.reddit.com/r/ChroniCalc:\r\n\r\n";
            const string ERROR_HEADER_ONLINE  = "An error has occurred.  Would you like to copy the error details link to your clipboard to send it to the developer?\r\n";

            DialogResult   dialogResult;
            string         pasteUrl;
            PasteBinClient client = new PasteBinClient(PasteBinClient.PBType.Error);

            // Post the error to Pastebin and provide the user a link to send instead of asking them to do screenshots
            // Setup the data for the Pastebin  //TODO enhance to include the Build data if it exists?
            var entry = new PasteBinEntry
            {
                Title      = "ChroniCalc Error",
                Text       = message,
                Expiration = PasteBinExpiration.OneMonth,
                Private    = false
            };

            try
            {
                // Call through the Pastebin API to get the URL
                pasteUrl = client.Paste(entry, false);

                // Display the message to the user and give them a chance to copy the Pastebin URL containing the call stack
                dialogResult = MessageBox.Show(string.Concat(ERROR_HEADER_ONLINE, Environment.NewLine, message, Environment.NewLine, pasteUrl), "ChroniCalc Error", MessageBoxButtons.YesNo);

                switch (dialogResult)
                {
                case DialogResult.Yes:
                    Clipboard.SetText(pasteUrl);
                    break;

                default:
                    break;
                }
            }
            catch (Exception)
            {
                // If an exception was returned from Pastebin, it's likely the user is not connected to the internet or Pastebin is down, so just show the message to them instead
                MessageBox.Show(string.Concat(ERROR_HEADER_OFFLINE, message), "ChroniCalc Error");
            }
        }
コード例 #3
0
        public EChroniCalcException(string message)
        {
            DialogResult   dialogResult;
            string         pasteUrl;
            PasteBinClient client = new PasteBinClient(PasteBinClient.PBType.Error);

            // Post the error to Pastebin and provide the user a link to send instead of asking them to do screenshots
            // Setup the data for the Pastebin  //TODO enhance to include the Build data if it exists?
            var entry = new PasteBinEntry
            {
                Title      = "ChroniCalc Exception",
                Text       = message,
                Expiration = PasteBinExpiration.OneMonth,
                Private    = false
            };

            try
            {
                // Call through the Pastebin API to get the URL
                pasteUrl = client.Paste(entry, false);

                // Display the message to the user and give them a chance to copy the Pastebin URL containing the call stack
                dialogResult = MessageBox.Show(string.Concat(ERROR_HEADER_ONLINE, Environment.NewLine, message, Environment.NewLine, pasteUrl), "ChroniCalc Exception", MessageBoxButtons.YesNo);

                switch (dialogResult)
                {
                case DialogResult.Yes:
                    Clipboard.SetText(pasteUrl);
                    break;

                default:
                    break;
                }
            }
            catch (Exception)
            {
                // If an exception was returned from Pastebin, it's likely the user is not connected to the internet or Pastebin is down, so just show the message to them instead
                MessageBox.Show(string.Concat(ERROR_HEADER_OFFLINE, message), "ChroniCalc Exception");
            }
        }
コード例 #4
0
        private void BtnPastebinShare_Click(object sender, EventArgs e)
        {
            // Generate a Pastebin URL of the current Build for the user to share
            string        buildAsText;
            string        pasteUrl;
            XmlSerializer serializer;
            var           client = new PasteBinClient(PasteBinClient.PBType.BuildShare);

            // Optional; will publish as a guest if not logged in; could enable this to see how many pastes people are using but
            //   this exposes username/password since it's on Github
            //client.Login(userName, password); //this'll set User Key

            serializer = new XmlSerializer((this.ParentForm as BuildShareForm).ParentForm.build.GetType());

            try
            {
                // Save the build to a string format
                using (StringWriter writer = new StringWriter())
                {
                    serializer.Serialize(writer, (this.ParentForm as BuildShareForm).ParentForm.build);
                    buildAsText = writer.ToString();
                }
            }
            catch (Exception ex)
            {
                // Display error that the build could not be serialized to be shared via Pastebin
                Alerts.DisplayError("PastebinShare:  Unable to serialize the build to be shared with Pastebin." + Environment.NewLine + ex.ToString());
                return;
            }

            // Setup the data for the Pastebin
            var entry = new PasteBinEntry
            {
                Title      = "ChroniCalc Build Share",
                Text       = buildAsText,
                Expiration = PasteBinExpiration.Never,
                Private    = false,
                Format     = "xml"
            };

            try
            {
                // Call through the Pastebin API to get the URL
                pasteUrl = client.Paste(entry);

                // Show the Pastebin URL in the textbox
                txtPastebinShare.Text = pasteUrl;
            }
            catch (System.Net.WebException)
            {
                MessageBox.Show("Unable to reach Pastebin.  This function is not currently available.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (PasteBinApiException ex)
            {
                MessageBox.Show("Unable to retrieve Build URL from Pastebin.  This function is not currently available." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                // Something else happened and we're not sure what, so provide error details to the user to pass along to me
                throw new EChroniCalcException("PasteBinShare: Unable to reach Pastebin." + Environment.NewLine + ex.Message);
            }
        }