public async Task <Channel> CreateChannelAsync(GenesysAuthTokenInfo authToken)
        {
            var path    = "/api/v2/notifications/channels";
            var request = new HttpRequestMessage(HttpMethod.Post, $"https://api.{_config.Environment}" + path);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken.AccessToken);
            var response = await _http.SendAsync(request);

            try
            {
                response.EnsureSuccessStatusCode();
                var responseContent = await response.Content.ReadAsStreamAsync();

                var channel = await JsonSerializer.DeserializeAsync <Channel>(responseContent);

                return(channel);
            }
            catch (HttpRequestException ex)
            {
                int statusCode = (int)response.StatusCode;
                throw new Exception($"Genesys API Error CreateChanngel. Status Code {statusCode}.", ex);
            }
            catch (Exception ex)
            {
                throw new Exception($"CreateChanngel Error", ex);
            }
        }
        /// <summary>
        /// The list of existing channels
        /// </summary>
        /// <param name="authToken"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task <Channel[]> GetChannelsAsync(GenesysAuthTokenInfo authToken, GetChanngelQuery query)
        {
            var path    = $"/api/v2/notifications/channels?includechannels={query}";
            var request = new HttpRequestMessage(HttpMethod.Get, $"https://api.{_config.Environment}" + path);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken.AccessToken);
            var response = await _http.SendAsync(request);

            try
            {
                response.EnsureSuccessStatusCode();
                var responseContent = await response.Content.ReadAsStreamAsync();

                var channels = await JsonSerializer.DeserializeAsync <ChannelsList>(responseContent);

                return(channels?.Items ?? new Channel[] { });
            }
            catch (HttpRequestException ex)
            {
                int statusCode = (int)response.StatusCode;
                throw new Exception($"Genesys API Error GetChanngelAsync. Status Code {statusCode}.", ex);
            }
            catch (Exception ex)
            {
                throw new Exception($"GetChanngelAsync Error", ex);
            }
        }
        public async Task CreateSubscriptionsAsync(GenesysAuthTokenInfo authToken, Channel channel, IEnumerable <string> topics)
        {
            var path    = $"/api/v2/notifications/channels/{channel.Id}/subscriptions";
            var request = new HttpRequestMessage(HttpMethod.Post, $"https://api.{_config.Environment}" + path);
            var data    = string.Join(",", topics.Select(t => "{" + $"\"id\":\"{t}\"" + "}").ToArray()).TrimEnd(',');
            var body    = $"[{data}]";

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken.AccessToken);
            request.Content = new StringContent(body, Encoding.UTF8, "application/json");
            var response = await _http.SendAsync(request);

            response.EnsureSuccessStatusCode();
        }
 public static async Task <Channel> GetOrCreateChannelAsync(this GenesysHttpClient _http, GenesysAuthTokenInfo authToken)
 => await GetLastChanngelAsync(_http, authToken, GetChanngelQuery.OAuthClient)
 ?? await _http.CreateChannelAsync(authToken);
 public static async Task <Channel> GetLastChanngelAsync(this GenesysHttpClient _http, GenesysAuthTokenInfo authToken, GetChanngelQuery query)
 => (await _http.GetChannelsAsync(authToken, query))
 .Where(ch => ch.Expires.HasValue).OrderByDescending(ch => ch.Expires).FirstOrDefault();