Skip to content

Commit

Permalink
Merge pull request #15 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 10, 2024
2 parents 75b1a42 + 70afe0a commit c4691e2
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM php:8.2-cli

WORKDIR /app
COPY . .

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install

ENV API_BASE_URL=https://api.buttercms.com/v2
ENV API_KEY=your_api_key

CMD ["php", "demo/demo.php"]
34 changes: 34 additions & 0 deletions demo/demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require_once 'vendor/autoload.php';

use ButterCMS\ButterCMS;

$apiKey = getenv('API_KEY');
if (empty($apiKey)) {
die("API_KEY environment variable is not set\n");
}

$client = new ButterCMS($apiKey);

try {
$params = ['preview' => 1];
$page = $client->fetchPage('*', 'test-page-1', $params);
echo "Page Slug: " . $page->getSlug() . "\n";
echo "Page Status: " . $page->getStatus() . "\n";
echo "Page Scheduled Date: " . $page->getScheduled() . "\n";
} catch (Exception $e) {
echo "Error fetching page: " . $e->getMessage() . "\n";
}

try {
$postResponse = $client->fetchPost('test-blog-post');
$post = $postResponse->getPost();
echo "Post Slug: " . $post->getSlug() . "\n";
echo "Post Status: " . $post->getStatus() . "\n";
echo "Post Scheduled Date: " . $post->getScheduled() . "\n";
} catch (Exception $e) {
echo "Error fetching post: " . $e->getMessage() . "\n";
}

?>
5 changes: 3 additions & 2 deletions src/ButterCMS/ButterCMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class ButterCMS
{
protected const VERSION = '3.0.1';
protected const API_ROOT_URL = 'https://api.buttercms.com/v2/';
protected const API_DEFAULT_URL = 'https://api.buttercms.com/v2/';

protected $maxRetryCount = 1;
protected $readAuthToken;
Expand Down Expand Up @@ -71,12 +71,13 @@ protected function request(string $method, $url, $query = [], $data = [], $tryCo
}
}

$apiRootUrl = getenv('API_BASE_URL') ?: self::API_DEFAULT_URL;
try {
$options = array_filter([
'query' => $query,
'json' => $data,
]);
$response = $client->$method(self::API_ROOT_URL . $url, $options);
$response = $client->$method($apiRootUrl . $url, $options);
} catch (BadResponseException $e) {
$httpCode = (int)$e->getResponse()->getStatusCode();
if ($tryCount < $this->maxRetryCount && $httpCode !== 404) {
Expand Down
48 changes: 48 additions & 0 deletions src/ButterCMS/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,57 @@ class Page extends Model
protected $published;
protected $updated;
protected $fields;
protected $status;
protected $scheduled;

public function getSlug()
{
return $this->slug;
}

public function getPageType()
{
return $this->page_type;
}

public function getPublished()
{
return $this->published;
}

public function getUpdated()
{
return $this->updated;
}

public function getStatus()
{
return $this->status;
}

public function getScheduled()
{
return $this->scheduled;
}

public function getField($fieldName, $default = null)
{
return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : $default;
}

public function isPublished()
{
return 'published' === $this->status;
}

public function isScheduled()
{
return 'scheduled' === $this->status;
}

public function isDraft()
{
return 'draft' === $this->status;
}
}

92 changes: 92 additions & 0 deletions src/ButterCMS/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Post extends Model
protected $published;
protected $created;
protected $status;
protected $scheduled;
protected $title;
protected $body;
protected $summary;
Expand Down Expand Up @@ -46,8 +47,99 @@ public function __construct(array $data)
parent::__construct($data);
}

public function getSlug()
{
return $this->slug;
}

public function getUrl()
{
return $this->url;
}

public function getPublished()
{
return $this->published;
}

public function getCreated()
{
return $this->created;
}

public function getStatus()
{
return $this->status;
}

public function getScheduled()
{
return $this->scheduled;
}

public function getTitle()
{
return $this->title;
}

public function getBody()
{
return $this->body;
}

public function getSummary()
{
return $this->summary;
}

public function getSeoTitle()
{
return $this->seo_title;
}

public function getMetaDescription()
{
return $this->meta_description;
}

public function getAuthor()
{
return $this->author;
}

public function getCategories()
{
return $this->categories;
}

public function getTags()
{
return $this->tags;
}

public function getFeaturedImage()
{
return $this->featured_image;
}

public function getFeaturedImageAlt()
{
return $this->featured_image_alt;
}

public function isPublished()
{
return 'published' === $this->status;
}

public function isScheduled()
{
return 'scheduled' === $this->status;
}

public function isDraft()
{
return 'draft' === $this->status;
}
}

5 changes: 5 additions & 0 deletions src/ButterCMS/Model/PostResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function __construct(array $dataArray)

parent::__construct($dataArray);
}

public function getPost()
{
return $this->post;
}
}

0 comments on commit c4691e2

Please sign in to comment.