Skip to content

Commit

Permalink
feat(kobo): Improve Kobo proxy config
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed May 25, 2024
1 parent 885ede5 commit a4423b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
9 changes: 6 additions & 3 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ parameters:
BOOK_FILE_NAMING_FORMAT: '%env(BOOK_FILE_NAMING_FORMAT)%'
KOBO_PROXY_ENABLED: '%env(bool:KOBO_PROXY_ENABLED)%'
env(KOBO_PROXY_ENABLED): true
KOBO_PROXY_USE_DEV: '%env(bool:KOBO_PROXY_USE_DEV)%'
env(KOBO_PROXY_USE_DEV): false
KOBO_PROXY_USE_EVERYWHERE: '%env(bool:KOBO_PROXY_USE_EVERYWHERE)%'
env(KOBO_PROXY_USE_EVERYWHERE): false
env(KOBO_API_URL): "https://storeapi.kobo.com"
KOBO_API_URL: '%env(KOBO_API_URL)%'
env(KOBO_IMAGE_API_URL): "https://cdn.kobo.com/book-images"
KOBO_IMAGE_API_URL: '%env(KOBO_IMAGE_API_URL)%'
services:
# default configuration for services in *this* file
_defaults:
Expand Down Expand Up @@ -92,7 +94,8 @@ services:

App\Kobo\Proxy\KoboProxyConfiguration:
calls:
- [ setUseDevProxy, ['%KOBO_PROXY_USE_DEV%'] ]
- [ setStoreApiUrl, ['%KOBO_API_URL%'] ]
- [ setImageApiUrl, ['%KOBO_IMAGE_API_URL%'] ]
- [ setEnabled, ['%KOBO_PROXY_ENABLED%'] ]
- [ setUseProxyEverywhere, ['%KOBO_PROXY_USE_EVERYWHERE%'] ]

Expand Down
65 changes: 27 additions & 38 deletions src/Kobo/Proxy/KoboProxyConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@

class KoboProxyConfiguration
{
public const KOBO_STOREAPI_URL_PROD = 'https://storeapi.kobo.com';
public const KOBO_IMAGEHOST_URL_PROD = 'https://cdn.kobo.com/book-images';
public const KOBO_STOREAPI_URL_SAFE = 'https://books-proxy.example.com';
public const KOBO_IMAGEHOST_URL_SAFE = 'https://books-proxy.example.com';
private bool $useDevProxy = true;
private bool $useProxy = true;
private bool $useProxyEverywhere = true;
private bool $useProxyEverywhere = false;

private bool $useRedirect = false;
private string $imageApiUrl = '';
private string $storeApiUrl = '';

public function useProxy(): bool
{
Expand All @@ -27,19 +23,29 @@ public function useProxyEverywhere(): bool
return $this->useProxyEverywhere;
}

protected function useDevProxy(): bool
public function getStoreApiUrl(): string
{
return $this->useDevProxy;
if ($this->storeApiUrl === '') {
throw new \InvalidArgumentException('Store API URL is not set');
}

return $this->storeApiUrl;
}

public function getStoreApiUrl(): string
public function setStoreApiUrl(string $storeApiUrl): self
{
return $this->useDevProxy() ? self::KOBO_STOREAPI_URL_SAFE : self::KOBO_STOREAPI_URL_PROD;
$this->storeApiUrl = $storeApiUrl;

return $this;
}

public function getImageApiUrl(): string
{
return $this->useDevProxy() ? self::KOBO_IMAGEHOST_URL_SAFE : self::KOBO_IMAGEHOST_URL_PROD;
if ($this->storeApiUrl === '') {
throw new \InvalidArgumentException('Image API URL is not set');
}

return $this->imageApiUrl;
}

public function isImageHostUrl(Request|RequestInterface $request): bool
Expand All @@ -51,9 +57,9 @@ public function isImageHostUrl(Request|RequestInterface $request): bool
|| str_ends_with($uri, '.jpeg');
}

public function setUseDevProxy(bool $useDevProxy): KoboProxyConfiguration
public function setImageApiUrl(string $imageApiUrl): KoboProxyConfiguration
{
$this->useDevProxy = $useDevProxy;
$this->imageApiUrl = $imageApiUrl;

return $this;
}
Expand All @@ -65,30 +71,6 @@ public function setEnabled(bool $useProxy): KoboProxyConfiguration
return $this;
}

public function setUseProxyEverywhere(bool $useProxyEverywhere): KoboProxyConfiguration
{
$this->useProxyEverywhere = $useProxyEverywhere;

return $this;
}

public function isBlacklistedForEveryWhere(Request $request): bool
{
return false;
}

public function setUseRedirect(bool $useRedirect): KoboProxyConfiguration
{
$this->useRedirect = $useRedirect;

return $this;
}

public function isUseRedirect(): bool
{
return $this->useRedirect;
}

public function getNativeInitializationJson(): array
{
return [
Expand Down Expand Up @@ -242,4 +224,11 @@ public function getNativeInitializationJson(): array
'wishlist_page' => 'https://store.kobobooks.com/{region}/{language}/account/wishlist',
];
}

public function setUseProxyEverywhere(bool $useProxyEverywhere): self
{
$this->useProxyEverywhere = $useProxyEverywhere;

return $this;
}
}

0 comments on commit a4423b3

Please sign in to comment.