Skip to content

Commit

Permalink
Logger and Session
Browse files Browse the repository at this point in the history
- added Logger #16
- back to the old style session storing. Storing data to environmental variables
  • Loading branch information
Damian Staszewski committed Oct 22, 2017
1 parent 57fb02b commit dc70a57
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,8 @@
public class Authentication <T extends BasePlatform, H extends HiRez<T>>{
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;
Expand All @@ -38,17 +40,18 @@ 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) {
return String.format("%s/%s%s", platform.getUrl(), getEndpoint(endpoint), (args.length > 0) ? "/" + String.join("/", args) : "");
}

public boolean hasSessionKey() {
return SESSION_KEY != null;
return sessions.containsKey(platform) && sessions.get(platform) != null;
}

<O extends Object> O get(String endpoint, Class<O> classModel, String... args) {
<O> O get(String endpoint, Class<O> classModel, String... args) {
O objectData = restClient.request(endpoint, classModel, args);
if (objectData instanceof Model) {
try {
Expand All @@ -68,10 +71,11 @@ <T extends Model> T get(Class<T> 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());
}
}

Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/stachu540/hirezapi/api/HiRezSession.java
Original file line number Diff line number Diff line change
@@ -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<BasePlatform, String> {
HiRezSession() {
super();
Map<String, String> envs = System.getenv();
List<BasePlatform> 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);
}
}

0 comments on commit dc70a57

Please sign in to comment.