Skip to content

Commit

Permalink
Merge pull request #46 from ButterCMS/feature/api-changes
Browse files Browse the repository at this point in the history
feat: Update due to API changes
  • Loading branch information
ViolanteCodes authored Dec 23, 2024
2 parents 52d9bd1 + 2db792a commit cb53a44
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ButterCMS/ButterCMSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}/";
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions ButterCMS/Models/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public class Page<T>
[JsonProperty("page_type")]
public string PageType { get; set; }
public T Fields { get; set; }
public StatusEnum Status { get; set; }
public DateTime? Scheduled { get; set; }
}
}

4 changes: 3 additions & 1 deletion ButterCMS/Models/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace ButterCMS.Models
{
public enum PostStatusEnum
public enum StatusEnum
{
Unknown = 0,
Draft = 1,
Published = 2
Published = 2,
Scheduled = 3
}
}

64 changes: 64 additions & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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<string, string> parameters = new Dictionary<string, string>();
parameters.Add("preview", "1");

var pageResponse = client.RetrievePage<PageFields>("*", "test-page-1", parameters);
var page = pageResponse.Data;
Console.WriteLine("Page retrieved successfully");
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}");
}

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}");
}
}
}
}
11 changes: 11 additions & 0 deletions Demo/buttercms-demo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../ButterCMS/ButterCMS.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -620,6 +623,8 @@ Layout = "~/Views/Shared/Layouts/_Layout.cshtml";
|Published| DateTime?|
|PageType| string|
|Fields|T|
|Status|[StatusEnum](#statusenum)|
|Scheduled| DateTime|

## Exceptions

Expand All @@ -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.

0 comments on commit cb53a44

Please sign in to comment.