Skip to content

Commit 6a188ba

Browse files
committed
feat: add JWT-based fetch util for GitHub API interactions
- Added `fetch_with_jwt` function to streamline API calls with JWT authentication. - Updated `get_delivery`, `list_deliveries`, and `redeliver` methods to utilize the new JWT fetch functionality, enhancing security and code reuse. - Refactored `get_installation_token` and `get_installation` methods to leverage `fetch_with_jwt`, simplifying the code and improving maintainability.
1 parent 1784fad commit 6a188ba

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/algora/integrations/github/client.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ defmodule Algora.Github.Client do
112112
)
113113
end
114114

115+
def fetch_with_jwt(path, method \\ "GET", body \\ nil) do
116+
with {:ok, jwt, _claims} <- Crypto.generate_jwt() do
117+
fetch(jwt, path, method, body)
118+
end
119+
end
120+
115121
defp build_query(opts), do: if(opts == [], do: "", else: "?" <> URI.encode_query(opts))
116122

117123
@impl true
@@ -185,20 +191,14 @@ defmodule Algora.Github.Client do
185191

186192
@impl true
187193
def get_installation_token(installation_id) do
188-
path = "/app/installations/#{installation_id}/access_tokens"
189-
190-
with {:ok, jwt, _claims} <- Crypto.generate_jwt(),
191-
{:ok, %{"token" => token}} <- fetch(jwt, path, "POST") do
194+
with {:ok, %{"token" => token}} <- fetch_with_jwt("/app/installations/#{installation_id}/access_tokens", "POST") do
192195
{:ok, token}
193196
end
194197
end
195198

196199
@impl true
197200
def get_installation(installation_id) do
198-
path = "/app/installations/#{installation_id}"
199-
200-
with {:ok, jwt, _claims} <- Crypto.generate_jwt(),
201-
{:ok, %{"token" => token}} <- fetch(jwt, path, "GET") do
201+
with {:ok, %{"token" => token}} <- fetch_with_jwt("/app/installations/#{installation_id}") do
202202
{:ok, token}
203203
end
204204
end

0 commit comments

Comments
 (0)