diff --git a/src/main/java/io/supabase/GoTrueApi.java b/src/main/java/io/supabase/GoTrueApi.java index 0b9294f..c992488 100644 --- a/src/main/java/io/supabase/GoTrueApi.java +++ b/src/main/java/io/supabase/GoTrueApi.java @@ -4,6 +4,7 @@ import io.supabase.exceptions.ApiException; import io.supabase.exceptions.UrlNotFoundException; import io.supabase.utils.RestUtils; +import org.springframework.http.HttpMethod; import java.util.HashMap; import java.util.Map; @@ -21,7 +22,7 @@ protected GoTrueApi(String url, Map headers) throws UrlNotFoundE } /** - * Send an magic-link to a given email. + * Send a magic-link to a given email. * * @param email the email the link should be sent to. * @throws ApiException if the underlying http request throws an error of any kind. @@ -32,7 +33,7 @@ public void magicLink(String email) throws ApiException { EmailDto emailDto = new EmailDto(); emailDto.setEmail(email); - RestUtils.post(emailDto, headers, urlMagicLink); + RestUtils.sendRequest(HttpMethod.POST, emailDto, Void.class, headers, urlMagicLink); } /** @@ -47,7 +48,7 @@ public void recoverPassword(String email) throws ApiException { EmailDto emailDto = new EmailDto(); emailDto.setEmail(email); - RestUtils.post(emailDto, headers, urlRecover); + RestUtils.sendRequest(HttpMethod.POST, emailDto, Void.class, headers, urlRecover); } /** @@ -59,7 +60,7 @@ public void recoverPassword(String email) throws ApiException { public SettingsDto getSettings() throws ApiException { String urlSettings = String.format("%s/settings", url); - return RestUtils.get(SettingsDto.class, headers, urlSettings); + return RestUtils.sendRequest(HttpMethod.GET, null, SettingsDto.class, headers, urlSettings); } /** @@ -83,7 +84,7 @@ public String getUrlForProvider(String provider) { public UserUpdatedDto updateUser(String jwt, UserAttributesDto attributes) throws ApiException { String urlUser = String.format("%s/user", url); - return RestUtils.put(attributes, UserUpdatedDto.class, headersWithJWT(jwt), urlUser); + return RestUtils.sendRequest(HttpMethod.PUT, attributes, UserUpdatedDto.class, headersWithJWT(jwt), urlUser); } /** @@ -98,7 +99,7 @@ public AuthenticationDto refreshAccessToken(String refreshToken) throws ApiExcep RefreshTokenDto refreshTokenDto = new RefreshTokenDto(); refreshTokenDto.setRefreshToken(refreshToken); - return RestUtils.post(refreshTokenDto, AuthenticationDto.class, headers, urlToken); + return RestUtils.sendRequest(HttpMethod.POST, refreshTokenDto, AuthenticationDto.class, headers, urlToken); } /** @@ -111,7 +112,7 @@ public AuthenticationDto refreshAccessToken(String refreshToken) throws ApiExcep public UserDto getUser(String jwt) throws ApiException { String urlUser = String.format("%s/user", url); - return RestUtils.get(UserDto.class, headersWithJWT(jwt), urlUser); + return RestUtils.sendRequest(HttpMethod.GET, null, UserDto.class, headersWithJWT(jwt), urlUser); } /** @@ -123,7 +124,7 @@ public UserDto getUser(String jwt) throws ApiException { public void signOut(String jwt) throws ApiException { String urlLogout = String.format("%s/logout", url); - RestUtils.post(headersWithJWT(jwt), urlLogout); + RestUtils.sendRequest(HttpMethod.POST, null, Void.class, headersWithJWT(jwt), urlLogout); } /** @@ -152,7 +153,7 @@ public AuthenticationDto signInWithEmail(String email, String password) throws A public AuthenticationDto signInWithEmail(CredentialsDto credentials) throws ApiException { String urlToken = String.format("%s/token?grant_type=password", url); - return RestUtils.post(credentials, AuthenticationDto.class, headers, urlToken); + return RestUtils.sendRequest(HttpMethod.POST, credentials, AuthenticationDto.class, headers, urlToken); } /** @@ -181,7 +182,7 @@ public AuthenticationDto signUpWithEmail(String email, String password) throws A public AuthenticationDto signUpWithEmail(CredentialsDto credentials) throws ApiException { String urlSignup = String.format("%s/signup", url); - return RestUtils.post(credentials, AuthenticationDto.class, headers, urlSignup); + return RestUtils.sendRequest(HttpMethod.POST, credentials, AuthenticationDto.class, headers, urlSignup); } /** diff --git a/src/main/java/io/supabase/utils/RestUtils.java b/src/main/java/io/supabase/utils/RestUtils.java index d40e2df..d2023d3 100644 --- a/src/main/java/io/supabase/utils/RestUtils.java +++ b/src/main/java/io/supabase/utils/RestUtils.java @@ -18,99 +18,29 @@ public class RestUtils { private static final ObjectMapper mapper = new ObjectMapper(); private static final RestTemplate rest = new RestTemplate(); - private RestUtils() { } - /** - * Sends a Put request. + * Sends an HTTP request. * - * @param body the body of the request, will be parsed to json. + * @param method the HTTP method (GET, POST, PUT, DELETE, etc.). + * @param body the body of the request, will be parsed to JSON. * @param responseClass the class of the response. * @param headers the headers that will be sent with the request. - * @param url the url the request will be sent to. + * @param url the URL the request will be sent to. * @param the type of the response. - * @return the response of the request parsed from json to R. - * @throws ApiException if a Exception is thrown. + * @return the response of the request parsed from JSON to R. + * @throws ApiException if an exception is thrown. */ - public static R put(Object body, Class responseClass, Map headers, String url) throws ApiException { + public static R sendRequest(HttpMethod method, Object body, Class responseClass, + Map headers, String url) throws ApiException { try { HttpEntity entity = toEntity(body, headers); - return rest.exchange(url, HttpMethod.PUT, entity, responseClass).getBody(); - } catch (RestClientResponseException | ResourceAccessException e) { - throw new ApiException("Put failed", e); - } catch (JsonProcessingException e) { - throw new ApiException("Object mapping failed", e); - } - } - - /** - * Sends a Get request. - * - * @param responseClass the class of the response. - * @param headers the headers that will be sent with the request. - * @param url the url the request will be sent to. - * @param the type of the response. - * @return the response of the request parsed from json to R. - * @throws ApiException if a Exception is thrown. - */ - public static R get(Class responseClass, Map headers, String url) throws ApiException { - try { - HttpEntity entity = toEntity(headers); - ResponseEntity res = rest.exchange(url, HttpMethod.GET, entity, responseClass); + ResponseEntity res = rest.exchange(url, method, entity, responseClass); return res.getBody(); } catch (RestClientResponseException | ResourceAccessException e) { - throw new ApiException("Get failed", e); - } - } - - - /** - * Sends a Post request. - * - * @param headers the headers that will be sent with the request. - * @param url the url the request will be sent to. - * @throws ApiException if a Exception is thrown. - */ - public static void post(Map headers, String url) throws ApiException { - try { - HttpEntity entity = toEntity(headers); - rest.postForObject(url, entity, Void.class); - } catch (RestClientResponseException | ResourceAccessException e) { - throw new ApiException("Post failed", e); - } - } - - /** - * Sends a Post request. - * - * @param body the body of the request, will be parsed to json. - * @param headers the headers that will be sent with the request. - * @param url the url the request will be sent to. - * @throws ApiException if a Exception is thrown. - */ - public static void post(Object body, Map headers, String url) throws ApiException { - post(body, Void.class, headers, url); - } - - /** - * Sends a Post request. - * - * @param body the body of the request, will be parsed to json. - * @param responseClass the class of the response. - * @param headers the headers that will be sent with the request. - * @param url the url the request will be sent to. - * @param the type of the response. - * @return the response of the request parsed from json to R. - * @throws ApiException if a Exception is thrown. - */ - public static R post(Object body, Class responseClass, Map headers, String url) throws ApiException { - try { - HttpEntity entity = toEntity(body, headers); - return rest.postForObject(url, entity, responseClass); - } catch (RestClientResponseException | ResourceAccessException e) { - throw new ApiException("Post failed", e); + throw new ApiException(method + " request failed", e); } catch (JsonProcessingException e) { throw new ApiException("Object mapping failed", e); } diff --git a/src/test/java/io/supabase/RestUtilsTest.java b/src/test/java/io/supabase/RestUtilsTest.java index bad476f..8547ce9 100644 --- a/src/test/java/io/supabase/RestUtilsTest.java +++ b/src/test/java/io/supabase/RestUtilsTest.java @@ -4,80 +4,89 @@ import io.supabase.data.CircularDependentB; import io.supabase.exceptions.ApiException; import io.supabase.utils.RestUtils; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate; -import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; class RestUtilsTest { - @AfterEach - void tearDown() { - // to ensure that the tests dont affect each other - RestTemplate rest = new RestTemplate(); - rest.delete("http://localhost:3000/users"); - } + private final String BASE_URL = "http://localhost:3000"; + private final RestTemplate restTemplate = new RestTemplate(); @Test - void constructor() { - try { - Constructor c = RestUtils.class.getDeclaredConstructor(); - c.setAccessible(true); - AtomicReference rUtils = new AtomicReference<>(null); - Assertions.assertDoesNotThrow(() -> rUtils.set(c.newInstance())); - Assertions.assertNotNull(rUtils.get()); - } catch (NoSuchMethodException e) { - Assertions.fail(); - } - } + void sendRequest_get_headers() { + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); - @Test - void get_headers() { - Assertions.assertThrows(ApiException.class, () -> RestUtils.get(Object.class, new HashMap<>(), "http://smth/")); + Assertions.assertThrows(ApiException.class, () -> + RestUtils.sendRequest(HttpMethod.GET, null, Object.class, headers, BASE_URL + "/test") + ); } @Test - void post() { - // to raise a JsonProcessingException + void sendRequest_post() { + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + CircularDependentA a = new CircularDependentA(); CircularDependentB b = new CircularDependentB(); a.setB(b); b.setA(a); - Assertions.assertThrows(ApiException.class, () -> RestUtils.post(a, CircularDependentA.class, null, "http://smth/")); - } + Assertions.assertThrows(ApiException.class, () -> + RestUtils.sendRequest(HttpMethod.POST, a, CircularDependentA.class, headers, BASE_URL + "/test") + ); + } @Test - void post_headers() { - // to raise a JsonProcessingException + void sendRequest_post_headers() { + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + CircularDependentA a = new CircularDependentA(); CircularDependentB b = new CircularDependentB(); a.setB(b); b.setA(a); - Assertions.assertThrows(ApiException.class, () -> RestUtils.post(a, CircularDependentA.class, new HashMap<>(), "http://smth/")); - } + Assertions.assertThrows(ApiException.class, () -> + RestUtils.sendRequest(HttpMethod.POST, a, CircularDependentA.class, headers, BASE_URL + "/test") + ); + } @Test - void put_json() { - // to raise a JsonProcessingException + void sendRequest_put_json() { + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + CircularDependentA a = new CircularDependentA(); CircularDependentB b = new CircularDependentB(); a.setB(b); b.setA(a); - Assertions.assertThrows(ApiException.class, () -> RestUtils.put(a, CircularDependentA.class, null, "http://smth/")); + + Assertions.assertThrows(ApiException.class, () -> + RestUtils.sendRequest(HttpMethod.PUT, a, CircularDependentA.class, headers, BASE_URL + "/test") + ); } @Test - void put() { - // some url that does not exist - Assertions.assertThrows(ApiException.class, () -> RestUtils.put(null, Object.class, null, "http://localhost:1/")); + void sendRequest_put() { + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + + Assertions.assertThrows(ApiException.class, () -> + RestUtils.sendRequest(HttpMethod.PUT, null, Object.class, headers, BASE_URL + "/test") + ); } @Test