diff --git a/SlackAPI.Tests/Conversations.cs b/SlackAPI.Tests/Conversations.cs new file mode 100644 index 00000000..3c8b74dc --- /dev/null +++ b/SlackAPI.Tests/Conversations.cs @@ -0,0 +1,41 @@ +using System.Linq; +using SlackAPI.RPCMessages; +using SlackAPI.Tests.Configuration; +using SlackAPI.Tests.Helpers; +using Xunit; + +namespace SlackAPI.Tests +{ + [Collection("Integration tests")] + public class Conversations + { + private readonly IntegrationFixture fixture; + + public Conversations(IntegrationFixture fixture) + { + this.fixture = fixture; + } + [Fact] + public void ConversationList() + { + var client = this.fixture.UserClient; + ConversationsListResponse actual = null; + using (var sync = new InSync(nameof(SlackClient.ChannelLookup))) + { + client.GetConversationsList(response => + { + actual = response; + sync.Proceed(); + }); + } + + Assert.True(actual.ok, "Error while fetching conversation list."); + Assert.True(actual.channels.Any()); + + // check to null + var someChannel = actual.channels.First(); + Assert.NotNull(someChannel.id); + Assert.NotNull(someChannel.name); + } + } +} \ No newline at end of file diff --git a/SlackAPI.Tests/UserUIInteraction.cs b/SlackAPI.Tests/UserUIInteraction.cs index 60b7faf8..0ece63cf 100644 --- a/SlackAPI.Tests/UserUIInteraction.cs +++ b/SlackAPI.Tests/UserUIInteraction.cs @@ -52,7 +52,7 @@ public void TestGetAccessToken() var accessTokenResponse = GetAccessToken(slackClientHelpers, clientId, clientSecret, redirectUrl, code); Assert.True(accessTokenResponse.ok); - Assert.Equal("identify", accessTokenResponse.scope); + Assert.Contains("identify", accessTokenResponse.scope); } } diff --git a/SlackAPI/Channel.cs b/SlackAPI/Channel.cs index 5d058198..c45d1c23 100644 --- a/SlackAPI/Channel.cs +++ b/SlackAPI/Channel.cs @@ -10,12 +10,14 @@ public class Channel : Conversation { public string name; public string creator; + public string user; public bool is_archived; public bool is_member; public bool is_general; public bool is_channel; public bool is_group; + public bool is_im; //Is this deprecated by is_open? public bool IsPrivateGroup { get { return id != null && id[0] == 'G'; } } @@ -24,9 +26,5 @@ public class Channel : Conversation public OwnedStampedMessage purpose; public string[] members; - - //im related properties - public bool is_im; - public string user; } } diff --git a/SlackAPI/SlackClient.cs b/SlackAPI/SlackClient.cs index 2d7f2604..a0762e9b 100644 --- a/SlackAPI/SlackClient.cs +++ b/SlackAPI/SlackClient.cs @@ -108,14 +108,7 @@ protected virtual void Connected(LoginResponse loginDetails) public void APIRequestWithToken(Action callback, params Tuple[] getParameters) where K : Response { - Tuple[] tokenArray = new Tuple[]{ - new Tuple("token", APIToken) - }; - - if (getParameters != null && getParameters.Length > 0) - tokenArray = tokenArray.Concat(getParameters).ToArray(); - - APIRequest(callback, tokenArray, new Tuple[0]); + APIRequest(callback, getParameters, new Tuple[0], APIToken); } public void TestAuth(Action callback) @@ -872,7 +865,6 @@ public void UploadFile(Action callback, byte[] fileData, str Uri target = new Uri(Path.Combine(APIBaseLocation, useAsync ? "files.uploadAsync" : "files.upload")); List parameters = new List(); - parameters.Add(string.Format("token={0}", APIToken)); //File/Content if (!string.IsNullOrEmpty(fileType)) @@ -892,7 +884,7 @@ public void UploadFile(Action callback, byte[] fileData, str using (MultipartFormDataContent form = new MultipartFormDataContent()) { form.Add(new ByteArrayContent(fileData), "file", fileName); - HttpResponseMessage response = PostRequest(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form); + HttpResponseMessage response = PostRequestAsync(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form, APIToken).Result; string result = response.Content.ReadAsStringAsync().Result; callback(result.Deserialize()); } diff --git a/SlackAPI/SlackClientBase.cs b/SlackAPI/SlackClientBase.cs index 5b2f08f4..1e6d357b 100644 --- a/SlackAPI/SlackClientBase.cs +++ b/SlackAPI/SlackClientBase.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json; @@ -33,7 +34,11 @@ protected SlackClientBase(IWebProxy proxySettings) protected Uri GetSlackUri(string path, Tuple[] getParameters) { - string parameters = getParameters + string parameters = default; + + if (getParameters != null && getParameters.Length > 0) + { + parameters = getParameters .Where(x => x.Item2 != null) .Select(new Func, string>(a => { @@ -54,11 +59,19 @@ protected Uri GetSlackUri(string path, Tuple[] getParameters) return string.Format("{0}&{1}", a, b); }); - Uri requestUri = new Uri(string.Format("{0}?{1}", path, parameters)); + } + + Uri requestUri = default; + + if (!string.IsNullOrEmpty(parameters)) + requestUri = new Uri(string.Format("{0}?{1}", path, parameters)); + else + requestUri = new Uri(path); + return requestUri; } - protected void APIRequest(Action callback, Tuple[] getParameters, Tuple[] postParameters) + protected void APIRequest(Action callback, Tuple[] getParameters, Tuple[] postParameters, string token = "") where K : Response { RequestPath path = RequestPath.GetRequestPath(); @@ -68,12 +81,15 @@ protected void APIRequest(Action callback, Tuple[] getPara Uri requestUri = GetSlackUri(Path.Combine(APIBaseLocation, path.Path), getParameters); HttpWebRequest request = CreateWebRequest(requestUri); + if (!string.IsNullOrEmpty(token)) + request.Headers.Add("Authorization", "Bearer " + token); + //This will handle all of the processing. RequestState state = new RequestState(request, postParameters, callback); state.Begin(); } - public Task APIRequestAsync(Tuple[] getParameters, Tuple[] postParameters) + public Task APIRequestAsync(Tuple[] getParameters, Tuple[] postParameters, string token = "") where K : Response { RequestPath path = RequestPath.GetRequestPath(); @@ -83,6 +99,9 @@ public Task APIRequestAsync(Tuple[] getParameters, Tuple(request, postParameters); return state.Execute(); @@ -111,9 +130,18 @@ protected HttpWebRequest CreateWebRequest(Uri requestUri) return httpWebRequest; } - protected HttpResponseMessage PostRequest(string requestUri, MultipartFormDataContent form) + protected Task PostRequestAsync(string requestUri, MultipartFormDataContent form, string token) { - return httpClient.PostAsync(requestUri, form).Result; + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + Content = form, + RequestUri = new Uri(requestUri), + }; + + requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + + return httpClient.SendAsync(requestMessage); } public void RegisterConverter(JsonConverter converter) diff --git a/SlackAPI/SlackTaskClient.cs b/SlackAPI/SlackTaskClient.cs index cea7fe0e..11962dfb 100644 --- a/SlackAPI/SlackTaskClient.cs +++ b/SlackAPI/SlackTaskClient.cs @@ -91,11 +91,7 @@ public Task APIRequestWithTokenAsync() public Task APIRequestWithTokenAsync(params Tuple[] postParameters) where K : Response { - Tuple[] tokenArray = new Tuple[]{ - new Tuple("token", APIToken) - }; - - return APIRequestAsync(tokenArray, postParameters); + return APIRequestAsync(new Tuple[] { }, postParameters, APIToken); } public Task TestAuthAsync() @@ -816,7 +812,6 @@ public async Task UploadFileAsync(byte[] fileData, string fi Uri target = new Uri(Path.Combine(APIBaseLocation, useAsync ? "files.uploadAsync" : "files.upload")); List parameters = new List(); - parameters.Add(string.Format("token={0}", APIToken)); //File/Content if (!string.IsNullOrEmpty(fileType)) @@ -836,7 +831,7 @@ public async Task UploadFileAsync(byte[] fileData, string fi using (MultipartFormDataContent form = new MultipartFormDataContent()) { form.Add(new ByteArrayContent(fileData), "file", fileName); - HttpResponseMessage response = PostRequest(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form); + HttpResponseMessage response = await PostRequestAsync(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form, APIToken); string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return result.Deserialize(); }