diff --git a/src/main/java/com/github/stachu540/hirezapi/api/Authentication.java b/src/main/java/com/github/stachu540/hirezapi/api/Authentication.java index 54211b7..941f01c 100644 --- a/src/main/java/com/github/stachu540/hirezapi/api/Authentication.java +++ b/src/main/java/com/github/stachu540/hirezapi/api/Authentication.java @@ -8,10 +8,11 @@ import com.github.stachu540.hirezapi.models.TestSession; import com.github.stachu540.hirezapi.models.json.CreateSession; import com.github.stachu540.hirezapi.models.json.Model; -import com.github.stachu540.hirezapi.util.RestClient; +import com.github.stachu540.hirezapi.api.rest.RestClient; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import org.slf4j.Logger; import java.security.MessageDigest; import java.text.SimpleDateFormat; @@ -23,7 +24,8 @@ public class Authentication >{ private final String DEV_ID; private final String AUTH_KEY; - private String SESSION_KEY; + private final Logger logger; + private final HiRezSession sessions = new HiRezSession(); @Getter(AccessLevel.NONE) private final H api; @@ -38,6 +40,7 @@ public Authentication(HiRezAPI main, T base_platform, H api) { this.AUTH_KEY = main.getAuthKey(); this.platform = base_platform; this.api = api; + this.logger = main.getLogger(); } public String getUrl(String endpoint, String... args) { @@ -45,10 +48,10 @@ public String getUrl(String endpoint, String... args) { } public boolean hasSessionKey() { - return SESSION_KEY != null; + return sessions.containsKey(platform) && sessions.get(platform) != null; } - O get(String endpoint, Class classModel, String... args) { + O get(String endpoint, Class classModel, String... args) { O objectData = restClient.request(endpoint, classModel, args); if (objectData instanceof Model) { try { @@ -68,10 +71,11 @@ T get(Class classModel, String... args) { return get(endpoint, classModel, args); } + @SuppressWarnings("unchecked") private void createSession() { CreateSession session = api.createSession(); if (session.getRetMsg().equals("Approved")) { - SESSION_KEY = session.getSessionId(); + sessions.put(platform, session.getSessionId()); } } @@ -90,7 +94,7 @@ private String getEndpoint(String endpoint) { return String.format("%s/%s/%s/%s", base_endpoint, DEV_ID, getSignatue(endpoint), getTimestamp()); default: if (hasSessionKey() && (endpoint.equals("testsession") || testSession())) { - return String.format("%s/%s/%s/%s/%s", base_endpoint, DEV_ID, getSignatue(endpoint), SESSION_KEY, getTimestamp()); + return String.format("%s/%s/%s/%s/%s", base_endpoint, DEV_ID, getSignatue(endpoint), sessions.get(platform), getTimestamp()); } else { createSession(); return getEndpoint(endpoint); diff --git a/src/main/java/com/github/stachu540/hirezapi/api/HiRezSession.java b/src/main/java/com/github/stachu540/hirezapi/api/HiRezSession.java new file mode 100644 index 0000000..57804de --- /dev/null +++ b/src/main/java/com/github/stachu540/hirezapi/api/HiRezSession.java @@ -0,0 +1,30 @@ +package com.github.stachu540.hirezapi.api; + +import com.github.stachu540.hirezapi.enums.url.BasePlatform; +import com.github.stachu540.hirezapi.enums.url.EPaladins; +import com.github.stachu540.hirezapi.enums.url.ESmite; + +import java.util.*; + +public class HiRezSession extends HashMap { + HiRezSession() { + super(); + Map envs = System.getenv(); + List platforms = new ArrayList<>(); + platforms.addAll(Arrays.asList(ESmite.values())); + platforms.addAll(Arrays.asList(EPaladins.values())); + platforms.forEach(platform -> { + if (envs.containsKey(platform.getName())) + put(platform, envs.get(platform.getName())); + }); + } + + @Override + public String put(BasePlatform platform, String sessionId) { + if (!System.getenv().containsKey(platform.getName()) || !System.getenv().get(platform.getName()).equals(sessionId)) + System.getenv().put(platform.getName(), sessionId); + + remove(platform); + return super.put(platform, sessionId); + } +}