Esempio n. 1
0
        public async Task <IActionResult> GetShortenUrl(ShortenUrlInput input)
        {
            //add fluent validation
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiExceptionResponse("Invalid Input")));
            }
            var response = await _service.ShortenURL(_mapper.Map <ShortenUrlInput, UrlInfo>(input));

            return(Ok(response));
        }
        public async Task <ShortenUrlResponse> ShortenUrl(ShortenUrlInput input)
        {
            var UserId = GetCurrentUserId();
            ShortenUrlPayload payload = new ShortenUrlPayload()
            {
                UserId = UserId,
                Url    = input.Url,
            };
            var         json        = JsonConvert.SerializeObject(payload);
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
            var         uri         = "https://localhost:44364/api/Links/Shorten";
            var         request     = new HttpRequestMessage(HttpMethod.Post, uri);

            request.Content = httpContent;
            //adding auth token in header
            var authToken = _httpContextAccessor.HttpContext.Request.Headers["Authorization"].ToString();

            request.Headers.Add("Authorization", authToken);
            var client   = _clientFactory.CreateClient();
            var response = await client.SendAsync(request);

            //TODO add customised error responses
            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(new ShortenUrlResponse()
                {
                    Url = new Url(),
                    Message = "Server Error"
                });
            }
            string content = await response.Content.ReadAsStringAsync();

            var ShortenUrl = JsonConvert.DeserializeObject <ShortenUrlResponse>(content);

            return(ShortenUrl);
        }
Esempio n. 3
0
 public async Task <ShortenUrlResponse> ShortenUrl(ShortenUrlInput input)
 {
     //TODO model validation and remove IActionresult
     return(await _service.ShortenUrl(input));
 }