From 41070593dbea37dc12ad78714db5cb3b1e9b8653 Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Thu, 24 Oct 2024 16:23:18 +0200 Subject: [PATCH 1/6] feat: update Page and Post models --- ButterCMS/Models/Page.cs | 3 +++ ButterCMS/Models/Post.cs | 4 +++- ButterCMS/Models/{PostStatusEnum.cs => StatusEnum.cs} | 6 ++++-- 3 files changed, 10 insertions(+), 3 deletions(-) rename ButterCMS/Models/{PostStatusEnum.cs => StatusEnum.cs} (54%) diff --git a/ButterCMS/Models/Page.cs b/ButterCMS/Models/Page.cs index 81ed039..23c0d7e 100644 --- a/ButterCMS/Models/Page.cs +++ b/ButterCMS/Models/Page.cs @@ -12,5 +12,8 @@ public class Page [JsonProperty("page_type")] public string PageType { get; set; } public T Fields { get; set; } + public StatusEnum Status { get; set; } + public DateTime Scheduled { get; set; } } } + diff --git a/ButterCMS/Models/Post.cs b/ButterCMS/Models/Post.cs index 8e8d4e6..ea3207c 100644 --- a/ButterCMS/Models/Post.cs +++ b/ButterCMS/Models/Post.cs @@ -19,6 +19,8 @@ public class Post public string Summary { get; set; } public string SeoTitle { get; set; } public string MetaDescription { get; set; } - public PostStatusEnum Status { get; set; } + public StatusEnum Status { get; set; } + public DateTime Scheduled { get; set; } } } + diff --git a/ButterCMS/Models/PostStatusEnum.cs b/ButterCMS/Models/StatusEnum.cs similarity index 54% rename from ButterCMS/Models/PostStatusEnum.cs rename to ButterCMS/Models/StatusEnum.cs index e03d8df..a28bfaf 100644 --- a/ButterCMS/Models/PostStatusEnum.cs +++ b/ButterCMS/Models/StatusEnum.cs @@ -1,9 +1,11 @@ namespace ButterCMS.Models { - public enum PostStatusEnum + public enum StatusEnum { Unknown = 0, Draft = 1, - Published = 2 + Published = 2, + Scheduled = 3 } } + From 6120c088309ae31968be6f8d6852743091b48796 Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Thu, 24 Oct 2024 16:23:34 +0200 Subject: [PATCH 2/6] docs: update readme to include new api changes --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3027f12..d17b96e 100644 --- a/README.md +++ b/README.md @@ -532,15 +532,18 @@ Layout = "~/Views/Shared/Layouts/_Layout.cshtml"; |Summary|string| |SeoTitle|string| |MetaDescription|string| -|Status|[PostStatusEnum](#poststatusenum)| +|Status|[StatusEnum](#statusenum)| +|Scheduled|DateTime| -### PostStatusEnum +### StatusEnum |Constant|Value| |---|---| |Unknown|0| |Draft|1| |Published|2| +|Scheduled|3| + ### PostResponse Class @@ -620,6 +623,8 @@ Layout = "~/Views/Shared/Layouts/_Layout.cshtml"; |Published| DateTime?| |PageType| string| |Fields|T| +|Status|[StatusEnum](#statusenum)| +|Scheduled| DateTime| ## Exceptions @@ -644,3 +649,4 @@ This exception will be thrown when the library can't fit the returned data from ### PagesObjectMismatchException This exception will be thrown when the library can't fit the returned data from a Pages request into the passed object class. + From d0157204b91519906b6d731c3b5cfb0df2bf8800 Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Mon, 28 Oct 2024 13:16:24 +0100 Subject: [PATCH 3/6] feat: parametrize base url --- ButterCMS/ButterCMSClient.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ButterCMS/ButterCMSClient.cs b/ButterCMS/ButterCMSClient.cs index 10c0ad9..66958e2 100644 --- a/ButterCMS/ButterCMSClient.cs +++ b/ButterCMS/ButterCMSClient.cs @@ -23,7 +23,7 @@ public class ButterCMSClient private TimeSpan defaultTimeout = new TimeSpan(0, 0, 10); private int maxRequestTries; - private const string apiBaseAddress = "https://api.buttercms.com/"; + private const string apiBaseAddressDefault = "https://api.buttercms.com/"; private const string listPostsEndpoint = "v2/posts/"; private const string retrievePostEndpoint = "v2/posts/{0}/"; @@ -56,6 +56,7 @@ public ButterCMSClient(string authToken, TimeSpan? timeOut = null, int maxReques System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; #endif + string apiBaseAddress = Environment.GetEnvironmentVariable("API_BASE_URL") ?? apiBaseAddressDefault; httpClient = new HttpClient(httpMessageHandler ?? new HttpClientHandler { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip From fa2bd34c531a2a07344121f4509aca7252a7fdb4 Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Mon, 28 Oct 2024 13:17:26 +0100 Subject: [PATCH 4/6] feat: add demo project --- Demo/Program.cs | 51 ++++++++++++++++++++++++++++++++++++++ Demo/buttercms-demo.csproj | 11 ++++++++ Dockerfile | 24 ++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 Demo/Program.cs create mode 100644 Demo/buttercms-demo.csproj create mode 100644 Dockerfile diff --git a/Demo/Program.cs b/Demo/Program.cs new file mode 100644 index 0000000..d6cf747 --- /dev/null +++ b/Demo/Program.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using ButterCMS; +using ButterCMS.Models; +using Newtonsoft.Json; + +public class PageFields +{ + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("description")] + public string Description { get; set; } +} + + +namespace ButterCMSDemo +{ + class Program + { + static void Main(string[] args) + { + var apiKey = Environment.GetEnvironmentVariable("API_KEY"); + if (string.IsNullOrEmpty(apiKey)) + { + Console.WriteLine("API_KEY environment variable is not set"); + return; + } + + var client = new ButterCMSClient(apiKey); + + try + { + Dictionary parameters = new Dictionary(); + parameters.Add("preview", "1"); + + var pageResponse = client.RetrievePage("*", "test-page-1", parameters); + var page = pageResponse.Data; + Console.WriteLine("Page retrieved successfully"); + Console.WriteLine($"Page: {page}"); + Console.WriteLine($"Page Slug: {page.Slug}"); + Console.WriteLine($"Page Status: {page.Status}"); + Console.WriteLine($"Page Scheduled: {page.Scheduled}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error fetching page: {ex.Message}"); + } + } + } +} diff --git a/Demo/buttercms-demo.csproj b/Demo/buttercms-demo.csproj new file mode 100644 index 0000000..a7a550b --- /dev/null +++ b/Demo/buttercms-demo.csproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..52c1030 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +# Stage 1: Build C# SDK and Demo +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build + +WORKDIR /src + +# Copy the library +COPY ButterCMS/ ./ButterCMS/ + +# Copy the demo app +COPY Demo/ ./Demo/ + +WORKDIR /src/Demo +RUN dotnet restore +RUN dotnet publish -c Release -o /app/out + +# Stage 2: Run the Demo +FROM mcr.microsoft.com/dotnet/runtime:6.0 +WORKDIR /app +COPY --from=build /app/out . + +ENV API_KEY=your_api_key +ENV API_BASE_URL=https://api.buttercms.com/ + +CMD ["dotnet", "buttercms-demo.dll"] \ No newline at end of file From 15c06e2cff22efe1376161a75c0fd56c3bdf1da5 Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Mon, 28 Oct 2024 13:18:02 +0100 Subject: [PATCH 5/6] fix: make published and scheduled dates optional --- ButterCMS/Models/Page.cs | 2 +- ButterCMS/Models/Post.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ButterCMS/Models/Page.cs b/ButterCMS/Models/Page.cs index 23c0d7e..f4304db 100644 --- a/ButterCMS/Models/Page.cs +++ b/ButterCMS/Models/Page.cs @@ -13,7 +13,7 @@ public class Page public string PageType { get; set; } public T Fields { get; set; } public StatusEnum Status { get; set; } - public DateTime Scheduled { get; set; } + public DateTime? Scheduled { get; set; } } } diff --git a/ButterCMS/Models/Post.cs b/ButterCMS/Models/Post.cs index ea3207c..e56ea95 100644 --- a/ButterCMS/Models/Post.cs +++ b/ButterCMS/Models/Post.cs @@ -20,7 +20,7 @@ public class Post public string SeoTitle { get; set; } public string MetaDescription { get; set; } public StatusEnum Status { get; set; } - public DateTime Scheduled { get; set; } + public DateTime? Scheduled { get; set; } } } From 2db792a38e5473fb3672407c8d3dfb97666fc56e Mon Sep 17 00:00:00 2001 From: Martin Albert Date: Mon, 28 Oct 2024 13:23:44 +0100 Subject: [PATCH 6/6] chore: add post demo --- Demo/Program.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Demo/Program.cs b/Demo/Program.cs index d6cf747..6ea5613 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -37,7 +37,6 @@ static void Main(string[] args) var pageResponse = client.RetrievePage("*", "test-page-1", parameters); var page = pageResponse.Data; Console.WriteLine("Page retrieved successfully"); - Console.WriteLine($"Page: {page}"); Console.WriteLine($"Page Slug: {page.Slug}"); Console.WriteLine($"Page Status: {page.Status}"); Console.WriteLine($"Page Scheduled: {page.Scheduled}"); @@ -46,6 +45,20 @@ static void Main(string[] args) { Console.WriteLine($"Error fetching page: {ex.Message}"); } + + try + { + var postResponse = client.RetrievePost("test-blog-post"); + var post = postResponse.Data; + Console.WriteLine("Post retrieved successfully"); + Console.WriteLine($"Post Slug: {post.Slug}"); + Console.WriteLine($"Post Status: {post.Status}"); + Console.WriteLine($"Post Scheduled: {post.Scheduled}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error fetching post: {ex.Message}"); + } } } }