-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
mommons-shared/src/main/java/cz/maku/mommons/utils/Cooldown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cz.maku.mommons.utils; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Cooldown { | ||
|
||
private final long cooldown; | ||
private final TimeUnit timeUnit; | ||
private final long executionTime; | ||
|
||
public Cooldown(long cooldown, TimeUnit timeUnit) { | ||
this.cooldown = cooldown; | ||
this.timeUnit = timeUnit; | ||
executionTime = System.currentTimeMillis(); | ||
} | ||
|
||
public boolean isExpired() { | ||
return System.currentTimeMillis() - executionTime >= timeUnit.toMillis(cooldown); | ||
} | ||
|
||
public static Cooldown of(long cooldown, TimeUnit timeUnit) { | ||
return new Cooldown(cooldown, timeUnit); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
mommons-shared/src/main/java/cz/maku/mommons/utils/Rests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cz.maku.mommons.utils; | ||
|
||
import com.google.common.collect.Maps; | ||
import cz.maku.mommons.Mommons; | ||
import okhttp3.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public final class Rests { | ||
|
||
private static <RES> Optional<RES> perform(Request request, Class<RES> clazz) throws IOException { | ||
Call call = Mommons.HTTP_CLIENT.newCall(request); | ||
Response response = call.execute(); | ||
ResponseBody responseBody = response.body(); | ||
if (responseBody == null) return Optional.empty(); | ||
return Optional.of(Mommons.GSON.fromJson(responseBody.string(), clazz)); | ||
} | ||
|
||
public static <RES> Optional<RES> get(String url, Class<RES> clazz, Map<String, String> headers) throws IOException { | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.headers(Headers.of(headers)) | ||
.build(); | ||
|
||
return perform(request, clazz); | ||
} | ||
|
||
public static <REQ, RES> Optional<RES> post(String url, REQ req, Class<RES> clazz, Map<String, String> headers) throws IOException { | ||
RequestBody body = RequestBody.create(Mommons.GSON.toJson(req), MediaType.get("application/json")); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.headers(Headers.of(headers)) | ||
.post(body) | ||
.build(); | ||
|
||
return perform(request, clazz); | ||
} | ||
|
||
public static <T> Optional<T> get(String url, Class<T> clazz) throws IOException { | ||
return get(url, clazz, Maps.newHashMap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters