From 7f3291e75c2f303546f8fd2abb3550b20085419e Mon Sep 17 00:00:00 2001 From: Hugo Hedlund Date: Sun, 14 Jun 2020 00:30:00 +0200 Subject: [PATCH 1/7] Check if the url parameter to UrlShortener and UrlUpdate functions is empty and returns an error message if so. --- src/shortenerTools/UrlShortener/UrlShortener.cs | 8 +++++++- src/shortenerTools/UrlUpdate/UrlUpdate.cs | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/shortenerTools/UrlShortener/UrlShortener.cs b/src/shortenerTools/UrlShortener/UrlShortener.cs index 19a55b7b..f3a58242 100644 --- a/src/shortenerTools/UrlShortener/UrlShortener.cs +++ b/src/shortenerTools/UrlShortener/UrlShortener.cs @@ -53,7 +53,13 @@ public static async Task Run( { return req.CreateResponse(HttpStatusCode.NotFound); } - + + // If the Url parameter only contains whitespaces or is empty return with BadRequest. + if (string.IsNullOrWhiteSpace(input.Url)) + { + return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The url parameter can not be empty."); + } + var result = new ShortResponse(); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) diff --git a/src/shortenerTools/UrlUpdate/UrlUpdate.cs b/src/shortenerTools/UrlUpdate/UrlUpdate.cs index c6d89ecd..6b3bcce9 100644 --- a/src/shortenerTools/UrlUpdate/UrlUpdate.cs +++ b/src/shortenerTools/UrlUpdate/UrlUpdate.cs @@ -62,6 +62,12 @@ public static async Task Run( return req.CreateResponse(HttpStatusCode.NotFound); } + // If the Url parameter only contains whitespaces or is empty return with BadRequest. + if (string.IsNullOrWhiteSpace(input.Url)) + { + return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The url parameter can not be empty."); + } + ShortUrlEntity result; var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) From eaee30b690e570f0e8954e4e51e6a4ad87c2afa4 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 06:49:48 -0400 Subject: [PATCH 2/7] Update API calls Documantation --- how-it-works.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/how-it-works.md b/how-it-works.md index 2d143f44..5356fa5f 100644 --- a/how-it-works.md +++ b/how-it-works.md @@ -11,7 +11,44 @@ Azure Functions Azure Function were the perfect match for this project because when you use a dynamic plan you are charged only when the function is running. In our case, it's only a few seconds at the time. To know more read [Azure Function Pricing](https://azure.microsoft.com/en-us/pricing/details/functions/?WT.mc_id=azurlshortener-github-frbouch) -1- Function: UrlClickStats +1- Function: UrlArchive +-------------------------- + +This function set the property `IsArchived` to true. It's a soft delete. When `IsArchived` equal true it won't be return in the list and should works anymore as a short URL. You can call it directly doing an HTTP request of type POST with a header "Content-Type" equal to "application/json". Passing the vanity of that specific URL in the body as showed below. + +### Expected Input + +```json +{ + // [Required] + "PartitionKey": "d", + + // [Required] + "RowKey": "doc", + + // [Optional] all other properties +} +``` + +### Output + +```json +{ + "Url": "https://docs.microsoft.com/en-ca/azure/azure-functions/functions-create-your-first-function-visual-studio", + "Title": "My Title", + "ShortUrl": null, + "Clicks": 0, + "IsArchived": true, + "PartitionKey": "a", + "RowKey": "azFunc2", + "Timestamp": "2020-07-23T06:22:33.852218-04:00", + "ETag": "W/\"datetime'2020-07-23T10%3A24%3A51.3440526Z'\"" +} +``` + + + +2- Function: UrlClickStats -------------------------- This function return the statistic for a specific URL. You can call it directly doing an HTTP request of type POST with a header "Content-Type" equal to "application/json". Passing the vanity of that specific URL in the body as showed below. @@ -42,7 +79,7 @@ This function return the statistic for a specific URL. You can call it directly -2- Function: UrlList +3- Function: UrlList ------------------------- This function return a list of all URLs created previously including the clicks count. You can call it directly doing an HTTP request of type GET with a header "Content-Type" equal to "application/json". @@ -68,7 +105,7 @@ No input required. } ``` -3- Function: UrlRedirect +4- Function: UrlRedirect ------------------------- This function return a HTTP Redirect to the URL. You can call it directly doing an HTTP request of type POST or GET passing the vanity at the end of the URL. The Azure Function Proxy will call Function passing the parameter. @@ -78,7 +115,7 @@ For example if the domain is *c5m.ca* and the vanity is "2w", the request `c5m.c Every times the Azure Function is called it will increment the click count and save the timestamp when this call appends. -4- Function: UrlShortener +5- Function: UrlShortener ------------------------- This function creates the short version of our URL and return the info it. You can call it directly doing an HTTP request of type POST with a header "Content-Type" equal to "application/json". Passing the URL and an optional vanity in the body as showed below. If no vanity is specified one will be automatically generated for you! @@ -125,6 +162,50 @@ Here the result when the vanity wasn't specified and the alternative when the va --- + +6- Function: UrlUpdate +-------------------------- + +This function will update the properties: `Url` and `Title` to the new value. You can call it directly doing an HTTP request of type POST with a header "Content-Type" equal to "application/json". Passing the vanity of that specific URL in the body as showed below. + +### Expected Input + +```json +{ + // [Required] + "PartitionKey": "d", + + // [Required] + "RowKey": "doc", + + // [Optional] New Title for this URL, or text description of your choice. + "title": "Quickstart: Create your first function in Azure using Visual Studio" + + // [Optional] New long Url where the the user will be redirect + "Url": "https://SOME_URL" +} +``` + +### Output + +```json +{ + "Url": "https://docs.microsoft.com/en-ca/azure/azure-functions/functions-create-your-first-function-visual-studio", + "Title": "My Title", + "ShortUrl": null, + "Clicks": 0, + "IsArchived": true, + "PartitionKey": "a", + "RowKey": "azFunc2", + "Timestamp": "2020-07-23T06:22:33.852218-04:00", + "ETag": "W/\"datetime'2020-07-23T10%3A24%3A51.3440526Z'\"" +} +``` + +--- + + + Azure Table Storage =================== From 6eeb72e6c62e2a556b263e0de0c5f9933fb836ff Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 06:50:31 -0400 Subject: [PATCH 3/7] Change Method and add a return object --- src/shortenerTools/UrlArchive/UrlArchive.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shortenerTools/UrlArchive/UrlArchive.cs b/src/shortenerTools/UrlArchive/UrlArchive.cs index fc02bc5e..03dc0637 100644 --- a/src/shortenerTools/UrlArchive/UrlArchive.cs +++ b/src/shortenerTools/UrlArchive/UrlArchive.cs @@ -29,7 +29,7 @@ public static class UrlArchive { [FunctionName("UrlArchive")] public static async Task Run( - [HttpTrigger(AuthorizationLevel.Function, "delete", Route = null)]HttpRequestMessage req, + [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, ILogger log, ExecutionContext context) { @@ -66,7 +66,7 @@ public static async Task Run( return req.CreateResponse(HttpStatusCode.BadRequest, ex); } - return req.CreateResponse(HttpStatusCode.OK); + return req.CreateResponse(HttpStatusCode.OK, result); } } } From 1061633be94f4f0b563515856ea462677cfb1557 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 06:53:43 -0400 Subject: [PATCH 4/7] Update Admin website archive calling --- .../adminBlazorWebsite/src/Data/UrlShortenerService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs index 7ac81af9..36efec80 100644 --- a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs +++ b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs @@ -116,7 +116,7 @@ public async Task ArchiveShortUrl(ShortUrlEntity archivedUrl) CancellationToken cancellationToken; using (var client = new HttpClient()) - using (var request = new HttpRequestMessage(HttpMethod.Delete, url)) + using (var request = new HttpRequestMessage(HttpMethod.Post, url)) using (var httpContent = CreateHttpContent(archivedUrl)) { request.Content = httpContent; From 4e0f97510600bb8a8961eda0101ecb6586dcf314 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 06:53:43 -0400 Subject: [PATCH 5/7] Revert "Update Admin website archive calling" This reverts commit 1061633be94f4f0b563515856ea462677cfb1557. --- .../adminBlazorWebsite/src/Data/UrlShortenerService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs index 36efec80..7ac81af9 100644 --- a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs +++ b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs @@ -116,7 +116,7 @@ public async Task ArchiveShortUrl(ShortUrlEntity archivedUrl) CancellationToken cancellationToken; using (var client = new HttpClient()) - using (var request = new HttpRequestMessage(HttpMethod.Post, url)) + using (var request = new HttpRequestMessage(HttpMethod.Delete, url)) using (var httpContent = CreateHttpContent(archivedUrl)) { request.Content = httpContent; From 11c57fa99743b9fdf1c730f10579b9ad80609b3c Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 06:53:43 -0400 Subject: [PATCH 6/7] Revert "Update Admin website archive calling" This reverts commit 1061633be94f4f0b563515856ea462677cfb1557. --- .../adminBlazorWebsite/src/Data/UrlShortenerService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs index 36efec80..7ac81af9 100644 --- a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs +++ b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs @@ -116,7 +116,7 @@ public async Task ArchiveShortUrl(ShortUrlEntity archivedUrl) CancellationToken cancellationToken; using (var client = new HttpClient()) - using (var request = new HttpRequestMessage(HttpMethod.Post, url)) + using (var request = new HttpRequestMessage(HttpMethod.Delete, url)) using (var httpContent = CreateHttpContent(archivedUrl)) { request.Content = httpContent; From 14eb442886e9d5a5f4b0e9e53ca27e3256ff9d7a Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 23 Jul 2020 07:05:01 -0400 Subject: [PATCH 7/7] Update Admin website calling Archive --- .../adminBlazorWebsite/src/Data/UrlShortenerService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs index 7ac81af9..36efec80 100644 --- a/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs +++ b/src/adminTools/adminBlazorWebsite/src/Data/UrlShortenerService.cs @@ -116,7 +116,7 @@ public async Task ArchiveShortUrl(ShortUrlEntity archivedUrl) CancellationToken cancellationToken; using (var client = new HttpClient()) - using (var request = new HttpRequestMessage(HttpMethod.Delete, url)) + using (var request = new HttpRequestMessage(HttpMethod.Post, url)) using (var httpContent = CreateHttpContent(archivedUrl)) { request.Content = httpContent;