/// <summary>
 /// Start the asynchronous communication with the API.
 /// </summary>
 public void StartAPI()
 {
     Task.Run(async() =>
     {
         try
         {
             if (DevFlags.LoggingEnabled)
             {
                 Logger.APILog("Start processing");
             }
             await ProcessingCycle(cancellationToken.Token);
         }
         catch (OperationCanceledException e)
         {
             //Ignore, just return
             if (DevFlags.LoggingEnabled)
             {
                 Logger.APILog("Processing task was cancelled");
             }
             Logger.APILog("Recieved " + ResponsesRecieved + " responses");
             Logger.APILog("Recieved " + ErrorResponses + " errors");
         }
         catch (Exception e)
         {
             Logger.Log("API_Helper - Error: " + e.Message);
         }
     });
 }
 /// <summary>
 /// Uses the <see cref="cancellationToken"/> to cancel all tasks.
 /// </summary>
 public void StopAPI()
 {
     if (!cancellationToken.IsCancellationRequested)
     {
         if (DevFlags.LoggingEnabled)
         {
             Logger.APILog("Cancelling Tasks");
         }
         cancellationToken.Cancel();
     }
 }
        /// <summary>
        /// Implementation of the actual communication with the API used by <see cref="ProcessingCycle(CancellationToken)"/>.
        /// </summary>
        /// <param name="currentModel">Data that will be sent to the API.</param>
        /// <param name="myClient">HttpClient that is used for the communication.</param>
        /// <param name="ct">Token that throws an OperationCancelledException when the token state is set to cancel. Used to stop the task.</param>
        /// <returns>Task[EssentiaModel], method can be run asynchronously. Actual return data is the EssentiaModel</returns>
        private async Task <EssentiaModel> DoAPICall(EssentiaModel currentModel, HttpClient myClient, CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            EssentiaModel apiModel = null;

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.RequestUri = new Uri(ApiAddress);
                request.Method     = HttpMethod.Post;
                request.Headers.Add("Accept", "application/json");
                var requestContent = JsonConvert.SerializeObject(currentModel);
                request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

                ct.ThrowIfCancellationRequested();
                HttpResponseMessage response = await myClient.SendAsync(request);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    ResponsesRecieved++;
                    HttpContent content = response.Content;
                    string      json    = await content.ReadAsStringAsync();

                    apiModel = JsonConvert.DeserializeObject <EssentiaModel>(json);
                    if (DevFlags.LoggingEnabled)
                    {
                        Logger.APILog("Response == OK -" + apiModel.chordData);
                    }
                }
                else if (DevFlags.LoggingEnabled)
                {
                    Logger.APILog("Response != OK"); ErrorResponses++;
                }
            }
            ct.ThrowIfCancellationRequested();
            return(apiModel);
        }