private async Task InvokeRestMethod()

in src/Modules/Profile/Commands.Profile/InvokePowerBIRestMethod.cs [120:212]


        private async Task<HttpResult> InvokeRestMethod(string url, string body, PowerBIWebRequestMethod requestType)
        {
            // https://msdn.microsoft.com/en-us/library/mt243842.aspx
            // https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
            var token = this.Authenticator.Authenticate(this.Profile, this.Logger, this.Settings).Result;
            using (var client = new HttpClient())
            {
                this.PopulateClient(token, client);
                HttpResponseMessage response = null;
                if (string.IsNullOrEmpty(this.OutFile))
                {
                    switch (requestType)
                    {
                        case PowerBIWebRequestMethod.Get:
                            response = await client.GetAsync(url);
                            break;
                        case PowerBIWebRequestMethod.Post:
                            response = await client.PostAsync(url, new StringContent(body, Encoding.UTF8, this.ContentType));
                            break;
                        case PowerBIWebRequestMethod.Delete:
                            response = await client.DeleteAsync(url);
                            break;
                        case PowerBIWebRequestMethod.Put:
                            response = await client.PutAsync(url, new StringContent(body, Encoding.UTF8, this.ContentType));
                            break;
                        case PowerBIWebRequestMethod.Patch:
                            response = await client.PatchAsync(url, new StringContent(body, Encoding.UTF8, this.ContentType));
                            break;
                        case PowerBIWebRequestMethod.Options:
                            response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Options, url));
                            break;
                        default:
                            throw new NotSupportedException($"{nameof(requestType)} of value {requestType} is not supported");
                    }
                }
                else
                {
                    // Stream based, OutFile specified
                    HttpRequestMessage request = null;
                    switch (requestType)
                    {
                        case PowerBIWebRequestMethod.Get:
                            request = new HttpRequestMessage(HttpMethod.Get, url);
                            break;
                        case PowerBIWebRequestMethod.Post:
                            request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(body, Encoding.UTF8, this.ContentType) };
                            break;
                        case PowerBIWebRequestMethod.Delete:
                            request = new HttpRequestMessage(HttpMethod.Delete, url);
                            break;
                        case PowerBIWebRequestMethod.Put:
                            request = new HttpRequestMessage(HttpMethod.Put, url) { Content = new StringContent(body, Encoding.UTF8, this.ContentType) };
                            break;
                        case PowerBIWebRequestMethod.Patch:
                            request = new HttpRequestMessage(new HttpMethod("PATCH"), url) { Content = new StringContent(body, Encoding.UTF8, this.ContentType) };
                            break;
                        case PowerBIWebRequestMethod.Options:
                            request = new HttpRequestMessage(HttpMethod.Options, url);
                            break;
                        default:
                            throw new NotSupportedException($"{nameof(requestType)} of value {requestType} is not supported");
                    }

                    response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                }

                this.Logger.WriteVerbose($"Request Uri: {response.RequestMessage.RequestUri}");
                this.Logger.WriteVerbose($"Status Code: {response.StatusCode} ({(int)response.StatusCode})");

                response.EnsureSuccessStatusCode();

                // Need to stream results back before HttpClient is disposed
                var result = new HttpResult()
                {
                    ResponseMessage = response
                };

                if (string.IsNullOrEmpty(this.OutFile))
                {
                    result.Content = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    var stream = await response.Content.ReadAsStreamAsync();
                    var memoryStream = new MemoryStream();
                    await stream.CopyToAsync(memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    result.ContentStream = memoryStream;
                }

                return result;
            }
        }