Skip to content

Commit

Permalink
Implemeting Region enum
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Jan 22, 2025
1 parent 479dbe9 commit 476f6f7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ jobs:
uses: 'codecov/codecov-action@v5.1.2'
with:
files: './.build/logs/clover.xml'
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ env.my_codecov_token }}


21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,14 @@ use Storyblok\Mapi\MapiClient;
/** @var MapiClient $client */
$client = new MapiClient($storyblokPersonalAccessToken);
```
The second parameter (a string) for setting the region. In this case, you can use the string "US" or "AP" or "CA" or "CN".
The second parameter is for setting the region.
We provide and Enum class for setting the region.In this case, you can use the Region Enum Region::US or Region::AP or Region::CA or Region::CN.
For example:
```php
$client = new MapiClient($storyblokPersonalAccessToken, 'US');
```
Or if you prefer you can use the static methods `initEU` to access the European region.

If you need access to other regions, you can use:

- `initUS()` for the US region
- `initAP()` for the Asian Pacific region
- `initCA()` for the Canadian region
- `initCN()` for the China region.
use \Storyblok\Mapi\Data\Enum\Region;

You can use a more generic `init()` method defining the second parameter (a string) for setting the region. In this case, you can use the string "US" or "AP" or "CA" or "CN":

```php
// Using the default region EU
$client = MapiClient::init($storyblokPersonalAccessToken);
// Using the region US as an alternative way of initUS()
$client = MapiClient::init($storyblokPersonalAccessToken, 'US');
$client = new MapiClient($storyblokPersonalAccessToken, Region::US);
```

## Handling the Personal Access Token
Expand Down
35 changes: 35 additions & 0 deletions src/Data/Enum/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Storyblok\Mapi\Data\Enum;

enum Region: string
{
case EU = 'EU';
case US = 'US';
case AP = 'AP';
case CA = 'CA';
case CN = 'CN';

/**
* Get all region values as an array.
*
* @return array<string>
*/
public static function values(): array
{
return array_column(self::cases(), 'value');
}

/**
* Check if a value is a valid region.
*
* @param string $value the code of region to be validated
* @return bool true if the region is one of the valid region (Upper case)
*/
public static function isValid(string $value): bool
{
return in_array($value, self::values(), true);
}
}
46 changes: 4 additions & 42 deletions src/MapiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Storyblok\Mapi;

use Storyblok\Mapi\Data\Enum\Region;
use Storyblok\Mapi\Endpoints\AssetApi;
use Storyblok\Mapi\Endpoints\ManagementApi;
use Storyblok\Mapi\Endpoints\SpaceApi;
Expand All @@ -18,10 +19,10 @@ class MapiClient

public function __construct(
string $personalAccessToken,
string $region = "EU",
Region $region = Region::EU,
?string $baseUri = null,
) {
$baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region);
$baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region->value);
$this->httpClient = HttpClient::create()
->withOptions([
'base_uri' => $baseUriMapi,
Expand All @@ -34,50 +35,11 @@ public function __construct(
]);
}

public static function initEU(string $personalAccessToken): self
{
return self::init(
$personalAccessToken,
"EU",
);
}

public static function initUS(string $personalAccessToken): self
{
return self::init(
$personalAccessToken,
"US",
);
}

public static function initAP(string $personalAccessToken): self
{
return self::init(
$personalAccessToken,
"AP",
);
}

public static function initCA(string $personalAccessToken): self
{
return self::init(
$personalAccessToken,
"CA",
);
}

public static function initCN(string $personalAccessToken): self
{
return self::init(
$personalAccessToken,
"CN",
);
}


public static function init(
string $personalAccessToken,
string $region = "EU",
Region $region = Region::EU,
?string $baseUri = null,
): self {

Expand Down

0 comments on commit 476f6f7

Please sign in to comment.