<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Taut.Authorizations;

namespace Taut.Channels
{
    public class ChannelService : BaseAuthenticatedApiService, IChannelService
    {
        private const string INFO_METHOD = "channels.info";
        private const string LIST_METHOD = "channels.list";

        public ChannelService(IUserCredentialService userCredentialService)
            : base(userCredentialService) { }

        public IObservable<ChannelInfoResponse> Info(string channelId)
        {
            channelId.ThrowIfNull("channelId");

            return Observable.Create<ChannelInfoResponse>(async (observer, cancellationToken) =>
            {
                var requestUrl = BuildRequestUrl(INFO_METHOD, new { channel = channelId });
                observer.OnNext(await GetResponseAsync<ChannelInfoResponse>(requestUrl, cancellationToken));
                observer.OnCompleted();
                return Disposable.Empty;
            });
        }

        public IObservable<ChannelListResponse> List(bool excludeArchived = false)
        {
            return Observable.Create<ChannelListResponse>(async (observer, cancellationToken) =>
            {
                var requestUrl = BuildRequestUrl(LIST_METHOD,
                    new
                    {
                        exclude_archived = Convert.ToInt32(excludeArchived),
                    });
                observer.OnNext(await GetResponseAsync<ChannelListResponse>(requestUrl, cancellationToken));
                observer.OnCompleted();
                return Disposable.Empty;
            });
        }
    }
}<commit_msg>Refactored to share code between similar API calls. This should work for most/all GET requests.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Taut.Authorizations;

namespace Taut.Channels
{
    public class ChannelService : BaseAuthenticatedApiService, IChannelService
    {
        private const string INFO_METHOD = "channels.info";
        private const string LIST_METHOD = "channels.list";

        public ChannelService(IUserCredentialService userCredentialService)
            : base(userCredentialService) { }

        public IObservable<ChannelInfoResponse> Info(string channelId)
        {
            channelId.ThrowIfNull("channelId");

            return ApiCallHelper(INFO_METHOD,
                    new { channel = channelId },
                    async (requestUrl, cancellationToken) => await GetResponseAsync<ChannelInfoResponse>(requestUrl, cancellationToken));
        }

        public IObservable<ChannelListResponse> List(bool excludeArchived = false)
        {
            return ApiCallHelper(LIST_METHOD,
                    new { exclude_archived = Convert.ToInt32(excludeArchived) },
                    async (requestUrl, cancellationToken) => await GetResponseAsync<ChannelListResponse>(requestUrl, cancellationToken));
        }

        private IObservable<T> ApiCallHelper<T>(string method, object queryParams, Func<string, CancellationToken, Task<T>> httpCall)
        {
            return Observable.Create<T>(async (observer, cancellationToken) =>
            {
                var requestUrl = BuildRequestUrl(method, queryParams);
                observer.OnNext(await httpCall.Invoke(requestUrl, cancellationToken));
                observer.OnCompleted();
                return Disposable.Empty;
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.08057630736392743, 'fraction_numerical': 0.0010672358591248667, 'mean_word_length': 2.6187258687258685, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1858340', 'n_tokens_mistral': 916, 'n_tokens_neox': 891, 'n_words': 190}