001package com.plivo.api.models.token;
002
003import com.plivo.api.exceptions.PlivoRestException;
004import com.plivo.api.models.base.VoiceCreator;
005import retrofit2.Call;
006
007import java.io.IOException;
008
009
010public class TokenCreator extends VoiceCreator<TokenCreateResponse> {
011
012  private final String iss;
013
014  private String sub;
015
016  private Integer nbf;
017
018  private Integer exp;
019
020  private String app;
021
022  private TokenPermission per;
023
024  public TokenCreator(String iss) {
025    this.iss = iss;
026    this.per = new TokenPermission();
027  }
028
029  public String sub() {
030    return this.sub;
031  }
032
033  public TokenCreator sub(final String sub) {
034    this.sub = sub;
035    return this;
036  }
037
038  public Integer nbf() {
039    return this.nbf;
040  }
041
042  public TokenCreator nbf(final Integer nbf) {
043    this.nbf = nbf;
044    return this;
045  }
046
047  public Integer exp() {
048    return this.exp;
049  }
050
051  public TokenCreator exp(final Integer exp) {
052    this.exp = exp;
053    return this;
054  }
055
056  public String app() {
057    return this.app;
058  }
059
060  public TokenCreator app(final String app) {
061    this.app = app;
062    return this;
063  }
064
065  public boolean incoming_allow() {
066    return this.per.voice.incomingAllow;
067  }
068  public TokenCreator incoming_allow(final boolean incoming_allow) {
069    this.per.voice.incomingAllow = incoming_allow;
070    return this;
071  }
072
073  public boolean outgoing_allow() {
074    return this.per.voice.outgoingAllow;
075  }
076
077  public TokenCreator outgoing_allow(final boolean outgoing_allow) {
078    this.per.voice.outgoingAllow = outgoing_allow;
079    return this;
080  }
081
082  public TokenPermission per() {
083    return this.per;
084  }
085
086  public TokenCreateResponse createToken() throws PlivoRestException, IOException {
087    validateParams();
088    return create();
089  }
090
091  private void validateParams() {
092    if (sub == null && incoming_allow() == true) {
093      throw new IllegalArgumentException("sub should be present if incoming is allowed");
094    }
095
096    if (nbf() != null && exp() != null) {
097      if (nbf > exp) {
098        throw new IllegalArgumentException("exp should be greater than nbf");
099      }
100    }
101  }
102
103  @Override
104  protected Call<TokenCreateResponse> obtainCall() {
105    return client().getVoiceApiService().tokenCreate(client().getAuthId(),this);
106  }
107  @Override
108  protected Call<TokenCreateResponse> obtainFallback1Call() {
109    return client().getVoiceFallback1Service().tokenCreate(client().getAuthId(),this);
110  }
111  @Override
112  protected Call<TokenCreateResponse> obtainFallback2Call() {
113    return client().getVoiceFallback2Service().tokenCreate(client().getAuthId(),this);
114  }
115
116
117}