Skip to content

Commit

Permalink
Merge pull request #14 from still-code/more-api
Browse files Browse the repository at this point in the history
allow to create, update, and delete sites throw umami API
  • Loading branch information
atmonshi authored Apr 17, 2023
2 parents 3c4f5e6 + a176fd8 commit 9b8953a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
8 changes: 1 addition & 7 deletions src/Umami.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ class Umami
*
* @throws RequestException
*/
public static function auth()
public function __construct()
{
abort_if(
config('umami.url') === null ||
config('umami.username') === null ||
config('umami.password') === null, 421, 'please make sur to set all umami config');

if (session()->has('umami_token')) {
return session('umami_token');
}

$response = Http::post(config('umami.url').'/auth/login', [
'username' => config('umami.username'),
'password' => config('umami.password'),
Expand All @@ -48,8 +44,6 @@ public static function auth()
*/
public static function query(string $siteID, string $part = 'stats', array $options = null, bool $force = false): mixed
{
self::auth();

$options = self::setOptions($part, $options);
$response = Http::withToken(session('umami_token'))
->get(config('umami.url').'/websites/'.$siteID.'/'.$part, $options);
Expand Down
48 changes: 46 additions & 2 deletions src/Websites.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ trait Websites
*/
public static function websites(bool $force = false): mixed
{
self::auth();

$response = Http::withToken(session('umami_token'))
->get(config('umami.url').'/websites');

Expand All @@ -32,4 +30,50 @@ public static function websites(bool $force = false): mixed
return $response->json();
});
}

/**
* @return array|mixed
*
* @throws RequestException
*/
public static function createWebsite(string $domain, string $name, bool $share = false, bool $public = false): mixed
{
$response = Http::withToken(session('umami_token'))
->post(config('umami.url').'/websites', [
'domain' => $domain,
'name' => $name,
'share' => $share,
'public' => $public,
]);

$response->throw();

return $response->json();
}

/**
* @throws RequestException
*/
public static function updateWebsite(string $websiteUuid, array $data): mixed
{
$response = Http::withToken(session('umami_token'))
->post(config('umami.url').'/websites/'.$websiteUuid, $data);

$response->throw();

return $response->json();
}

/**
* @throws RequestException
*/
public static function deleteWebsite($websiteUuid): mixed
{
$response = Http::withToken(session('umami_token'))
->delete(config('umami.url').'/websites/'.$websiteUuid);

$response->throw();

return $response->json();
}
}

0 comments on commit 9b8953a

Please sign in to comment.