Exemplo n.º 1
0
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        {
            var content = await req.Content.ReadAsStringAsync();

            var request = JsonConvert.DeserializeObject <ShortUrlRequest>(content);

            Assembly assembly = typeof(PostNewUrl).GetTypeInfo().Assembly;

            var builder = new ConfigurationBuilder()
                          .AddJsonFile(new EmbeddedFileProvider(assembly, "post_new_url"), "appsettings.json", true, false);

            var configuration = builder.Build();

            var result = new ShortUrlResponse(
                (shortUrl) => ShortenUrlFunctions.SaveShortUrl(() => configuration["StorageConnection"], shortUrl),
                ShortenUrlFunctions.ShortenUrl,
                (hash) => ShortenUrlFunctions.RetrieveShortUrl(() => configuration["StorageConnection"], hash),
                request);

            return(new HttpResponseMessage
            {
                StatusCode = result.StatusCode,
                Content = new StringContent(JObject.FromObject(result.value).ToString(), Encoding.UTF8, "application/json")
            });
        }
Exemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, string hash, TraceWriter log)
        {
            var environment = Environment.GetEnvironmentVariable("FUNCTION_ENVIRONMENT");

            Assembly assembly = typeof(GetUrl).GetTypeInfo().Assembly;

            var builder = new ConfigurationBuilder()
                          .AddJsonFile(new EmbeddedFileProvider(assembly, "get_url"), "appsettings.json", true, false)
                          .AddJsonFile(new EmbeddedFileProvider(assembly, "get_url"), $"appsettings.{environment}.json", true, false);

            var configuration = builder.Build();

            var shortUrl = await ShortenUrlFunctions
                           .RetrieveShortUrl(
                () => configuration["StorageConnection"],
                hash);

            if (shortUrl == null)
            {
                return(new HttpResponseMessage
                {
                    StatusCode = System.Net.HttpStatusCode.NotFound
                });
            }
            else
            {
                var response = new HttpResponseMessage
                {
                    StatusCode = System.Net.HttpStatusCode.Redirect,
                };
                response.Headers.Location = new System.Uri(shortUrl);
                return(response);
            }
        }
        public void ShortenGoogleUrlShouldReturn_c7e4()
        {
            var result = ShortenUrlFunctions.ShortenUrl("http://google.com/");

            Assert.Equal("c7e4", result);
        }