All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chain.sequence.http.session.ProdRefresher Maven / Gradle / Ivy

The newest version!
package com.chain.sequence.http.session;

import com.chain.sequence.exception.ChainException;
import com.chain.sequence.exception.ConfigurationException;
import com.chain.sequence.http.HttpWrapper;
import com.google.gson.annotations.SerializedName;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class ProdRefresher implements Refresher {
  private class SessionResponse {
    @SerializedName("refresh_at")
    public Integer refreshAt;

    @SerializedName("team_name")
    public String teamName;

    @SerializedName("refresh_token")
    public String dischargeMacaroon;

    public SessionResponse() {
      this.refreshAt = 0;
      this.teamName = null;
      this.dischargeMacaroon = null;
    }
  }

  private HttpWrapper http;
  private SessionResponse resp;

  public ProdRefresher() {
    try {
      List sessionURLs =
          new ArrayList<>(Arrays.asList(new URL("https://session-api.seq.com")));
      this.http = new HttpWrapper(sessionURLs);
      this.resp = new SessionResponse();
    } catch (MalformedURLException e) {
      new ConfigurationException(e.getMessage());
    }
  }

  public String teamName() {
    return this.resp.teamName;
  }

  public String dischargeMacaroon() {
    return this.resp.dischargeMacaroon;
  }

  public boolean needsRefresh() {
    return this.resp.refreshAt < System.currentTimeMillis() / 1000;
  }

  public void refresh(String macaroon) throws ChainException {
    Map sessionReq = new HashMap();
    sessionReq.put("macaroon", macaroon);
    this.resp = this.http.post("/sessions/validate", sessionReq, SessionResponse.class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy