Skip to content

made some changes with issue #3 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/main/java/io/supabase/GoTrueApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@ protected GoTrueApi(String url, Map<String, String> 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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
90 changes: 10 additions & 80 deletions src/main/java/io/supabase/utils/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <R> 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> R put(Object body, Class<R> responseClass, Map<String, String> headers, String url) throws ApiException {
public static <R> R sendRequest(HttpMethod method, Object body, Class<R> responseClass,
Map<String, String> headers, String url) throws ApiException {
try {
HttpEntity<String> 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 <R> 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> R get(Class<R> responseClass, Map<String, String> headers, String url) throws ApiException {
try {
HttpEntity<String> entity = toEntity(headers);
ResponseEntity<R> res = rest.exchange(url, HttpMethod.GET, entity, responseClass);
ResponseEntity<R> 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<String, String> headers, String url) throws ApiException {
try {
HttpEntity<String> 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<String, String> 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 <R> 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> R post(Object body, Class<R> responseClass, Map<String, String> headers, String url) throws ApiException {
try {
HttpEntity<String> 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);
}
Expand Down
83 changes: 46 additions & 37 deletions src/test/java/io/supabase/RestUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<RestUtils> c = RestUtils.class.getDeclaredConstructor();
c.setAccessible(true);
AtomicReference<RestUtils> rUtils = new AtomicReference<>(null);
Assertions.assertDoesNotThrow(() -> rUtils.set(c.newInstance()));
Assertions.assertNotNull(rUtils.get());
} catch (NoSuchMethodException e) {
Assertions.fail();
}
}
void sendRequest_get_headers() {
Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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
Expand Down