Skip to content

Commit

Permalink
adding token class files
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Jun 14, 2024
1 parent fd9a2ba commit f690533
Showing 1 changed file with 164 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* This code was generated by
* ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
* | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
* | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
*
* Organization Public API
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* NOTE: This class is auto generated by OpenAPI Generator.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package com.twilio.rest.previewiam.organizations;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.twilio.base.Creator;
import com.twilio.http.Request;
import com.twilio.http.TwilioRestClient;
import com.twilio.constant.EnumConstants;
import com.twilio.converter.Promoter;
import com.twilio.exception.ApiConnectionException;
import com.twilio.converter.PrefixedCollapsibleMap;
import com.twilio.converter.Converter;
import com.twilio.exception.ApiException;
import com.twilio.exception.RestException;
import com.twilio.http.HttpMethod;
import com.twilio.http.Response;
import com.twilio.rest.Domains;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.time.LocalDate;
import com.twilio.converter.Converter;
import java.time.ZonedDateTime;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.time.format.DateTimeFormatter;
import com.twilio.converter.DateConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import lombok.ToString;

import java.net.URI;

public class TokenCreator extends Creator<Token>{
private String grantType;
private String clientId;
private String clientSecret;
private String code;
private String redirectUri;
private String audience;
private String refreshToken;
private String scope;

public TokenCreator(final String grantType, final String clientId) {
this.grantType = grantType;
this.clientId = clientId;
}

public TokenCreator setGrantType(final String grantType){
this.grantType = grantType;
return this;
}
public TokenCreator setClientId(final String clientId){
this.clientId = clientId;
return this;
}
public TokenCreator setClientSecret(final String clientSecret){
this.clientSecret = clientSecret;
return this;
}
public TokenCreator setCode(final String code){
this.code = code;
return this;
}
public TokenCreator setRedirectUri(final String redirectUri){
this.redirectUri = redirectUri;
return this;
}
public TokenCreator setAudience(final String audience){
this.audience = audience;
return this;
}
public TokenCreator setRefreshToken(final String refreshToken){
this.refreshToken = refreshToken;
return this;
}
public TokenCreator setScope(final String scope){
this.scope = scope;
return this;
}

@Override
public Token create(final TwilioRestClient client){
String path = "/v1/token";

path = path.replace("{"+"grant_type"+"}", this.grantType.toString());
path = path.replace("{"+"client_id"+"}", this.clientId.toString());

Request request = new Request(
HttpMethod.POST,
Domains.PREVIEWIAM.toString(),
path
);
request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
addPostParams(request);
Response response = client.request(request);
if (response == null) {
throw new ApiConnectionException("Token creation failed: Unable to connect to server");
} else if (!TwilioRestClient.SUCCESS.test(response.getStatusCode())) {
RestException restException = RestException.fromJson(response.getStream(), client.getObjectMapper());
if (restException == null) {
throw new ApiException("Server Error, no content", response.getStatusCode());
}
throw new ApiException(restException);
}

return Token.fromJson(response.getStream(), client.getObjectMapper());
}
private void addPostParams(final Request request) {
if (grantType != null) {
request.addPostParam("grant_type", grantType);

}
if (clientId != null) {
request.addPostParam("client_id", clientId);

}
if (clientSecret != null) {
request.addPostParam("client_secret", clientSecret);

}
if (code != null) {
request.addPostParam("code", code);

}
if (redirectUri != null) {
request.addPostParam("redirect_uri", redirectUri);

}
if (audience != null) {
request.addPostParam("audience", audience);

}
if (refreshToken != null) {
request.addPostParam("refresh_token", refreshToken);

}
if (scope != null) {
request.addPostParam("scope", scope);

}
}
}

0 comments on commit f690533

Please sign in to comment.