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
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.mymetaverse.sdk</groupId>
<artifactId>java-sdk</artifactId>
<version>2.4.0</version>
<version>2.5.2</version>
<build>
<defaultGoal>package install</defaultGoal>
<finalName>${project.name}-${project.version}</finalName>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<groupId>io.mymetaverse.sdk</groupId>
<artifactId>java-sdk</artifactId>
<version>2.4.0</version>
<version>2.5.2</version>


<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/io/mymetavese/metaapi/api/RestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import io.mymetavese.metaapi.MetaAPIImpl;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

public interface RestAction<T> {
Expand All @@ -23,7 +25,6 @@ public interface RestAction<T> {
/**
* Submits a request for execution in asynchronous logic.
*
*
* @param success The success callback from the request.
* @param failure The callback error from the request.
*/
Expand All @@ -32,7 +33,6 @@ public interface RestAction<T> {
/**
* Submits a request for execution in asynchronous logic.
*
*
* @param success The success callback from the request.
*/
default void queue(Consumer<? super T> success) {
Expand All @@ -46,6 +46,13 @@ default void queue(Consumer<? super T> success) {
*/
T complete();

/**
* Submits a Request in synchronous logic with a Timeout
*
* @return The response of the Request.
*/
T complete(long timeout, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException;

/**
* Submits a request for execution after some time delay.
* @param time The time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import io.mymetavese.metaapi.api.RestAction;
import io.mymetavese.metaapi.api.entities.LinkingLink;

public interface GetLinkingLink extends RestAction<LinkingLink> {
public interface GetLinkingLinkAction extends RestAction<LinkingLink> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface GameEntity extends ApiEntity {
*
* @return An action that represents the linking link.
*/
GetLinkingLink getLinkingLink();
GetLinkingLinkAction getLinkingLink();

/**
* Get the currently active metacitizen for this game entity.
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/io/mymetavese/metaapi/requests/RequestGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import okhttp3.internal.http.HttpMethod;

import java.io.IOException;
import java.util.Objects;

public class RequestGenerator {

Expand All @@ -29,6 +28,7 @@ public void asyncRequest(Request<?> request) {
}

public void request(Request<?> request) {

String route = request.getCompiledRoute();

okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
Expand All @@ -52,28 +52,29 @@ else if(bodyObject != null)
if (request.getHeaders() != null && !request.getHeaders().isEmpty())
request.getHeaders().forEach(builder::addHeader);

// Maybe should add attempts here.

try(Response response = httpClient.newCall(builder.build()).execute()) {
try (Response response = httpClient.newCall(builder.build()).execute()) {

if (response.code() >= 500) {
throw new IOException("Internal server error: " + Objects.requireNonNull(response.body()).string());
String responseBodyString = response.peekBody(Long.MAX_VALUE).string();
request.handleResponse(response);
throw new IOException("Internal server error: " + responseBodyString);
}

if(response.code() == 401 && request.getAttempts() < 2) {
// Try to Re-authenticate if the token is invalid and the re-authentication has not been attempted for more
// than 2 times.
if (response.code() == 401 && request.getAttempts() < 2) {

request.addAttempt();
api.getTokenHandler().reauthenticate();
api.getTokenHandler().authenticate();
this.request(request);
return;

} else {
request.handleResponse(response);
}

request.handleResponse(response);
} catch (IOException ex) {
ex.printStackTrace();

if(request.getAttempts() < 2) {
request.addAttempt();
this.request(request);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

public abstract class RestActionImpl<T> extends Transformable<T> implements RestAction<T> {
Expand Down Expand Up @@ -104,5 +107,11 @@ public void queue(Consumer<? super T> success, Consumer<? super RequestError> fa
public T complete() {
return submit().join();
}

@Override
public T complete(long timeout, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException {
return submit().get(timeout, timeUnit);
}

}

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.mymetavese.metaapi.requests.actions;

import io.mymetavese.metaapi.api.MetaAPI;
import io.mymetavese.metaapi.api.actions.GetLinkingLink;
import io.mymetavese.metaapi.api.actions.GetLinkingLinkAction;
import io.mymetavese.metaapi.api.entities.v2.GameEntity;
import io.mymetavese.metaapi.api.entities.LinkingLink;
import io.mymetavese.metaapi.requests.RestActionImpl;
import io.mymetavese.metaapi.requests.entities.LinkingLinkImpl;
import io.mymetavese.metaapi.requests.routes.Routes;

public class GetLinkingLinkActionImpl extends RestActionImpl<LinkingLink> implements GetLinkingLink {
public class GetLinkingLinkActionImpl extends RestActionImpl<LinkingLink> implements GetLinkingLinkAction {

private final GameEntity gameEntity;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.mymetavese.metaapi.requests.actions.drops;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import io.mymetavese.metaapi.api.MetaAPI;
import io.mymetavese.metaapi.api.actions.drops.ConsumeDropAction;
Expand All @@ -11,12 +10,8 @@
import io.mymetavese.metaapi.requests.RestActionImpl;
import io.mymetavese.metaapi.requests.entities.drops.responses.DropConsumedResponseImpl;
import io.mymetavese.metaapi.requests.routes.Routes;
import okhttp3.Response;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Objects;

public class ConsumeDropActionImpl extends RestActionImpl<DropConsumedResponse> implements ConsumeDropAction {

Expand All @@ -31,24 +26,6 @@ public ConsumeDropActionImpl(MetaAPI api, String dropId, GameEntity dropReceiver
this.dropEntryRequirements = dropEntryRequirements;
}

@Override
public DropConsumedResponse transform(Response response) {

if (response == null || response.body() == null) {
throw new NullPointerException("Response cannot be null");
}

Gson gson = new Gson();
try (Reader reader = Objects.requireNonNull(response.body()).charStream()) {
return gson.fromJson(reader, DropConsumedResponseImpl.class);
} catch (IOException ex) {
ex.printStackTrace();
}

return null;

}

@Override
protected String compileRoute() {
return route.compileRoute(dropId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CreateLinkingLinkAction createLinkingLink() {
}

@Override
public GetLinkingLink getLinkingLink() {
public GetLinkingLinkAction getLinkingLink() {
return new GetLinkingLinkActionImpl(metaAPI, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public static StaticToken create(String token) {
}

@Override
public void reauthenticate() { }
public void authenticate() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public interface TokenHandler {

String getToken();

void reauthenticate();
void authenticate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.mymetavese.metaapi.requests.token.oauth;

import io.mymetavese.metaapi.requests.token.TokenHandler;
import io.mymetavese.metaapi.requests.token.oauth.scopes.OAuthScope;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public interface OAuthToken extends TokenHandler {

List<String> getOAuthScopes();

String getBaseAuthUrl();

final class Builder {

private String clientID;
private String clientSecret;
private String authenticationAddress = "cloud.mymetaverse.io";
private final List<String> scopes = new ArrayList<>();

private Builder() { }

public static OAuthToken.Builder createBuilder() {
return new OAuthToken.Builder();
}

public OAuthToken.Builder withClientID(String clientID) {
this.clientID = clientID;
return this;
}

public OAuthToken.Builder withClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
}

public OAuthToken.Builder withAuthenticationAddress(String authenticationAddress) {
this.authenticationAddress = authenticationAddress;
return this;
}

public OAuthToken.Builder useScopes(OAuthScope... scopes) {
Arrays.stream(scopes).forEach(scope -> this.scopes.add(scope.getScope()));
return this;
}

public OAuthToken.Builder useScopes(String... scopes) {
this.scopes.addAll(Arrays.asList(scopes));
return this;
}

public OAuthTokenImpl build() {
return new OAuthTokenImpl(clientID, clientSecret, authenticationAddress, scopes);
}

public OAuthTokenImpl buildAuthenticated() {
OAuthTokenImpl oAuthToken = new OAuthTokenImpl(clientID, clientSecret, authenticationAddress, scopes);
oAuthToken.authenticate();
return oAuthToken;
}

}

}
Loading