Skip to content

Instantly share code, notes, and snippets.

@ghuntley
Created November 25, 2014 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghuntley/c2cc22530f3f7d077db9 to your computer and use it in GitHub Desktop.
Save ghuntley/c2cc22530f3f7d077db9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using FloJoCore.Helpers;
using FloJoCore.Models;
using Fusillade;
using Refit;
namespace FloJoCore
{
public interface ISlackApi
{
[Post("/api/auth.start")]
Task<AvailableTeamList> GetTeamsForUserRaw(
[Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, string> form);
[Post("/api/auth.signin")]
Task<AuthenticationResult> LoginRaw([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, string> form);
[Post("/api/channels.list")]
Task<ChannelList> GetChannelsRaw([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, string> form);
[Post("/api/channels.history")]
Task<MessageListForRoom> GetMessagesForChannelRaw(
[Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, string> form);
}
public class SlackApi
{
public static readonly string ApiBaseAddress = "https://slack.com";
private static readonly Lazy<ISlackApi> background;
private static readonly Lazy<ISlackApi> userInitiated;
private static readonly Lazy<ISlackApi> speculative;
static SlackApi()
{
Func<HttpMessageHandler, ISlackApi> createClient = messageHandler =>
{
Ensure.ArgumentIsOfType(messageHandler, typeof(NetCache), "Fusillade HttpMessageHandler");
var client = new HttpClient(messageHandler)
{
BaseAddress = new Uri(ApiBaseAddress)
};
return RestService.For<ISlackApi>(client);
};
background = new Lazy<ISlackApi>(() => createClient.Invoke(NetCache.Background));
userInitiated = new Lazy<ISlackApi>(() => createClient.Invoke(NetCache.UserInitiated));
speculative = new Lazy<ISlackApi>(() => createClient.Invoke(NetCache.Speculative));
}
public static ISlackApi Background
{
get { return background.Value; }
}
public static ISlackApi UserInitiated
{
get { return userInitiated.Value; }
}
public static ISlackApi Speculative
{
get { return speculative.Value; }
}
}
public static class SlackApiExtensions
{
public static Task<AvailableTeamList> GetTeamsForUser(this ISlackApi This, string email)
{
return This.GetTeamsForUserRaw(new Dictionary<string, string> {{"email", email}});
}
public static Task<AuthenticationResult> Login(this ISlackApi This, string userId, string teamId,
string password)
{
var dict = new Dictionary<string, string>
{
{"user", userId},
{"team", teamId},
{"password", password},
};
return This.LoginRaw(dict);
}
public static async Task<ChannelList> GetChannels(this ISlackApi This, string token)
{
var dict = new Dictionary<string, string>
{
{"token", token},
{"exclude_archived", "1"},
};
ChannelList ret = await This.GetChannelsRaw(dict);
if (!ret.ok || ret.channels == null || ret.channels.Count == 0)
{
// NB: Slack guarantees that there is always at least one
// channel
throw new Exception("Retrieving channels failed");
}
return ret;
}
public static async Task<MessageListForRoom> GetLatestMessages(this ISlackApi This, string token,
string channelId)
{
var dict = new Dictionary<string, string>
{
{"token", token},
{"channel", channelId},
};
MessageListForRoom ret = await This.GetMessagesForChannelRaw(dict);
if (!ret.ok || ret.messages == null)
{
throw new Exception("Can't load messages for " + channelId);
}
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment