public async Task <LineClientResult <T> > Execute <T>(object body, CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = new LineClientResult <T>();

            var url = await BuildUri(Path, Params).ConfigureAwait(false);

            Client.CheckDisposed();

            var message = new HttpRequestMessage(Method, url);

            BuildBodyContent(message, body);

            var response = await Client.HttpClient.SendAsync(message, cancellationToken).ConfigureAwait(false);

            ResponseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.NotFound && !response.IsSuccessStatusCode)
            {
                if (ResponseStream == null)
                {
                    throw new ArgumentException($"Unexpected response, status code {response.StatusCode}");
                }
            }

            result.Response            = Deserialize <T>(ResponseStream);
            result.IsSuccessStatusCode = response.IsSuccessStatusCode;
            result.StatusCode          = response.StatusCode;

            return(result);
        }
        public async Task <LineClientResult <Stream> > ExecuteStreamServiceCall <T>(object body, CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = new LineClientResult <T>();

            var url = await BuildUri(Path, Params).ConfigureAwait(false);

            Client.CheckDisposed();

            var message = new HttpRequestMessage(Method, url);

            BuildBodyContent(message, body);

            var response = await Client.HttpClient.SendAsync(message, cancellationToken).ConfigureAwait(false);

            ResponseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                Stream content = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                content.Seek(0, SeekOrigin.Begin);
                MemoryStream st = new MemoryStream();
                content.CopyTo(st);
                st.Seek(0, SeekOrigin.Begin);

                return(new LineClientResult <Stream>()
                {
                    IsSuccessStatusCode = response.IsSuccessStatusCode,
                    Response = st,
                    StatusCode = response.StatusCode
                });
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                throw new InvalidDataException(response.Content.ReadAsStringAsync().Result);
            }
            else if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new UnauthorizedAccessException("Unauthorized response");
            }
            throw new Exception(string.Format(CultureInfo.InvariantCulture, "Unknown response. Status Code: {0}", response.StatusCode));
        }
Example #3
0
        public void GetMemberProfile_JsonFormatTest()
        {
            var apiResponse = new LineClientResult <Profile>()
            {
                Response = new Profile()
                {
                    displayName = "",
                    pictureUrl  = "",
                    userId      = ""
                }
            };

            _roomClient.Setup(x => x.GetMemberProfile(It.IsAny <string>(), It.IsAny <string>())).Returns(Task.FromResult <LineClientResult <Profile> >(apiResponse));

            // Execute
            Task <LineClientResult <Profile> > response = _roomClient.Object.GetMemberProfile("userId", "roomId");

            var result = response.Result;

            // Verify
            Assert.AreEqual <string>(JsonConvert.SerializeObject(result, jsonSettings), "{\"response\":{\"userId\":\"\",\"displayName\":\"\",\"pictureUrl\":\"\"},\"statusCode\":0,\"isSuccessStatusCode\":false}");
        }
Example #4
0
        public void GetUserProfile_JsonFormatTest()
        {
            var apiResponse = new LineClientResult <Profile>()
            {
                Response = new Profile()
                {
                    displayName = "",
                    pictureUrl  = "",
                    userId      = ""
                }
            };

            _userClient.Setup(x => x.GetProfile(It.IsAny <string>())).Returns(Task.FromResult <LineClientResult <Profile> >(apiResponse));

            // Execute
            Task <LineClientResult <Profile> > response = _userClient.Object.GetProfile("user id");

            var result = response.Result.Response;

            // Verify
            Assert.AreEqual <string>(JsonConvert.SerializeObject(result), "{\"userId\":\"\",\"displayName\":\"\",\"pictureUrl\":\"\"}");
        }