Example #1
0
        private async void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                btnTest.Text = "Executing...";
                if (string.IsNullOrWhiteSpace(tbHeaders.Text))
                {
                    tbHeaders.Text = "{ }";
                }

                JObject response = null;
                switch (cbType.SelectedItem.ToString())
                {
                case "GET": response = await BLIO.HttpRequest("GET", tbUrl.Text, tbHeaders.Text);

                    break;

                case "POST": response = await BLIO.HttpRequest("POST", tbUrl.Text, tbHeaders.Text, tbBody.Text);

                    break;
                }
                if (response != null)
                {
                    btnTest.Text = "Test";
                    string message = "API Connection: Success\r\n\r\n";
                    int    count   = 1;
                    try
                    {
                        foreach (var cond in Conditions)
                        {
                            HttpCondition condit = new HttpCondition(cond, response);
                            message += "Condition " + count + ": " + condit.Evaluate() + "\r\n";

                            count++;
                        }
                    }
                    catch { }
                    MaterialRemindMeBox.Show(message);
                }
            }
            catch (Exception ex)
            {
                MaterialRemindMeBox.Show("Failure! " + ex.Message);
            }
            finally
            {
                btnTest.Text = "Test";
            }
        }
Example #2
0
        private async void ExecuteHttpRequest(object sender, EventArgs e, HttpRequests http, Reminder rem)
        {
            try
            {
                if (!BLIO.HasInternetAccess())
                {
                    BLIO.Log("Cancelling ExecuteHttpRequest(). No internet access");
                    return;
                }

                BLIO.Log("ExecuteHttpRequest timer tick! [ " + http.URL + " ]");
                if (httpTimers.Where(t => t.Key.Id == rem.Id) == null)
                {
                    BLIO.Log("Attempted to ExecuteHttpRequest() from a timer that no longer exists. Cancelling.");
                    return;
                }

                JObject response = await BLIO.HttpRequest(http.Type, http.URL, http.OtherHeaders, http.AcceptHeader, http.ContentTypeHeader, http.Body);



                List <HttpCondition> conditions = new List <HttpCondition>();
                foreach (HttpRequestCondition cond in BLLocalDatabase.HttpRequestConditions.GetConditions(http.Id))
                {
                    conditions.Add(new HttpCondition(cond, response));
                }

                bool conditionMet = conditions.Count > 0;
                foreach (HttpCondition con in conditions) //Check for ALL conditions and see if all of them return true
                {
                    if (!con.Evaluate())
                    {
                        conditionMet = false;
                    }
                }

                if (conditionMet)
                {
                    //All conditions returned true!

                    MakeReminderPopup(rem);

                    if (http.AfterPopup == "Stop")
                    {
                        var timer = GetTimer(rem);
                        if (timer != null)
                        {
                            timer.Stop();

                            //remove from dictionary
                            httpTimers.Remove(rem);
                        }
                    }
                }
                else
                {
                    BLIO.Log("ExecuteHttpRequest returned FALSE");
                }
            }
            catch (Exception ex)
            {
                BLIO.Log("ExecuteHttpRequest() Failed. " + ex.GetType().ToString());
            }
        }
Example #3
0
        /// <summary>
        /// Transforms the part of the textbox string "API{url,data}" to the data from the API
        /// </summary>
        /// <param name="reminderText">The reminder text</param>
        /// <returns>The reminder text with the API{} replaced with the actual API value</returns>
        private async void TransformAPITextToValue(string color, string reminderText, float previewSize = 0)
        {
            float size = previewSize == 0 ? BLLocalDatabase.PopupDimension.GetPopupDimensions().FontNoteSize : previewSize;

            if (!reminderText.Contains("API{"))
            {
                return;
            }

            try
            {
                int retryCount = 0;
                htmlLblText.Text = "<p style=\"color: " + color + "; font-size: " + Math.Round(size * 1.28) + "px;\">Loading...</p>";
                new Thread(async() =>
                {
                    startMethod:



                    try
                    {
                        //if !hasinternetaccess Thread.Sleep(250); , 8 times, then error

                        if (!BLIO.HasInternetAccess())
                        {
                            retryCount++;

                            if (retryCount <= 8)
                            {
                                Thread.Sleep(250);
                                goto startMethod;
                            }
                            else
                            {
                                htmlLblText.Invoke((MethodInvoker)(() =>
                                {
                                    htmlLblText.Text = "An error occured." + rem.Note;
                                }));

                                return;
                            }
                        }

                        retryCount = 0;

                        //Interner access!

                        int startIndex = reminderText.IndexOf("API{");
                        int endIndex   = -1;

                        bool found = false;
                        int count  = 1;
                        while (!found)
                        {
                            if (reminderText[startIndex + count] == '}')
                            {
                                endIndex = startIndex + count;
                                found    = true;
                            }
                            else
                            {
                                count++;
                            }
                        }

                        //[url, dataToPick]
                        string[] data    = (reminderText.Substring(startIndex + 4, endIndex - (startIndex + 4))).Split(',');
                        JObject response = await BLIO.HttpRequest("GET", data[0]);

                        //This is the API value the user is requesting. Replace API{url,data} with this.
                        string value = response.SelectTokens(data[1]).Select(t => t.Value <string>()).ToList()[0];

                        StringBuilder stringBuilder = new StringBuilder(reminderText);
                        stringBuilder.Remove(startIndex, endIndex - (startIndex) + 1);
                        stringBuilder.Insert(startIndex, value);
                        reminderText = stringBuilder.ToString();

                        //TODO if response.status != 200   stringBuilder.Insert(startIndex, "Error occured");
                        //Bitcoin: $60,001
                        //Superfarm: Error
                        //Reef: $0,04
                        //

                        //Still contains another API{} ? again...
                        if (reminderText.Contains("API{"))
                        {
                            goto startMethod;
                        }

                        htmlLblText.Invoke((MethodInvoker)(() =>
                        {
                            htmlLblText.Text = "<p style=\"color: " + color + "; font-size: " + Math.Round(size * 1.28) + "px;\">" + reminderText + "</p>";
                        }));
                        //return reminderText;
                    }
                    catch (Exception ex)
                    {
                        retryCount++;

                        if (retryCount <= 8)
                        {
                            Thread.Sleep(250);
                            goto startMethod;
                        }
                        else
                        {
                            htmlLblText.Text = "An error occured. (" + ex.GetType().ToString() + ")\r\n" + rem.Note;
                        }
                    }
                }).Start();
            }
            catch (Exception ex)
            {
                BLIO.WriteError(ex, "TransformAPITextToValue() failed with " + ex.GetType().ToString());
                //return reminderText;
            }
        }