public void Initialize(string accessToken, string accessSecret, long userId, string screenName) { var newInstance = new TwitterApiConnection(accessToken, accessSecret); var oldInstance = Interlocked.Exchange(ref this.apiConnection, newInstance); oldInstance?.Dispose(); this.CurrentUserId = userId; this.CurrentScreenName = screenName; }
private async Task <UserAccount> PinAuth() { var requestToken = await TwitterApiConnection.GetRequestTokenAsync(); var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken); var pin = AuthDialog.DoAuth(this, pinPageUrl); if (string.IsNullOrEmpty(pin)) { return(null); // キャンセルされた場合 } var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin); return(new UserAccount { Username = accessTokenResponse["screen_name"], UserId = long.Parse(accessTokenResponse["user_id"]), Token = accessTokenResponse["oauth_token"], TokenSecret = accessTokenResponse["oauth_token_secret"], }); }
public async Task GetAsync_Test() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(x => { Assert.Equal(HttpMethod.Get, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.GetLeftPart(UriPartial.Path)); var query = HttpUtility.ParseQueryString(x.RequestUri.Query); Assert.Equal("1111", query["aaaa"]); Assert.Equal("2222", query["bbbb"]); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"hogehoge\""), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var param = new Dictionary<string, string> { ["aaaa"] = "1111", ["bbbb"] = "2222", }; var result = await apiConnection.GetAsync<string>(endpoint, param, endpointName: "/hoge/tetete") .ConfigureAwait(false); Assert.Equal("hogehoge", result); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task PostJsonAsync_Test() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(async x => { Assert.Equal(HttpMethod.Post, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.AbsoluteUri); Assert.Equal("application/json; charset=utf-8", x.Content.Headers.ContentType.ToString()); var body = await x.Content.ReadAsStringAsync() .ConfigureAwait(false); Assert.Equal("{\"aaaa\": 1111}", body); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"hogehoge\""), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); await apiConnection.PostJsonAsync(endpoint, "{\"aaaa\": 1111}") .ConfigureAwait(false); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task PostLazyAsync_Multipart_NullTest() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.httpUpload = http; mockHandler.Enqueue(async x => { Assert.Equal(HttpMethod.Post, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.AbsoluteUri); Assert.IsType<MultipartFormDataContent>(x.Content); var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>() .First(y => y.Name == "boundary").Value; // 前後のダブルクオーテーションを除去 boundary = boundary.Substring(1, boundary.Length - 2); var expectedText = $"--{boundary}\r\n" + $"\r\n--{boundary}--\r\n"; var expected = Encoding.UTF8.GetBytes(expectedText); Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync().ConfigureAwait(false)); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"hogehoge\""), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var result = await apiConnection.PostLazyAsync<string>(endpoint, param: null, media: null) .ConfigureAwait(false); Assert.Equal("hogehoge", await result.LoadJsonAsync() .ConfigureAwait(false)); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task PostLazyAsync_MultipartTest() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.httpUpload = http; using (var image = TestUtils.CreateDummyImage()) using (var media = new MemoryImageMediaItem(image)) { mockHandler.Enqueue(async x => { Assert.Equal(HttpMethod.Post, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.AbsoluteUri); Assert.IsType<MultipartFormDataContent>(x.Content); var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>() .First(y => y.Name == "boundary").Value; // 前後のダブルクオーテーションを除去 boundary = boundary.Substring(1, boundary.Length - 2); var expectedText = $"--{boundary}\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=aaaa\r\n" + "\r\n" + "1111\r\n"+ $"--{boundary}\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=bbbb\r\n" + "\r\n" + "2222\r\n" + $"--{boundary}\r\n" + $"Content-Disposition: form-data; name=media1; filename={media.Name}; filename*=utf-8''{media.Name}\r\n" + "\r\n"; var expected = Encoding.UTF8.GetBytes(expectedText) .Concat(image.Stream.ToArray()) .Concat(Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n")); Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync().ConfigureAwait(false)); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"hogehoge\""), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var param = new Dictionary<string, string> { ["aaaa"] = "1111", ["bbbb"] = "2222", }; var mediaParam = new Dictionary<string, IMediaItem> { ["media1"] = media, }; var result = await apiConnection.PostLazyAsync<string>(endpoint, param, mediaParam) .ConfigureAwait(false); Assert.Equal("hogehoge", await result.LoadJsonAsync() .ConfigureAwait(false)); Assert.Equal(0, mockHandler.QueueCount); } } }
public async Task PostLazyAsync_Test() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(async x => { Assert.Equal(HttpMethod.Post, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.AbsoluteUri); var body = await x.Content.ReadAsStringAsync() .ConfigureAwait(false); var query = HttpUtility.ParseQueryString(body); Assert.Equal("1111", query["aaaa"]); Assert.Equal("2222", query["bbbb"]); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"hogehoge\""), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var param = new Dictionary<string, string> { ["aaaa"] = "1111", ["bbbb"] = "2222", }; var result = await apiConnection.PostLazyAsync<string>(endpoint, param) .ConfigureAwait(false); Assert.Equal("hogehoge", await result.LoadJsonAsync() .ConfigureAwait(false)); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task GetStreamAsync_Test() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) using (var image = TestUtils.CreateDummyImage()) { apiConnection.http = http; mockHandler.Enqueue(x => { Assert.Equal(HttpMethod.Get, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.GetLeftPart(UriPartial.Path)); var query = HttpUtility.ParseQueryString(x.RequestUri.Query); Assert.Equal("1111", query["aaaa"]); Assert.Equal("2222", query["bbbb"]); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(image.Stream.ToArray()), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var param = new Dictionary<string, string> { ["aaaa"] = "1111", ["bbbb"] = "2222", }; var stream = await apiConnection.GetStreamAsync(endpoint, param) .ConfigureAwait(false); using (var memoryStream = new MemoryStream()) { // 内容の比較のために MemoryStream にコピー await stream.CopyToAsync(memoryStream).ConfigureAwait(false); Assert.Equal(image.Stream.ToArray(), memoryStream.ToArray()); } Assert.Equal(0, mockHandler.QueueCount); } }
public async Task GetAsync_ErrorJsonTest() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(x => { return new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new StringContent("{\"errors\":[{\"code\":187,\"message\":\"Status is a duplicate.\"}]}"), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var exception = await Assert.ThrowsAsync<TwitterApiException>(() => apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete")) .ConfigureAwait(false); // エラーレスポンスの JSON に含まれるエラーコードに基づいてメッセージを出力する Assert.Equal("DuplicateStatus", exception.Message); Assert.Equal(TwitterErrorCode.DuplicateStatus, exception.ErrorResponse.Errors[0].Code); Assert.Equal("Status is a duplicate.", exception.ErrorResponse.Errors[0].Message); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task GetAsync_ErrorStatusTest() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(x => { return new HttpResponseMessage(HttpStatusCode.BadGateway) { Content = new StringContent("### Invalid JSON Response ###"), }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); var exception = await Assert.ThrowsAsync<TwitterApiException>(() => apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete")) .ConfigureAwait(false); // エラーレスポンスの読み込みに失敗した場合はステータスコードをそのままメッセージに使用する Assert.Equal("BadGateway", exception.Message); Assert.Null(exception.ErrorResponse); Assert.Equal(0, mockHandler.QueueCount); } }
public async Task GetAsync_UpdateRateLimitTest() { using (var mockHandler = new HttpMessageHandlerMock()) using (var http = new HttpClient(mockHandler)) using (var apiConnection = new TwitterApiConnection("", "")) { apiConnection.http = http; mockHandler.Enqueue(x => { Assert.Equal(HttpMethod.Get, x.Method); Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", x.RequestUri.GetLeftPart(UriPartial.Path)); return new HttpResponseMessage(HttpStatusCode.OK) { Headers = { { "X-Rate-Limit-Limit", "150" }, { "X-Rate-Limit-Remaining", "100" }, { "X-Rate-Limit-Reset", "1356998400" }, { "X-Access-Level", "read-write-directmessages" }, }, Content = new StringContent("\"hogehoge\""), }; }); var apiStatus = new TwitterApiStatus(); MyCommon.TwitterApiInfo = apiStatus; var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); await apiConnection.GetAsync<string>(endpoint, null, endpointName: "/hoge/tetete") .ConfigureAwait(false); Assert.Equal(apiStatus.AccessLevel, TwitterApiAccessLevel.ReadWriteAndDirectMessage); Assert.Equal(apiStatus.AccessLimit["/hoge/tetete"], new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime())); Assert.Equal(0, mockHandler.QueueCount); } }