Skip to content

Commit 9b8953a

Browse files
authored
Merge pull request #14 from still-code/more-api
allow to create, update, and delete sites throw umami API
2 parents 3c4f5e6 + a176fd8 commit 9b8953a

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/Umami.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ class Umami
1616
*
1717
* @throws RequestException
1818
*/
19-
public static function auth()
19+
public function __construct()
2020
{
2121
abort_if(
2222
config('umami.url') === null ||
2323
config('umami.username') === null ||
2424
config('umami.password') === null, 421, 'please make sur to set all umami config');
2525

26-
if (session()->has('umami_token')) {
27-
return session('umami_token');
28-
}
29-
3026
$response = Http::post(config('umami.url').'/auth/login', [
3127
'username' => config('umami.username'),
3228
'password' => config('umami.password'),
@@ -48,8 +44,6 @@ public static function auth()
4844
*/
4945
public static function query(string $siteID, string $part = 'stats', array $options = null, bool $force = false): mixed
5046
{
51-
self::auth();
52-
5347
$options = self::setOptions($part, $options);
5448
$response = Http::withToken(session('umami_token'))
5549
->get(config('umami.url').'/websites/'.$siteID.'/'.$part, $options);

src/Websites.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ trait Websites
1717
*/
1818
public static function websites(bool $force = false): mixed
1919
{
20-
self::auth();
21-
2220
$response = Http::withToken(session('umami_token'))
2321
->get(config('umami.url').'/websites');
2422

@@ -32,4 +30,50 @@ public static function websites(bool $force = false): mixed
3230
return $response->json();
3331
});
3432
}
33+
34+
/**
35+
* @return array|mixed
36+
*
37+
* @throws RequestException
38+
*/
39+
public static function createWebsite(string $domain, string $name, bool $share = false, bool $public = false): mixed
40+
{
41+
$response = Http::withToken(session('umami_token'))
42+
->post(config('umami.url').'/websites', [
43+
'domain' => $domain,
44+
'name' => $name,
45+
'share' => $share,
46+
'public' => $public,
47+
]);
48+
49+
$response->throw();
50+
51+
return $response->json();
52+
}
53+
54+
/**
55+
* @throws RequestException
56+
*/
57+
public static function updateWebsite(string $websiteUuid, array $data): mixed
58+
{
59+
$response = Http::withToken(session('umami_token'))
60+
->post(config('umami.url').'/websites/'.$websiteUuid, $data);
61+
62+
$response->throw();
63+
64+
return $response->json();
65+
}
66+
67+
/**
68+
* @throws RequestException
69+
*/
70+
public static function deleteWebsite($websiteUuid): mixed
71+
{
72+
$response = Http::withToken(session('umami_token'))
73+
->delete(config('umami.url').'/websites/'.$websiteUuid);
74+
75+
$response->throw();
76+
77+
return $response->json();
78+
}
3579
}

0 commit comments

Comments
 (0)