From ce97b7829e1cb2695f47466f50d005fdb6be8acf Mon Sep 17 00:00:00 2001 From: sbansla Date: Thu, 22 Aug 2024 15:27:35 +0530 Subject: [PATCH 1/6] chore: ignore content_v1 spec for release --- scripts/build_twilio_library.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/build_twilio_library.py b/scripts/build_twilio_library.py index 443f2dcb9..c511db0e9 100644 --- a/scripts/build_twilio_library.py +++ b/scripts/build_twilio_library.py @@ -20,6 +20,10 @@ generateForLanguages = { 'twilio_iam_organizations.json' : ['java'] } + +ignoreFiles = { + 'twilio_content_v1.json' +} CLEANUP_IMPORT_LANGUAGES = ['java', 'php'] REMOVE_DUPLICATE_IMPORT_LANGUAGES = ['node'] CONFIG_FOLDER = 'tmp' @@ -32,7 +36,7 @@ def build(openapi_spec_path: str, output_path: str, language: str) -> None: else: spec_folder = openapi_spec_path spec_files = sorted(os.listdir(spec_folder)) - + spec_files.remove('twilio_content_v1.json') generate(spec_folder, spec_files, output_path, language) From 94680df8ed5ba615646ca60e0ac1cae4f117a0aa Mon Sep 17 00:00:00 2001 From: sbansla Date: Thu, 22 Aug 2024 15:43:57 +0530 Subject: [PATCH 2/6] removed unwanted code --- scripts/build_twilio_library.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/build_twilio_library.py b/scripts/build_twilio_library.py index c511db0e9..c7d983c4d 100644 --- a/scripts/build_twilio_library.py +++ b/scripts/build_twilio_library.py @@ -20,10 +20,6 @@ generateForLanguages = { 'twilio_iam_organizations.json' : ['java'] } - -ignoreFiles = { - 'twilio_content_v1.json' -} CLEANUP_IMPORT_LANGUAGES = ['java', 'php'] REMOVE_DUPLICATE_IMPORT_LANGUAGES = ['node'] CONFIG_FOLDER = 'tmp' From 4d54ca10a7ec579f427cc8493674db50afd52f97 Mon Sep 17 00:00:00 2001 From: sbansla Date: Thu, 22 Aug 2024 19:06:53 +0530 Subject: [PATCH 3/6] fix: made twilio_iam_organizations spec file as versionless --- src/main/java/com/twilio/oai/TwilioCodegenAdapter.java | 10 +++++----- .../com/twilio/oai/common/ApplicationConstants.java | 3 +++ src/test/java/com/twilio/oai/TwilioGeneratorTest.java | 9 ++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/twilio/oai/TwilioCodegenAdapter.java b/src/main/java/com/twilio/oai/TwilioCodegenAdapter.java index 72e2db227..c61226574 100644 --- a/src/main/java/com/twilio/oai/TwilioCodegenAdapter.java +++ b/src/main/java/com/twilio/oai/TwilioCodegenAdapter.java @@ -2,10 +2,6 @@ import java.io.File; import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Paths; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -13,6 +9,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.twilio.oai.common.ApplicationConstants; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.PathItem; import io.swagger.v3.oas.models.servers.Server; @@ -146,6 +143,9 @@ private String getInputSpecDomain() { } private String getInputSpecVersion() { - return codegen.getInputSpec().replaceAll(INPUT_SPEC_PATTERN, "${version}"); + String version = codegen.getInputSpec().replaceAll(INPUT_SPEC_PATTERN, "${version}"); + boolean textExists = Arrays.stream(ApplicationConstants.VERSION_LESS_SPECS) + .anyMatch(ignoredVersion -> ignoredVersion.equals(version)); + return textExists ? "" : version; } } diff --git a/src/main/java/com/twilio/oai/common/ApplicationConstants.java b/src/main/java/com/twilio/oai/common/ApplicationConstants.java index 953d2e03c..0694d76a9 100644 --- a/src/main/java/com/twilio/oai/common/ApplicationConstants.java +++ b/src/main/java/com/twilio/oai/common/ApplicationConstants.java @@ -55,4 +55,7 @@ public class ApplicationConstants { public static final String PHONE_NUMBER = "phone-number"; public static final Predicate SUCCESS = i -> i != null && i >= 200 && i < 400; + + public static final String[] VERSION_LESS_SPECS = {"organizations"}; + } diff --git a/src/test/java/com/twilio/oai/TwilioGeneratorTest.java b/src/test/java/com/twilio/oai/TwilioGeneratorTest.java index a06c24603..12f8f1af5 100644 --- a/src/test/java/com/twilio/oai/TwilioGeneratorTest.java +++ b/src/test/java/com/twilio/oai/TwilioGeneratorTest.java @@ -28,7 +28,14 @@ public class TwilioGeneratorTest { @Parameterized.Parameters public static Collection generators() { - return Arrays.asList(Generator.TWILIO_PYTHON); + return Arrays.asList(Generator.TWILIO_CSHARP, + Generator.TWILIO_GO, + Generator.TWILIO_JAVA, + Generator.TWILIO_NODE, + Generator.TWILIO_PHP, + Generator.TWILIO_PYTHON, + Generator.TWILIO_RUBY, + Generator.TWILIO_TERRAFORM); } private final Generator generator; From 968400aaf323b515fb8f5be07c81a99cae0a8215 Mon Sep 17 00:00:00 2001 From: Shubham Date: Fri, 23 Aug 2024 16:26:13 +0530 Subject: [PATCH 4/6] chore: dummy commit From 16620fb3c54a9a9698a986da3fcd139fd8f7dea0 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari Date: Fri, 23 Aug 2024 16:42:27 +0530 Subject: [PATCH 5/6] chore: adding examples for previewiam --- .../rest/previewiam/organizations/Token.java | 193 ------------------ .../organizations/TokenCreator.java | 165 --------------- 2 files changed, 358 deletions(-) delete mode 100644 examples/java/src/main/java/com/twilio/rest/previewiam/organizations/Token.java delete mode 100644 examples/java/src/main/java/com/twilio/rest/previewiam/organizations/TokenCreator.java diff --git a/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/Token.java b/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/Token.java deleted file mode 100644 index 1102d5c7e..000000000 --- a/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/Token.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * 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.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.twilio.base.noauth.Resource; -import com.twilio.converter.Converter; -import java.util.Currency; -import com.twilio.converter.DateConverter; -import com.twilio.converter.Promoter; -import com.twilio.converter.PrefixedCollapsibleMap; -import com.twilio.converter.CurrencyDeserializer; -import com.twilio.exception.ApiConnectionException; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - -import com.twilio.exception.ApiException; -import com.twilio.exception.RestException; -import com.twilio.http.HttpMethod; -import com.twilio.http.Request; -import com.twilio.http.Response; -import com.twilio.http.TwilioRestClient; -import com.twilio.rest.Domains; - -import lombok.ToString; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.time.ZonedDateTime; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -import java.util.Map; -import java.time.LocalDate; -import java.math.BigDecimal; -import com.twilio.type.PhoneNumberCapabilities; -import com.twilio.type.FeedbackIssue; -import com.twilio.type.IceServer; -import com.twilio.type.InboundCallPrice; -import com.twilio.type.OutboundPrefixPriceWithOrigin; -import com.twilio.type.OutboundPrefixPrice; -import com.twilio.type.OutboundCallPriceWithOrigin; -import com.twilio.type.PhoneNumberPrice; -import com.twilio.type.InboundSmsPrice; -import com.twilio.type.OutboundSmsPrice; -import com.twilio.type.OutboundCallPrice; -import com.twilio.type.RecordingRule; -import com.twilio.type.SubscribeRule; - -@JsonIgnoreProperties(ignoreUnknown = true) -@ToString -public class Token extends Resource { - private static final long serialVersionUID = 258139119277894L; - - - - public static TokenCreator creator(final String grantType, final String clientId){ - return new TokenCreator(grantType, clientId); - } - - /** - * Converts a JSON String into a Token object using the provided ObjectMapper. - * - * @param json Raw JSON String - * @param objectMapper Jackson ObjectMapper - * @return Token object represented by the provided JSON - */ - public static Token fromJson(final String json, final ObjectMapper objectMapper) { - // Convert all checked exceptions to Runtime - try { - return objectMapper.readValue(json, Token.class); - } catch (final JsonMappingException | JsonParseException e) { - throw new ApiException(e.getMessage(), e); - } catch (final IOException e) { - throw new ApiConnectionException(e.getMessage(), e); - } - } - - /** - * Converts a JSON InputStream into a Token object using the provided - * ObjectMapper. - * - * @param json Raw JSON InputStream - * @param objectMapper Jackson ObjectMapper - * @return Token object represented by the provided JSON - */ - public static Token fromJson(final InputStream json, final ObjectMapper objectMapper) { - // Convert all checked exceptions to Runtime - try { - return objectMapper.readValue(json, Token.class); - } catch (final JsonMappingException | JsonParseException e) { - throw new ApiException(e.getMessage(), e); - } catch (final IOException e) { - throw new ApiConnectionException(e.getMessage(), e); - } - } - - private final String accessToken; - private final String refreshToken; - private final String idToken; - private final String tokenType; - private final Long expiresIn; - - @JsonCreator - private Token( - @JsonProperty("access_token") - final String accessToken, - - @JsonProperty("refresh_token") - final String refreshToken, - - @JsonProperty("id_token") - final String idToken, - - @JsonProperty("token_type") - final String tokenType, - - @JsonProperty("expires_in") - final Long expiresIn - ) { - this.accessToken = accessToken; - this.refreshToken = refreshToken; - this.idToken = idToken; - this.tokenType = tokenType; - this.expiresIn = expiresIn; - } - - public final String getAccessToken() { - return this.accessToken; - } - public final String getRefreshToken() { - return this.refreshToken; - } - public final String getIdToken() { - return this.idToken; - } - public final String getTokenType() { - return this.tokenType; - } - public final Long getExpiresIn() { - return this.expiresIn; - } - - @Override - public boolean equals(final Object o) { - if (this==o) { - return true; - } - - if (o == null || getClass() != o.getClass()) { - return false; - } - - Token other = (Token) o; - - return Objects.equals(accessToken, other.accessToken) && Objects.equals(refreshToken, other.refreshToken) && Objects.equals(idToken, other.idToken) && Objects.equals(tokenType, other.tokenType) && Objects.equals(expiresIn, other.expiresIn) ; - } - - @Override - public int hashCode() { - return Objects.hash(accessToken, refreshToken, idToken, tokenType, expiresIn); - } - - -} - diff --git a/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/TokenCreator.java b/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/TokenCreator.java deleted file mode 100644 index 134cd0f9c..000000000 --- a/examples/java/src/main/java/com/twilio/rest/previewiam/organizations/TokenCreator.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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.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; - -import com.twilio.base.noauth.Creator; -import com.twilio.http.noauth.NoAuthRequest; -import com.twilio.http.noauth.NoAuthTwilioRestClient; - -public class TokenCreator extends Creator{ - 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 NoAuthTwilioRestClient client){ - String path = "/v1/token"; - - path = path.replace("{"+"grant_type"+"}", this.grantType.toString()); - path = path.replace("{"+"client_id"+"}", this.clientId.toString()); - - NoAuthRequest request = new NoAuthRequest( - 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 (!NoAuthTwilioRestClient.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 NoAuthRequest 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); - - } - } -} From 549bcfb35dc28c750011336a6919c34f625a8070 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari Date: Fri, 23 Aug 2024 16:46:23 +0530 Subject: [PATCH 6/6] chore: adding examples for previewiam --- .../com/twilio/rest/previewiam/v1/Token.java | 193 ++++++++++++++++++ .../rest/previewiam/v1/TokenCreator.java | 165 +++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 examples/java/src/main/java/com/twilio/rest/previewiam/v1/Token.java create mode 100644 examples/java/src/main/java/com/twilio/rest/previewiam/v1/TokenCreator.java diff --git a/examples/java/src/main/java/com/twilio/rest/previewiam/v1/Token.java b/examples/java/src/main/java/com/twilio/rest/previewiam/v1/Token.java new file mode 100644 index 000000000..17b8ef4d7 --- /dev/null +++ b/examples/java/src/main/java/com/twilio/rest/previewiam/v1/Token.java @@ -0,0 +1,193 @@ +/* + * 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.v1; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.twilio.base.noauth.Resource; +import com.twilio.converter.Converter; +import java.util.Currency; +import com.twilio.converter.DateConverter; +import com.twilio.converter.Promoter; +import com.twilio.converter.PrefixedCollapsibleMap; +import com.twilio.converter.CurrencyDeserializer; +import com.twilio.exception.ApiConnectionException; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import com.twilio.exception.ApiException; +import com.twilio.exception.RestException; +import com.twilio.http.HttpMethod; +import com.twilio.http.Request; +import com.twilio.http.Response; +import com.twilio.http.TwilioRestClient; +import com.twilio.rest.Domains; + +import lombok.ToString; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.time.ZonedDateTime; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.util.Map; +import java.time.LocalDate; +import java.math.BigDecimal; +import com.twilio.type.PhoneNumberCapabilities; +import com.twilio.type.FeedbackIssue; +import com.twilio.type.IceServer; +import com.twilio.type.InboundCallPrice; +import com.twilio.type.OutboundPrefixPriceWithOrigin; +import com.twilio.type.OutboundPrefixPrice; +import com.twilio.type.OutboundCallPriceWithOrigin; +import com.twilio.type.PhoneNumberPrice; +import com.twilio.type.InboundSmsPrice; +import com.twilio.type.OutboundSmsPrice; +import com.twilio.type.OutboundCallPrice; +import com.twilio.type.RecordingRule; +import com.twilio.type.SubscribeRule; + +@JsonIgnoreProperties(ignoreUnknown = true) +@ToString +public class Token extends Resource { + private static final long serialVersionUID = 258139119277894L; + + + + public static TokenCreator creator(final String grantType, final String clientId){ + return new TokenCreator(grantType, clientId); + } + + /** + * Converts a JSON String into a Token object using the provided ObjectMapper. + * + * @param json Raw JSON String + * @param objectMapper Jackson ObjectMapper + * @return Token object represented by the provided JSON + */ + public static Token fromJson(final String json, final ObjectMapper objectMapper) { + // Convert all checked exceptions to Runtime + try { + return objectMapper.readValue(json, Token.class); + } catch (final JsonMappingException | JsonParseException e) { + throw new ApiException(e.getMessage(), e); + } catch (final IOException e) { + throw new ApiConnectionException(e.getMessage(), e); + } + } + + /** + * Converts a JSON InputStream into a Token object using the provided + * ObjectMapper. + * + * @param json Raw JSON InputStream + * @param objectMapper Jackson ObjectMapper + * @return Token object represented by the provided JSON + */ + public static Token fromJson(final InputStream json, final ObjectMapper objectMapper) { + // Convert all checked exceptions to Runtime + try { + return objectMapper.readValue(json, Token.class); + } catch (final JsonMappingException | JsonParseException e) { + throw new ApiException(e.getMessage(), e); + } catch (final IOException e) { + throw new ApiConnectionException(e.getMessage(), e); + } + } + + private final String accessToken; + private final String refreshToken; + private final String idToken; + private final String tokenType; + private final Long expiresIn; + + @JsonCreator + private Token( + @JsonProperty("access_token") + final String accessToken, + + @JsonProperty("refresh_token") + final String refreshToken, + + @JsonProperty("id_token") + final String idToken, + + @JsonProperty("token_type") + final String tokenType, + + @JsonProperty("expires_in") + final Long expiresIn + ) { + this.accessToken = accessToken; + this.refreshToken = refreshToken; + this.idToken = idToken; + this.tokenType = tokenType; + this.expiresIn = expiresIn; + } + + public final String getAccessToken() { + return this.accessToken; + } + public final String getRefreshToken() { + return this.refreshToken; + } + public final String getIdToken() { + return this.idToken; + } + public final String getTokenType() { + return this.tokenType; + } + public final Long getExpiresIn() { + return this.expiresIn; + } + + @Override + public boolean equals(final Object o) { + if (this==o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + Token other = (Token) o; + + return Objects.equals(accessToken, other.accessToken) && Objects.equals(refreshToken, other.refreshToken) && Objects.equals(idToken, other.idToken) && Objects.equals(tokenType, other.tokenType) && Objects.equals(expiresIn, other.expiresIn) ; + } + + @Override + public int hashCode() { + return Objects.hash(accessToken, refreshToken, idToken, tokenType, expiresIn); + } + + +} + diff --git a/examples/java/src/main/java/com/twilio/rest/previewiam/v1/TokenCreator.java b/examples/java/src/main/java/com/twilio/rest/previewiam/v1/TokenCreator.java new file mode 100644 index 000000000..39be29372 --- /dev/null +++ b/examples/java/src/main/java/com/twilio/rest/previewiam/v1/TokenCreator.java @@ -0,0 +1,165 @@ +/* + * 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.v1; + +import com.fasterxml.jackson.databind.ObjectMapper; +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; + +import com.twilio.base.noauth.Creator; +import com.twilio.http.noauth.NoAuthRequest; +import com.twilio.http.noauth.NoAuthTwilioRestClient; + +public class TokenCreator extends Creator{ + 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 NoAuthTwilioRestClient client){ + String path = "/v1/token"; + + path = path.replace("{"+"grant_type"+"}", this.grantType.toString()); + path = path.replace("{"+"client_id"+"}", this.clientId.toString()); + + NoAuthRequest request = new NoAuthRequest( + 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 (!NoAuthTwilioRestClient.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 NoAuthRequest 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); + + } + } +}