Skip to content

Commit 2b7736f

Browse files
committed
Refactoring Space , Spaces and SpaceResponse
1 parent d97a6c9 commit 2b7736f

File tree

13 files changed

+466
-391
lines changed

13 files changed

+466
-391
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.1.0 - WIP
4+
- Refactor exception handling: Bubble up the exception
5+
- Refactor Space, Spaces and SpaceResponse
6+
- Adding some helper method to Space like region() and planLevel()
7+
38
## 0.0.7 - 2025-02-09
49

510
- AssetApi full coverage tests

README.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ use Storyblok\ManagementApi\ManagementApiClient;
4343
/** @var ManagementApiClient $client */
4444
$client = new ManagementApiClient($storyblokPersonalAccessToken);
4545
```
46+
47+
### Setting the space region
48+
4649
The second optional parameter is for setting the region.
50+
51+
> If you are interested to know more about Storyblok regions, check this FAQ: <https://www.storyblok.com/faq/where-are-your-servers-or-aws-sites-located>
52+
4753
We provide an Enum class to set the region. In this case, you can use the `Region` enum: `Region::US` or `Region::AP` or `Region::CA` or `Region::CN`.
4854

49-
For example, for using the US region, you can use:
55+
For example, for using the **US** region, you can use:
5056
```php
5157

5258
use \Storyblok\ManagementApi\Data\Enum\Region;
@@ -90,7 +96,7 @@ The Storyblok **Management API Client** provides two main approaches for interac
9096

9197
The `ManagementApi` class offers a flexible, generic interface for managing content. It includes methods to get, create, update, and delete content. With this approach, you can define the endpoint path and pass query string parameters as a generic array. The response is returned as a `StoryblokData` object, allowing you to access the JSON payload, status codes, and other details directly.
9298

93-
Alternatively, you can leverage dedicated classes like `SpaceApi`, which are tailored to specific resources. For instance, the `SpaceApi` class provides methods for managing spaces and returns specialized data objects, such as `SpaceData` (for a single space) or `SpacesData` (for a collection of spaces). These classes simplify interactions with specific endpoints by offering resource-specific methods.
99+
Alternatively, you can leverage dedicated classes like `SpaceApi`, which are tailored to specific resources. For instance, the `SpaceApi` class provides methods for managing spaces and returns specialized data objects, such as `Space` (for a single space) or `Spaces` (for a collection of spaces). These classes simplify interactions with specific endpoints by offering resource-specific methods.
94100

95101
If a dedicated API class like `SpaceApi` or `StoryApi` does not exist for your desired endpoint, you can always fall back to the more versatile `ManagementApi` class.
96102

@@ -112,35 +118,56 @@ Let's start analyzing the specialized classes, like for example the `SpaceApi`.
112118
Fetch a list of all spaces associated with your account in the current region (the region is initialized in the `ManagementApiClient`):
113119

114120
```php
121+
122+
$clientEU = new ManagementApiClient($accessToken);
123+
$spaceApi = new SpaceApi($clientEU);
115124
// Retrieve all spaces
116125
$response = $spaceApi->all();
117-
echo "STATUS CODE : " . $response->getResponseStatusCode() . PHP_EOL;
118-
echo "LAST URL : " . $response->getLastCalledUrl() . PHP_EOL;
119-
$data = $response->data();
126+
// here you can access to `$response` method if you need
127+
$spaces = $response->data();
128+
// Here you can access to the list of spaces via `$spaces`
120129
```
121130

122131
### Loop through the spaces
123132

124133
Iterate through the list of spaces to access their details:
125134

126135
```php
127-
foreach ($data as $key => $space) {
128-
echo $space->get("region") . " " . $space->get("id") . " " . $space->get("name") . PHP_EOL;
129-
}
130-
echo "SPACE NAME : " . $data->get("0.name") . PHP_EOL;
131-
echo "SPACES : " . $data->count() . PHP_EOL;
136+
$clientEU = new ManagementApiClient($accessToken);
137+
138+
$spaceApi = new SpaceApi($clientEU);
139+
$spaces = $spaceApi->all()->data();
140+
141+
$spaces->forEach( function (Space $space) {
142+
printf("SPACE : %s (%s) - %s - created at: %s as %s" ,
143+
$space->id(),
144+
$space->region(),
145+
$space->name(),
146+
$space->createdAt(),
147+
$space->planDescription()
148+
);
149+
echo PHP_EOL;
150+
151+
});
152+
132153
```
133154

134155
### Get one specific Space
135156

136157
Retrieve detailed information about a specific space using its ID:
137158

138159
```php
139-
// Get details of a specific space
140-
$response = $spaceApi->get($spaceID);
141-
$space = $response->data();
142-
echo $space->get("name") . PHP_EOL;
143-
echo $space->get("plan") . " " . $space->get("plan_level") . PHP_EOL;
160+
$spaceID = "12345"
161+
$spaceId = "321388";
162+
$spaceApi = new SpaceApi($clientEU);
163+
$space = $spaceApi->get($spaceId)->data();
164+
165+
printf(" The name for the Space id : %s is : %s . Plan: %s - %s" ,
166+
$spaceId,
167+
$space->name(),
168+
$space->planLevel(),
169+
$space->planDescription()
170+
);
144171
```
145172

146173
### Triggering the backup

0 commit comments

Comments
 (0)