Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.PhoneNumber;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.internal.DateUtil;
import io.weaviate.client6.v1.internal.MapUtil;
import io.weaviate.client6.v1.internal.grpc.ByteStringUtil;
import io.weaviate.client6.v1.internal.grpc.Rpc;
Expand Down Expand Up @@ -175,7 +176,7 @@ private static com.google.protobuf.Value marshalValue(Object value) {
} else if (value instanceof UUID v) {
protoValue.setStringValue(v.toString());
} else if (value instanceof OffsetDateTime v) {
protoValue.setStringValue(v.toString());
protoValue.setStringValue(DateUtil.toRFC3339(v));
} else if (value instanceof Boolean v) {
protoValue.setBoolValue(v.booleanValue());
} else if (value instanceof Number v) {
Expand Down Expand Up @@ -208,7 +209,7 @@ private static com.google.protobuf.Value marshalValue(Object value) {
} else if (listValue instanceof UUID lv) {
protoListValue.setStringValue(lv.toString());
} else if (listValue instanceof OffsetDateTime lv) {
protoListValue.setStringValue(lv.toString());
protoListValue.setStringValue(DateUtil.toRFC3339(lv));
} else if (listValue instanceof Boolean lv) {
protoListValue.setBoolValue(lv);
} else if (listValue instanceof Number lv) {
Expand Down Expand Up @@ -238,7 +239,7 @@ private static com.google.protobuf.Value marshalValue(Object value) {
.map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(lv.toString()).build()).toList();
} else if (value instanceof OffsetDateTime[] v) {
values = Arrays.stream(v)
.map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(lv.toString()).build()).toList();
.map(lv -> com.google.protobuf.Value.newBuilder().setStringValue(DateUtil.toRFC3339(lv)).build()).toList();
} else if (value instanceof Boolean[] v) {
values = Arrays.stream(v)
.map(lv -> com.google.protobuf.Value.newBuilder().setBoolValue(lv).build()).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;

import io.weaviate.client6.v1.internal.DateUtil;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase.Filters;

Expand Down Expand Up @@ -908,7 +909,7 @@ private DateOperand(OffsetDateTime value) {

@Override
public void appendTo(WeaviateProtoBase.Filters.Builder filter) {
filter.setValueText(value.toString());
filter.setValueText(DateUtil.toRFC3339(value));
}

@Override
Expand All @@ -930,7 +931,7 @@ private DateArrayOperand(OffsetDateTime... values) {
}

private List<String> formatted() {
return values.stream().map(OffsetDateTime::toString).toList();
return values.stream().map(DateUtil::toRFC3339).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
package io.weaviate.client6.v1.api.rbac.users;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

import io.weaviate.client6.v1.internal.ObjectBuilder;
import io.weaviate.client6.v1.internal.json.JSON;
import io.weaviate.client6.v1.internal.rest.Endpoint;
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;
import io.weaviate.client6.v1.internal.rest.UrlEncoder;

public record CreateDbUserRequest(String userId) {
public record CreateDbUserRequest(String userId, OffsetDateTime expiration) {

public static final Endpoint<CreateDbUserRequest, String> _ENDPOINT = SimpleEndpoint.noBody(
public static final Endpoint<CreateDbUserRequest, String> _ENDPOINT = new SimpleEndpoint<>(
__ -> "POST",
request -> "/users/db/" + UrlEncoder.encodeValue(request.userId),
request -> Collections.emptyMap(),
request -> {
if (request.expiration() == null) {
return null;
}
return JSON.serialize(Map.of("expiresAt", request.expiration()));
},
(statusCode, response) -> JSON.deserialize(response, CreateDbUserResponse.class).apiKey());

public static CreateDbUserRequest of(String userId, Function<Builder, ObjectBuilder<CreateDbUserRequest>> fn) {
return fn.apply(new Builder(userId)).build();
}

public CreateDbUserRequest(Builder builder) {
this(builder.userId, builder.expiration);
}

public static final class Builder implements ObjectBuilder<CreateDbUserRequest> {
private final String userId;
private OffsetDateTime expiration;

public Builder(String userId) {
this.userId = userId;
}

/* Set user expiration time. */
public Builder expiration(OffsetDateTime expiration) {
this.expiration = expiration;
return this;
}

@Override
public CreateDbUserRequest build() {
return new CreateDbUserRequest(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public record DbUser(
@SerializedName("roles") List<String> roleNames,
@SerializedName("createdAt") OffsetDateTime createdAt,
@SerializedName("lastUsedAt") OffsetDateTime lastUsedAt,
@SerializedName("expiresAt") OffsetDateTime userExpiresAt,
@SerializedName("apiKeyFirstLetters") String apiKeyFirstLetters) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.weaviate.client6.v1.api.rbac.users;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -27,14 +28,33 @@ public DbUsersClient(RestTransport restTransport) {
* or the server being unavailable.
*/
public String create(String userId) throws IOException {
return this.restTransport.performRequest(new CreateDbUserRequest(userId), CreateDbUserRequest._ENDPOINT);
return this.restTransport.performRequest(CreateDbUserRequest.of(userId, ObjectBuilder.identity()),
CreateDbUserRequest._ENDPOINT);
}

/**
* Create a new "db" user.
*
* @param userId User ID.
* @param fn Lambda expression for optional parameters.
* @return API key for the created user.
* @throws WeaviateApiException in case the server returned with an
* error status code.
* @throws IOException in case the request was not sent successfully
* due to a malformed request, a networking error
* or the server being unavailable.
*/
public String create(String userId, Function<CreateDbUserRequest.Builder, ObjectBuilder<CreateDbUserRequest>> fn)
throws IOException {
return this.restTransport.performRequest(CreateDbUserRequest.of(userId, fn), CreateDbUserRequest._ENDPOINT);
}

/**
* Delete a "db" user.
*
* @param userId User ID.
* @return {@code true} if the user was deleted, {@code false} if there was no user to delete.
* @return {@code true} if the user was deleted, {@code false} if there was no
* user to delete.
* @throws WeaviateApiException in case the server returned with an
* error status code.
* @throws IOException in case the request was not sent successfully
Expand All @@ -49,7 +69,8 @@ public boolean delete(String userId) throws IOException {
* Activate a "db" user.
*
* @param userId User ID.
* @return {@code true} if the user was activated, {@code false} if the user was already active.
* @return {@code true} if the user was activated, {@code false} if the user was
* already active.
* @throws WeaviateApiException in case the server returned with an
* error status code.
* @throws IOException in case the request was not sent successfully
Expand All @@ -64,7 +85,8 @@ public boolean activate(String userId) throws IOException {
* Deactivate a "db" user.
*
* @param userId User ID.
* @return {@code true} if the user was deactivated, {@code false} if the user was already deactivated.
* @return {@code true} if the user was deactivated, {@code false} if the user
* was already deactivated.
* @throws WeaviateApiException in case the server returned with an
* error status code.
* @throws IOException in case the request was not sent successfully
Expand All @@ -89,6 +111,22 @@ public String rotateKey(String userId) throws IOException {
return this.restTransport.performRequest(new RotateDbUserKeyRequest(userId), RotateDbUserKeyRequest._ENDPOINT);
}

/**
* Update user expiration time.
*
* @param userId User ID.
* @param expiration New expiration. Set {@code null} to disable expiration.
* @throws WeaviateApiException in case the server returned with an
* error status code.
* @throws IOException in case the request was not sent successfully
* due to a malformed request, a networking error
* or the server being unavailable.
*/
public void updateUserExpiration(String userId, OffsetDateTime expiration) throws IOException {
this.restTransport.performRequest(new UpdateDbUserExpirationRequest(userId, expiration),
UpdateDbUserExpirationRequest._ENDPOINT);
}

/**
* Fetch "db" user info.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.weaviate.client6.v1.api.rbac.users;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -22,7 +23,21 @@ public DbUsersClientAsync(RestTransport restTransport) {
* @return API key for the created user.
*/
public CompletableFuture<String> create(String userId) throws IOException {
return this.restTransport.performRequestAsync(new CreateDbUserRequest(userId), CreateDbUserRequest._ENDPOINT);
return this.restTransport.performRequestAsync(CreateDbUserRequest.of(userId, ObjectBuilder.identity()),
CreateDbUserRequest._ENDPOINT);
}

/**
* Create a new "db" user.
*
* @param userId User ID.
* @param fn Lambda expression for optional parameters.
* @return API key for the created user.
*/
public CompletableFuture<String> create(String userId,
Function<CreateDbUserRequest.Builder, ObjectBuilder<CreateDbUserRequest>> fn)
throws IOException {
return this.restTransport.performRequestAsync(CreateDbUserRequest.of(userId, fn), CreateDbUserRequest._ENDPOINT);
}

/**
Expand Down Expand Up @@ -62,6 +77,17 @@ public CompletableFuture<String> rotateKey(String userId) throws IOException {
return this.restTransport.performRequestAsync(new RotateDbUserKeyRequest(userId), RotateDbUserKeyRequest._ENDPOINT);
}

/**
* Update user expiration time.
*
* @param userId User ID.
* @param expiration New expiration. Set {@code null} to disable expiration.
*/
public CompletableFuture<Void> updateUserExpiration(String userId, OffsetDateTime expiration) throws IOException {
return this.restTransport.performRequestAsync(new UpdateDbUserExpirationRequest(userId, expiration),
UpdateDbUserExpirationRequest._ENDPOINT);
}

/**
* Fetch "db" user info.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.weaviate.client6.v1.api.rbac.users;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

import io.weaviate.client6.v1.internal.json.JSON;
import io.weaviate.client6.v1.internal.rest.Endpoint;
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;
import io.weaviate.client6.v1.internal.rest.UrlEncoder;

public record UpdateDbUserExpirationRequest(String userId, OffsetDateTime expiration) {

public static final Endpoint<UpdateDbUserExpirationRequest, Void> _ENDPOINT = SimpleEndpoint.sideEffect(
__ -> "PUT",
request -> "/users/db/" + UrlEncoder.encodeValue(request.userId) + "/expiration",
request -> Collections.emptyMap(),
request -> {
Map<String, Object> body = null;
if (request.expiration == null) {
body = Map.of("expiresAt", request.expiration());
} else {
body = Map.of("neverExpires", true);
}
return JSON.serialize(body);
});
}
22 changes: 21 additions & 1 deletion src/main/java/io/weaviate/client6/v1/internal/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
Expand All @@ -15,11 +18,28 @@ public final class DateUtil {
private DateUtil() {
}

private static final DateTimeFormatter RFC3339 = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
.appendOffsetId()
.toFormatter();

/** Convert ISO8601-formatted time string to {@link OffsetDateTime}. */
public static OffsetDateTime fromISO8601(String iso8601) {
return OffsetDateTime.parse(iso8601);
}

/** Format {@link OffsetDateTime} as RFC3339 timestamp. */
public static String toRFC3339(OffsetDateTime odt) {
return odt.format(RFC3339);
}

public static enum CustomTypeAdapterFactory implements TypeAdapterFactory {
INSTANCE;

Expand All @@ -34,7 +54,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

@Override
public void write(JsonWriter out, OffsetDateTime value) throws IOException {
out.value(value.toString());
out.value(toRFC3339(value));
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.weaviate.client6.v1.internal.json;

import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -2274,6 +2275,31 @@ public static Object[][] testCases() {
io.weaviate.client6.v1.api.rbac.groups.UserType.DB_ENV_USER,
"\"db_env_user\""
},
{
OffsetDateTime.class,
OffsetDateTime.parse("2024-03-01T00:00:00Z"),
"\"2024-03-01T00:00:00Z\"",
},
{
OffsetDateTime.class,
OffsetDateTime.parse("2024-03-01T00:00:01Z"),
"\"2024-03-01T00:00:01Z\"",
},
{
OffsetDateTime.class,
OffsetDateTime.parse("2024-03-01T12:34:56.789Z"),
"\"2024-03-01T12:34:56.789Z\"",
},
{
OffsetDateTime.class,
OffsetDateTime.parse("2024-03-01T00:00:00.000000001Z"),
"\"2024-03-01T00:00:00.000000001Z\"",
},
{
OffsetDateTime.class,
OffsetDateTime.parse("2024-03-01T00:00:00+02:00"),
"\"2024-03-01T00:00:00+02:00\"",
},
};
}

Expand Down
Loading