-
Notifications
You must be signed in to change notification settings - Fork 14
[V2 Api] Basic changes required for api v2 to work #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a1179d9
Basic changes required for api v2 to work
d54fcff
update field names
f23956f
updates to fields
671d7ac
new publishItem method
e4de9cf
remove comments
6c2a99b
update item with fieldData
673c5dd
update links
67dfbde
field data in method
82f4f8e
modify error response.
923dff5
publish site correctly
a18bafc
set version
ccf9254
ignore
9ac188c
Domains must be simply an array of domains
2ae8a9d
v2 readme updates
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
|
||
class Api | ||
{ | ||
const WEBFLOW_API_ENDPOINT = 'https://api.webflow.com'; | ||
// changed api endpoints | ||
// https://docs.developers.webflow.com/data/changelog/webflow-api-changed-endpoints | ||
// https://docs.developers.webflow.com/data/reference/ | ||
const WEBFLOW_API_ENDPOINT = 'https://api.webflow.com/v2'; | ||
const WEBFLOW_API_USERAGENT = 'Expertlead Webflow PHP SDK (https://github.com/expertlead/webflow-php-sdk)'; | ||
|
||
private $client; | ||
|
@@ -20,7 +23,7 @@ class Api | |
|
||
public function __construct( | ||
$token, | ||
$version = '1.0.0' | ||
$version = '2.0.0' | ||
) { | ||
if (empty($token)) { | ||
throw new WebflowException('token'); | ||
|
@@ -34,7 +37,7 @@ public function __construct( | |
return $this; | ||
} | ||
|
||
private function request(string $path, string $method, array $data = []) | ||
private function request(string $path, string $method, $data = []) | ||
{ | ||
$curl = curl_init(); | ||
$options = [ | ||
|
@@ -52,13 +55,15 @@ private function request(string $path, string $method, array $data = []) | |
]; | ||
if (!empty($data)) { | ||
$json = json_encode($data); | ||
|
||
$options[CURLOPT_POSTFIELDS] = $json; | ||
$options[CURLOPT_HTTPHEADER][] = "Content-Length: " . strlen($json); | ||
} | ||
curl_setopt_array($curl, $options); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
list($headers, $body) = explode("\r\n\r\n", $response, 2); | ||
|
||
return $this->parse($body); | ||
} | ||
private function get($path) | ||
|
@@ -76,6 +81,12 @@ private function put($path, $data) | |
return $this->request($path, "PUT", $data); | ||
} | ||
|
||
|
||
private function patch($path, $data) | ||
{ | ||
return $this->request($path, "PATCH", $data); | ||
} | ||
|
||
private function delete($path) | ||
{ | ||
return $this->request($path, "DELETE"); | ||
|
@@ -84,12 +95,12 @@ private function delete($path) | |
private function parse($response) | ||
{ | ||
$json = json_decode($response); | ||
if (isset($json->code) && isset($json->msg)) { | ||
$error = $json->msg; | ||
if (isset($json->problems)) { | ||
$error .= PHP_EOL . implode(PHP_EOL, $json->problems); | ||
if (isset($json->code) && isset($json->message)) { | ||
$error = $json->message; | ||
if (isset($json->details)) { | ||
$error .= PHP_EOL . $json->code . ': ' . implode(PHP_EOL, $json->details) ; | ||
} | ||
throw new \Exception($error, $json->code); | ||
throw new \Exception($error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new json->code is a string not a number, do we want to pick an error code to return? |
||
} | ||
return $json; | ||
} | ||
|
@@ -115,9 +126,40 @@ public function domains(string $siteId) | |
return $this->get("/sites/{$siteId}/domains"); | ||
} | ||
|
||
public function publishSite(string $siteId, array $domains) | ||
/** | ||
* Publish site must take an array list of customdomains to be published to | ||
* and a boolean indicating if your webflow.io subdomain should be published to | ||
* @param string $siteId | ||
* @param array $domains | ||
* @param $publishWebflowSubdomain | ||
* @return mixed | ||
*/ | ||
public function publishSite(string $siteId, array $domains, $publishWebflowSubdomain = false) | ||
{ | ||
return $this->post("/sites/${siteId}/publish", $domains); | ||
// if domains empty array then | ||
if (!$domains){ | ||
$data = []; | ||
} else { | ||
$data = ['customDomains' => $domains]; | ||
} | ||
|
||
|
||
$data['publishToWebflowSubdomain'] = $publishWebflowSubdomain; | ||
|
||
return $this->post("/sites/${siteId}/publish", $data); | ||
} | ||
|
||
/** | ||
* Publish just the items specified, rather than the entire site | ||
* @param string $collection_id | ||
* @param array $itemIds | ||
* @return mixed | ||
*/ | ||
public function publishItem(string $collection_id, array $itemIds) | ||
{ | ||
return $this->post("/collections/{$collection_id}/items/publish", [ | ||
'itemIds' => $itemIds, | ||
]); | ||
} | ||
|
||
// Collections | ||
|
@@ -145,11 +187,11 @@ public function itemsAll(string $collectionId): array | |
{ | ||
$response = $this->items($collectionId); | ||
$items = $response->items; | ||
$limit = $response->limit; | ||
$total = $response->total; | ||
$limit = $response->pagination->limit; | ||
$total = $response->pagination->total; | ||
$pages = ceil($total / $limit); | ||
for ($page = 1; $page < $pages; $page++) { | ||
$offset = $response->limit * $page; | ||
$offset = $response->pagination->limit * $page; | ||
$items = array_merge($items, $this->items($collectionId, $offset, $limit)->items); | ||
} | ||
return $items; | ||
|
@@ -160,23 +202,39 @@ public function item(string $collectionId, string $itemId) | |
return $this->get("/collections/{$collectionId}/items/{$itemId}"); | ||
} | ||
|
||
public function createItem(string $collectionId, array $fields, bool $live = false) | ||
public function createItem(string $collectionId, $fields, bool $live = false) | ||
{ | ||
$defaults = [ | ||
"_archived" => false, | ||
"_draft" => false, | ||
"isArchived" => false, | ||
"isDraft" => false, | ||
]; | ||
$data = (object) [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This little bit of code could probably be a little more compact |
||
'fieldData' => [], | ||
]; | ||
// must be an object property | ||
$data->fieldData = $fields; | ||
return $this->post("/collections/{$collectionId}/items" . ($live ? "?live=true" : ""), | ||
$data | ||
); | ||
|
||
return $this->post("/collections/{$collectionId}/items" . ($live ? "?live=true" : ""), [ | ||
'fields' => array_merge($defaults, $fields), | ||
]); | ||
} | ||
|
||
/** | ||
* Version 2 update item patches the item so you do not need to provide all details. | ||
* @param string $collectionId | ||
* @param string $itemId | ||
* @param array $fields | ||
* @param bool $live | ||
* @return mixed | ||
*/ | ||
public function updateItem(string $collectionId, string $itemId, array $fields, bool $live = false) | ||
{ | ||
return $this->put("/collections/{$collectionId}/items/{$itemId}" . ($live ? "?live=true" : ""), [ | ||
'fields' => $fields, | ||
]); | ||
$data = (object) [ | ||
'fieldData' => [], | ||
]; | ||
// must be an object property | ||
$data->fieldData = $fields; | ||
return $this->patch("/collections/{$collectionId}/items/{$itemId}" . ($live ? "?live=true" : ""), $data); | ||
} | ||
|
||
public function removeItem(string $collectionId, $itemId) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to ensure that implode(PHP_EOL, $json->details) will work when details has info. I didnt get these errors.