diff --git a/src/Api/Komoot.php b/src/Api/Komoot.php index 1f4b8af..acc02ca 100644 --- a/src/Api/Komoot.php +++ b/src/Api/Komoot.php @@ -5,8 +5,6 @@ namespace Woeler\KomootPhp\Api; use GuzzleHttp\Client; -use GuzzleHttp\Cookie\CookieJar; -use GuzzleHttp\Cookie\SetCookie; use Woeler\KomootPhp\Enums\PrivacySetting; use Woeler\KomootPhp\Enums\Sport; use Woeler\KomootPhp\Enums\TourType; @@ -14,64 +12,10 @@ class Komoot { private Client $client; - private CookieJar $cookies; - private ?int $userid = null; - public function __construct(private readonly string $email, private readonly string $password, ?Client $client = null) + public function __construct(private readonly string $email, private readonly string $password, private readonly int $userid) { $this->client = $client ?? new Client(); - $this->cookies = new CookieJar(); - } - - public function login(): void - { - $response = $this->client->post('https://account.komoot.com/v1/signin', [ - 'json' => ['email' => $this->email, 'password' => $this->password, 'reason' => 'header'], - ]); - - $headerSetCookies = $response->getHeader('Set-Cookie'); - - foreach ($headerSetCookies as $header) { - $this->cookies->setCookie(SetCookie::fromString($header)); - } - - $this->setAccountIdViaCookie(); - - $response = $this->client->get('https://account.komoot.com/actions/transfer', [ - 'cookies' => $this->cookies, - 'query' => [ - 'type' => 'signin', - 'reason' => 'header', - ], - ]); - - $headerSetCookies = $response->getHeader('Set-Cookie'); - - foreach ($headerSetCookies as $header) { - $this->cookies->setCookie(SetCookie::fromString($header)); - } - } - - private function setAccountIdViaCookie(): void - { - $response = json_decode($this->client->get('https://account.komoot.com/api/account/v1/session', [ - 'cookies' => $this->cookies, - 'query' => ['hl' => 'en'], - ])->getBody()->getContents(), true); - - $this->userid = (int) $response['_embedded']['profile']['username']; - } - - public function getCookieJar(): CookieJar - { - return $this->cookies; - } - - public function setCookieJar(CookieJar $cookieJar): self - { - $this->cookies = $cookieJar; - - return $this; } public function getUserId(): ?int @@ -79,13 +23,6 @@ public function getUserId(): ?int return $this->userid; } - public function setUserId(int $userId): self - { - $this->userid = $userId; - - return $this; - } - public function getTours(int $page = 0, int $limit = 50, ?TourType $type = null): array { $params = ['page' => $page, 'limit' => $limit]; @@ -95,7 +32,7 @@ public function getTours(int $page = 0, int $limit = 50, ?TourType $type = null) } return json_decode($this->client->get('https://www.komoot.com/api/v007/users/'.$this->userid.'/tours/', [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'query' => $params, ])->getBody()->getContents(), true); } @@ -120,35 +57,35 @@ public function getAllTours(?TourType $type = null): array public function getTour(int $tourId): array { return json_decode($this->client->get('https://www.komoot.com/api/v007/tours/'.$tourId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(), true); } public function getTourGpx(int $tourId): string { return $this->client->get('https://www.komoot.com/api/v007/tours/'.$tourId.'.gpx', [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(); } public function getTourPhotos(int $tourId): array { return json_decode($this->client->get('https://www.komoot.com/api/v007/tours/'.$tourId.'/cover_images/', [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(), true); } public function deleteTour(int $tourId): void { $this->client->delete('https://www.komoot.com/api/v007/tours/'.$tourId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ]); } public function renameTour(int $tourId, string $name): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/tours/'.$tourId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['name' => $name], ])->getBody()->getContents(), true); } @@ -156,7 +93,7 @@ public function renameTour(int $tourId, string $name): array public function changeTourPrivacy(int $tourId, PrivacySetting $privacy): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/tours/'.$tourId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['status' => $privacy->value], ])->getBody()->getContents(), true); } @@ -164,7 +101,7 @@ public function changeTourPrivacy(int $tourId, PrivacySetting $privacy): array public function changeTourSport(int $tourId, Sport $sport): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/tours/'.$tourId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['sport' => $sport->value], ])->getBody()->getContents(), true); } @@ -172,7 +109,7 @@ public function changeTourSport(int $tourId, Sport $sport): array public function getUser(int $userId): array { return json_decode($this->client->get('https://www.komoot.com/api/v007/users/'.$userId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(), true); } @@ -184,21 +121,21 @@ public function getSelfUser(): array public function getCollection(int $collectionId): array { return json_decode($this->client->get('https://www.komoot.com/api/v007/collections/'.$collectionId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(), true); } public function getCollectionTours(int $collectionId): array { return json_decode($this->client->get('https://www.komoot.com/api/v007/collections/'.$collectionId.'/compilation/', [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ])->getBody()->getContents(), true); } public function renameCollection(int $collectionId, string $name): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/collections/'.$collectionId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['name' => $name], ])->getBody()->getContents(), true); } @@ -206,7 +143,7 @@ public function renameCollection(int $collectionId, string $name): array public function changeCollectionDescription(int $collectionId, ?string $description): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/collections/'.$collectionId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['intro_plain' => $description ?? ''], ])->getBody()->getContents(), true); } @@ -214,7 +151,7 @@ public function changeCollectionDescription(int $collectionId, ?string $descript public function changeCollectionPrivacy(int $collectionId, PrivacySetting $privacy): array { return json_decode($this->client->patch('https://www.komoot.com/api/v007/collections/'.$collectionId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], 'json' => ['status' => $privacy->value], ])->getBody()->getContents(), true); } @@ -222,7 +159,7 @@ public function changeCollectionPrivacy(int $collectionId, PrivacySetting $priva public function deleteCollection(int $collectionId): void { $this->client->delete('https://www.komoot.com/api/v007/collections/'.$collectionId, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ]); } @@ -233,7 +170,7 @@ public function customRequest(string $endpoint, array $guzzleConfig = [], string } return json_decode($this->client->{strtolower($method)}($endpoint, [ - 'cookies' => $this->cookies, + ['auth' => [$this->email, $this->password]], ] + $guzzleConfig)->getBody()->getContents(), true); } }