Skip to content

Commit

Permalink
🔨 Refact: Add a completed callback in the Callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings committed Jan 8, 2023
1 parent bf78aec commit 07fa595
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 42 deletions.
3 changes: 3 additions & 0 deletions fluocean-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ dependencies {
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.10'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.1")

// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.hanbings.fluocean.common;

import io.hanbings.fluocean.common.interfaces.Callback;
import io.hanbings.fluocean.common.interfaces.Response;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;
Expand All @@ -9,20 +11,22 @@ public class OAuthCallback {
private OAuthCallback() {
}

public static <D, W> Callback<D, W> response(String token, @Nullable D data, @Nullable W wrong) {
public static <D, W> Callback<D, W> response(
String token, @Nullable D data, @Nullable W wrong, @NotNull Response response
) {
return (data == null && wrong == null) ?
new OAuthCallback.Exception<>(token, new IllegalArgumentException()) :
new OAuthCallback.Exception<>(token, new IllegalArgumentException(), null) :
((wrong == null) ?
new OAuthCallback.Success<>(token, data) :
new OAuthCallback.Failure<>(token, wrong)
new OAuthCallback.Success<>(token, data, response) :
new OAuthCallback.Failure<>(token, wrong, response)
);
}

public static <D, W> Callback<D, W> exception(String token, Throwable throwable) {
return new OAuthCallback.Exception<>(token, throwable);
return new OAuthCallback.Exception<>(token, throwable, null);
}

record Success<D, W>(String token, D data) implements Callback<D, W> {
record Success<D, W>(String token, D data, Response response) implements Callback<D, W> {

@Override
public W wrong() {
Expand All @@ -41,6 +45,13 @@ public Callback<D, W> succeed(Consumer<D> data) {
return this;
}

@Override
public Callback<D, W> completed(Consumer<Response> response) {
response.accept(this.response);

return this;
}

@Override
public Callback<D, W> fail(Consumer<W> wrong) {
return this;
Expand All @@ -57,7 +68,7 @@ public boolean success() {
}
}

record Failure<D, W>(String token, W wrong) implements Callback<D, W> {
record Failure<D, W>(String token, W wrong, Response response) implements Callback<D, W> {
@Override
public D data() {
return null;
Expand All @@ -73,6 +84,13 @@ public Callback<D, W> succeed(Consumer<D> data) {
return this;
}

@Override
public Callback<D, W> completed(Consumer<Response> response) {
response.accept(this.response);

return this;
}

@Override
public Callback<D, W> fail(Consumer<W> wrong) {
wrong.accept(this.wrong);
Expand All @@ -91,7 +109,7 @@ public boolean success() {
}
}

record Exception<D, W>(String token, Throwable throwable) implements Callback<D, W> {
record Exception<D, W>(String token, Throwable throwable, Response response) implements Callback<D, W> {
@Override
public D data() {
return null;
Expand All @@ -107,6 +125,11 @@ public Callback<D, W> succeed(Consumer<D> data) {
return this;
}

@Override
public Callback<D, W> completed(Consumer<Response> response) {
return this;
}

@Override
public Callback<D, W> fail(Consumer<W> wrong) {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.hanbings.fluocean.common.data;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;

public record Email(
@JsonProperty("email")
@SerializedName("email")
String email
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.hanbings.fluocean.common.data;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;

public record Openid(
@JsonProperty("openid")
@SerializedName("openid")
String openid
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.hanbings.fluocean.common.exception;

@SuppressWarnings("unused")
public class NoResponseReceivedException extends Exception {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ public interface Callback<D, W> {

W wrong();

Response response();

String token();

Throwable throwable();

Callback<D, W> succeed(Consumer<D> data);

Callback<D, W> completed(Consumer<Response> response);

Callback<D, W> fail(Consumer<W> wrong);

Callback<D, W> except(Consumer<Throwable> throwable);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Callback<DiscordAccess, DiscordAccess.Wrong> token(String code, String st
.get()
.object(DiscordAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -120,7 +120,7 @@ public Callback<DiscordAccess, DiscordAccess.Wrong> token(String code, String st
.get()
.object(DiscordAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down Expand Up @@ -154,7 +154,7 @@ public Callback<DiscordAccess, DiscordAccess.Wrong> refresh(String token) {
.get()
.object(DiscordAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -163,7 +163,7 @@ public Callback<DiscordAccess, DiscordAccess.Wrong> refresh(String token) {
.get()
.object(DiscordAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down Expand Up @@ -196,7 +196,7 @@ public Callback<DiscordRevoke, DiscordRevoke.Wrong> revoke(String token) {
.get()
.object(DiscordRevoke.class, response.raw());

return OAuthCallback.response(null, access, null);
return OAuthCallback.response(null, access, null, response);
}

return OAuthCallback.exception(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Callback<DropboxAccess, DropboxAccess.Wrong> token(String code, String st
.get()
.object(DropboxAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -86,7 +86,7 @@ public Callback<DropboxAccess, DropboxAccess.Wrong> token(String code, String st
.get()
.object(DropboxAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down Expand Up @@ -119,7 +119,7 @@ public <K, V> Map<K, V> map(Class<K> key, Class<V> value, String raw) {
.get()
.object(DropboxRevoke.class, "{ }");

return OAuthCallback.response(null, access, null);
return OAuthCallback.response(null, access, null, response);
}

return OAuthCallback.exception(
Expand Down Expand Up @@ -153,7 +153,7 @@ public Callback<DropboxRefresh, DropboxRefresh.Wrong> refresh(String token) {
.get()
.object(DropboxRefresh.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -162,7 +162,7 @@ public Callback<DropboxRefresh, DropboxRefresh.Wrong> refresh(String token) {
.get()
.object(DropboxRefresh.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public Callback<GithubAccess, GithubAccess.Wrong> token(String code, String stat
.get()
.object(GithubAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);
} else {
GithubAccess.Wrong wrong = this.serialization()
.get()
.object(GithubAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Callback<GoogleAccess, GoogleAccess.Wrong> token(String code, String stat
.get()
.object(GoogleAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -75,7 +75,7 @@ public Callback<GoogleAccess, GoogleAccess.Wrong> token(String code, String stat
.get()
.object(GoogleAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Callback<MicrosoftAccess, MicrosoftAccess.Wrong> token(String code, Strin
.get()
.object(MicrosoftAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -88,7 +88,7 @@ public Callback<MicrosoftAccess, MicrosoftAccess.Wrong> token(String code, Strin
.get()
.object(MicrosoftAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down Expand Up @@ -122,7 +122,7 @@ public Callback<MicrosoftAccess, MicrosoftAccess.Wrong> refresh(String token) {
.get()
.object(MicrosoftAccess.class, response.raw());

return OAuthCallback.response(access.accessToken(), access, null);
return OAuthCallback.response(access.accessToken(), access, null, response);

}

Expand All @@ -131,7 +131,7 @@ public Callback<MicrosoftAccess, MicrosoftAccess.Wrong> refresh(String token) {
.get()
.object(MicrosoftAccess.Wrong.class, response.raw());

return OAuthCallback.response(null, null, wrong);
return OAuthCallback.response(null, null, wrong, response);
}

return OAuthCallback.exception(
Expand Down

0 comments on commit 07fa595

Please sign in to comment.