Skip to content

Commit

Permalink
[core] Update drop Database API and remove api in URL (apache#4691)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 authored Dec 12, 2024
1 parent ce1c839 commit 96bcfda
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 184 deletions.
38 changes: 11 additions & 27 deletions paimon-core/src/main/java/org/apache/paimon/rest/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,9 @@ public HttpClient(HttpClientOptions httpClientOptions) {
@Override
public <T extends RESTResponse> T get(
String path, Class<T> responseType, Map<String, String> headers) {
try {
Request request =
new Request.Builder()
.url(uri + path)
.get()
.headers(Headers.of(headers))
.build();
return exec(request, responseType);
} catch (Exception e) {
throw new RuntimeException(e);
}
Request request =
new Request.Builder().url(uri + path).get().headers(Headers.of(headers)).build();
return exec(request, responseType);
}

@Override
Expand All @@ -90,26 +82,16 @@ public <T extends RESTResponse> T post(
.headers(Headers.of(headers))
.build();
return exec(request, responseType);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (JsonProcessingException e) {
throw new RESTException(e, "build request failed.");
}
}

@Override
public <T extends RESTResponse> T delete(
String path, RESTRequest body, Map<String, String> headers) {
try {
RequestBody requestBody = buildRequestBody(body);
Request request =
new Request.Builder()
.url(uri + path)
.delete(requestBody)
.headers(Headers.of(headers))
.build();
return exec(request, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
public <T extends RESTResponse> T delete(String path, Map<String, String> headers) {
Request request =
new Request.Builder().url(uri + path).delete().headers(Headers.of(headers)).build();
return exec(request, null);
}

@Override
Expand All @@ -135,6 +117,8 @@ private <T extends RESTResponse> T exec(Request request, Class<T> responseType)
} else {
throw new RESTException("response body is null.");
}
} catch (RESTException e) {
throw e;
} catch (Exception e) {
throw new RESTException(e, "rest exception");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.paimon.rest.exceptions.AlreadyExistsException;
import org.apache.paimon.rest.exceptions.NoSuchResourceException;
import org.apache.paimon.rest.requests.CreateDatabaseRequest;
import org.apache.paimon.rest.requests.DropDatabaseRequest;
import org.apache.paimon.rest.responses.ConfigResponse;
import org.apache.paimon.rest.responses.CreateDatabaseResponse;
import org.apache.paimon.rest.responses.DatabaseName;
Expand All @@ -47,6 +46,7 @@
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -160,11 +160,15 @@ public Database getDatabase(String name) throws DatabaseNotExistException {
@Override
public void dropDatabase(String name, boolean ignoreIfNotExists, boolean cascade)
throws DatabaseNotExistException, DatabaseNotEmptyException {
DropDatabaseRequest request = new DropDatabaseRequest(ignoreIfNotExists, cascade);
try {
client.delete(resourcePaths.database(name), request, headers());
if (!cascade && !this.listTables(name).isEmpty()) {
throw new DatabaseNotEmptyException(name);
}
client.delete(resourcePaths.database(name), headers());
} catch (NoSuchResourceException e) {
throw new DatabaseNotExistException(name);
if (!ignoreIfNotExists) {
throw new DatabaseNotExistException(name);
}
}
}

Expand All @@ -180,7 +184,7 @@ public Path getTableLocation(Identifier identifier) {

@Override
public List<String> listTables(String databaseName) throws DatabaseNotExistException {
throw new UnsupportedOperationException();
return new ArrayList<String>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public interface RESTClient extends Closeable {
<T extends RESTResponse> T post(
String path, RESTRequest body, Class<T> responseType, Map<String, String> headers);

<T extends RESTResponse> T delete(String path, RESTRequest body, Map<String, String> headers);
<T extends RESTResponse> T delete(String path, Map<String, String> headers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/** Resource paths for REST catalog. */
public class ResourcePaths {

public static final String V1_CONFIG = "/api/v1/config";
public static final String V1_CONFIG = "/v1/config";
private static final StringJoiner SLASH = new StringJoiner("/");

public static ResourcePaths forCatalogProperties(String prefix) {
Expand All @@ -37,10 +37,10 @@ public ResourcePaths(String prefix) {
}

public String databases() {
return SLASH.add("api").add("v1").add(prefix).add("databases").toString();
return SLASH.add("v1").add(prefix).add("databases").toString();
}

public String database(String databaseName) {
return SLASH.add("api").add("v1").add(prefix).add("databases").add(databaseName).toString();
return SLASH.add("v1").add(prefix).add("databases").add(databaseName).toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

/** Test for {@link HttpClient}. */
public class HttpClientTest {

private MockWebServer mockWebServer;
private HttpClient httpClient;
private ObjectMapper objectMapper = RESTObjectMapper.create();
Expand Down Expand Up @@ -113,14 +114,14 @@ public void testPostFail() {
@Test
public void testDeleteSuccess() {
mockHttpCallWithCode(mockResponseDataStr, 200);
MockRESTData response = httpClient.delete(MOCK_PATH, mockResponseData, headers);
MockRESTData response = httpClient.delete(MOCK_PATH, headers);
verify(errorHandler, times(0)).accept(any());
}

@Test
public void testDeleteFail() {
mockHttpCallWithCode(mockResponseDataStr, 400);
httpClient.delete(MOCK_PATH, mockResponseData, headers);
httpClient.delete(MOCK_PATH, headers);
verify(errorHandler, times(1)).accept(any());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.apache.paimon.rest;

import org.apache.paimon.rest.requests.CreateDatabaseRequest;
import org.apache.paimon.rest.requests.DropDatabaseRequest;
import org.apache.paimon.rest.responses.CreateDatabaseResponse;
import org.apache.paimon.rest.responses.DatabaseName;
import org.apache.paimon.rest.responses.ErrorResponse;
import org.apache.paimon.rest.responses.GetDatabaseResponse;
import org.apache.paimon.rest.responses.ListDatabasesResponse;

Expand All @@ -46,12 +46,6 @@ public static CreateDatabaseRequest createDatabaseRequest(String name) {
return new CreateDatabaseRequest(name, ignoreIfExists, options);
}

public static DropDatabaseRequest dropDatabaseRequest() {
boolean ignoreIfNotExists = true;
boolean cascade = true;
return new DropDatabaseRequest(ignoreIfNotExists, cascade);
}

public static CreateDatabaseResponse createDatabaseResponse(String name) {
Map<String, String> options = new HashMap<>();
options.put("a", "b");
Expand All @@ -71,4 +65,8 @@ public static ListDatabasesResponse listDatabasesResponse(String name) {
databaseNameList.add(databaseName);
return new ListDatabasesResponse(databaseNameList);
}

public static ErrorResponse noSuchResourceExceptionErrorResponse() {
return new ErrorResponse("message", 404, new ArrayList<>());
}
}
Loading

0 comments on commit 96bcfda

Please sign in to comment.