Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to add version header to java sdk on generation #100

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ target/
node_modules

#macOS
.DS_Store
.DS_Store

#local env
.env
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"scripts": {
"bundle": "redocly bundle openapi.json -o dist/bundledSchema.json",
"fix-java-file-get-requests": "ts-node ./scripts/java/fixFileRelatedGetRequests.ts",
"generate-java": "rm -rf ./dist/java-sdk && npm run bundle && npx mkdirp dist/java-sdk/src/test/java/unit/java/sdk && cp -a ./unit/e2e_tests/java/. ./dist/java-sdk/src/test/java/unit/java/sdk && openapi-generator-cli generate -g java -i ./dist/bundledSchema.json -o ./dist/java-sdk -p hideGenerationTimestamp=true -p packageName=unit.java.sdk -p modelPackage=unit.java.sdk.model -p apiPackage=unit.java.sdk.api --library native -p useJakartaEe=true --openapi-normalizer REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true && npm run fix-java-file-get-requests -- --path=./dist/java-sdk/src/main/java/unit/java/sdk/api/UnitApi.java",
"add-version-header-to-java": "ts-node ./scripts/java/addVersionHeaderToApiClient.ts",
"generate-java": "rm -rf ./dist/java-sdk && npm run bundle && npx mkdirp dist/java-sdk/src/test/java/unit/java/sdk && cp -a ./unit/e2e_tests/java/. ./dist/java-sdk/src/test/java/unit/java/sdk && openapi-generator-cli generate -g java -i ./dist/bundledSchema.json -o ./dist/java-sdk -p hideGenerationTimestamp=true -p packageName=unit.java.sdk -p modelPackage=unit.java.sdk.model -p apiPackage=unit.java.sdk.api --library native -p useJakartaEe=true --openapi-normalizer REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true && npm run fix-java-file-get-requests -- --path=./dist/java-sdk/src/main/java/unit/java/sdk/api/UnitApi.java && npm run add-version-header-to-java -- --path=./dist/java-sdk/src/main/java/unit/java/sdk/ApiClient.java",
"generate-ruby": "rm -rf ./dist/ruby-sdk && npm run bundle && cp -r unit ./dist/ruby-sdk && openapi-generator-cli generate -g ruby -i ./dist/bundledSchema.json -o ./dist/ruby-sdk",
"generate-node": "rm -rf ./dist/node-sdk && npm run bundle && openapi-generator-cli generate -g typescript-axios -i ./dist/bundledSchema.json -o ./dist/node-sdk",
"lint": "redocly lint --config .redocly.yaml openapi.json",
Expand All @@ -11,6 +12,7 @@
"dependencies": {
"@openapitools/openapi-generator-cli": "^2.14.0",
"@redocly/cli": "^1.13.0",
"dotenv": "^16.4.5",
"prettier": "^3.2.5",
"ts-node": "^10.9.2"
}
Expand Down
46 changes: 46 additions & 0 deletions scripts/java/addVersionHeaderToApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getPathCmdParameter, loadDotenv, openJavaFile } from "./utils";
import fs from "fs";

function addVersionHeadersToApiClient(data: string): string {
const interceptorRegex = /interceptor = null;/gm;
const interceptorWithVersionHeader = `interceptor = (req) -> {
req.setHeader("X-UNIT-SDK", "unit-openapi-java-sdk@v${process.env.JAVA_SDK_VERSION}");
};`;

const setInterceptorRegex = /this\.interceptor = interceptor;/gm;
const setInterceptorWithVersionHeader = `this.interceptor = (req) -> {
req.setHeader("X-UNIT-SDK", "unit-openapi-java-sdk@v${process.env.JAVA_SDK_VERSION}");
interceptor.accept(req);
};`;

var processedData = data;
processedData = processedData.replaceAll(
interceptorRegex,
interceptorWithVersionHeader
);

processedData = processedData.replaceAll(
setInterceptorRegex,
setInterceptorWithVersionHeader
);

return processedData;
}

function execute() {
loadDotenv();

try {
console.log("Adding version header to ApiClient...");
const path = getPathCmdParameter();
const data = openJavaFile(path);
const processedData = addVersionHeadersToApiClient(data);
console.log("Added version header to ApiClient!");

fs.writeFileSync(path, processedData);
} catch (e) {
console.error(e);
}
}

execute();
25 changes: 7 additions & 18 deletions scripts/java/fixFileRelatedGetRequests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import { getPathCmdParameter, openJavaFile } from "./utils";

function processData(data: string): string {
function fixFileRelatedGetRequests(data: string): string {
const mainFunctionNameRegex = /(File )(get|download)/gm;
const mainFunctionHttpInfoReturnRegex =
/(ApiResponse<)(File)(> localVarResponse)/gm;
Expand Down Expand Up @@ -73,26 +74,14 @@ import java.net.URI;`
}

function execute() {
const pathArg = process.argv.find((val) => val.includes("--path="));
if (pathArg == null) {
console.error(
`Undefined path! Don't forget to specify the path, e.g. "...fixFileRelatedGetRequests.ts ./TargetFile.java"`
);
return;
}

const path = pathArg?.replace("--path=", "");
if (!path.includes(".java")) {
console.error("Path must lead to a java file!");
return;
}

try {
console.log("Fixing file related get requests...");
const data = fs.readFileSync(path, "utf-8");
const processedData = processData(data);
fs.writeFileSync(path, processedData);
const path = getPathCmdParameter();
const data = openJavaFile(path);
const processedData = fixFileRelatedGetRequests(data);
console.log("Fixed file related get requests!");

fs.writeFileSync(path, processedData);
} catch (e) {
console.error(e);
}
Expand Down
31 changes: 31 additions & 0 deletions scripts/java/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from "fs";
import dotenv from "dotenv";

export function loadDotenv() {
dotenv.config();
}

export function openJavaFile(path: string): string {
if (!path.includes(".java")) {
throw new Error("Path must lead to a java file!");
}

return fs.readFileSync(path, "utf-8");
}

export function getPathCmdParameter(): string {
const pathArg = process.argv.find((val) => val.includes("--path="));

if (pathArg == null) {
throw new Error(
`Undefined path! Don't forget to specify the path, e.g. "...fixFileRelatedGetRequests.ts ./TargetFile.java"`
);
}

const path = pathArg?.replace("--path=", "");
if (!path.includes(".java")) {
throw new Error("Path must lead to a java file!");
}

return path;
}
8 changes: 4 additions & 4 deletions unit/e2e_tests/java/AccountTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ WalletAccount CreateAndCloseWalletAccount() throws ApiException {
CloseAccountRequestDataAttributes closeAccountRequestAttributes = new CloseAccountRequestDataAttributes();
closeAccountRequestAttributes.setReason(CloseAccountRequestDataAttributes.ReasonEnum.BY_CUSTOMER);
closeAccountRequestData.setAttributes(closeAccountRequestAttributes);
closeAccountRequestData.setType(CloseAccountRequestData.TypeEnum.DEPOSIT_ACCOUNT_CLOSE);
closeAccountRequestData.setType(CloseAccountRequestData.TypeEnum.WALLET_ACCOUNT_CLOSE);
closeAccountRequest.setData(closeAccountRequestData);

UnitAccountResponse res = unitApi.closeAccount(walletAccount.getId(), closeAccountRequest);
Expand All @@ -436,7 +436,7 @@ public void ReopenWalletAccountApiTest() throws ApiException {
}

WalletAccount CreateAndFreezeWalletAccount() throws ApiException {
DepositAccount walletAccount = CreateDepositAccount(unitApi, CreateIndividualCustomer(unitApi));
WalletAccount walletAccount = CreateWalletAccount();
FreezeAccountRequest freezeAccountRequest = new FreezeAccountRequest();
FreezeAccountRequestData freezeAccountRequestData = new FreezeAccountRequestData();
FreezeAccountRequestDataAttributes freezeAccountRequestDataAttributes = new FreezeAccountRequestDataAttributes();
Expand Down Expand Up @@ -464,8 +464,8 @@ public void UnfreezeWalletAccountApiTest() throws ApiException {

@Test
public void GetWalletAccountLimitsApiTest() throws ApiException {
DepositAccount depositAccount = CreateDepositAccount(unitApi, CreateIndividualCustomer(unitApi));
UnitGetAccountLimitsResponse res = unitApi.getAccountLimits(depositAccount.getId());
WalletAccount walletAccount = CreateWalletAccount();
UnitGetAccountLimitsResponse res = unitApi.getAccountLimits(walletAccount.getId());
assert res.getData().getType().equals(Limits.TypeEnum.WALLET_LIMITS);
}

Expand Down
5 changes: 1 addition & 4 deletions unit/e2e_tests/java/PaymentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static unit.java.sdk.TestHelpers.CreateWirePaymentCounterparty;
import static unit.java.sdk.TestHelpers.GenerateUnitApiClient;

import java.math.BigDecimal;
import java.util.List;

import static unit.java.sdk.CustomerTests.CreateIndividualCustomer;
Expand All @@ -19,7 +18,6 @@
import unit.java.sdk.model.AchPayment;
import unit.java.sdk.model.BookPayment;
import unit.java.sdk.model.CashDepositBarcode;
import unit.java.sdk.model.Coordinates;
import unit.java.sdk.model.Counterparty;
import unit.java.sdk.model.CounterpartyAccountRelationship;
import unit.java.sdk.model.CounterpartyAccountRelationshipData;
Expand Down Expand Up @@ -54,7 +52,6 @@
import unit.java.sdk.model.GenerateBarcodeRequestDataAttributes;
import unit.java.sdk.model.GenerateBarcodeRequestRelationships;
import unit.java.sdk.model.GetCashDepositStoreLocationsListFilterParameter;
import unit.java.sdk.model.GetCheckDepositsListFilterParameter;
import unit.java.sdk.model.IndividualCustomer;
import unit.java.sdk.model.IndividualDebitCard;
import unit.java.sdk.model.ListPageParameters;
Expand Down Expand Up @@ -301,7 +298,7 @@ public void UpdateAchPaymentApiTest() throws ApiException {
@Test
public void CancelAchPaymentTest() throws ApiException {
AchPayment payment = CreateAchPayment();
System.err.println(payment.getAttributes().getStatus());

UnitPaymentResponse res = unitApi.cancelPayment(payment.getId(), new Object());
assert res.getData().getType().equals(Payment.TypeEnum.ACH_PAYMENT);
}
Expand Down
2 changes: 1 addition & 1 deletion unit/e2e_tests/java/ReceivedPaymentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void ReprocessReceivedPaymentApiTest() throws ApiException {
UnitReceivedPaymentResponse res = unitApi.createIncomingAchPaymentSimulation(req);
ReceivedPayment payment = res.getData();
assert payment.getType().equals(ReceivedPayment.TypeEnum.ACH_RECEIVED_PAYMENT);
System.err.println(payment.toString());

UnitReceivedPaymentResponse reprocessRes = unitApi.reprocessReceivedPayment(payment.getId());
assert reprocessRes.getData().getType().equals(ReceivedPayment.TypeEnum.ACH_RECEIVED_PAYMENT);
}
Expand Down
9 changes: 8 additions & 1 deletion unit/e2e_tests/java/TestHelpers.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package unit.java.sdk;

import java.net.http.HttpRequest;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand Down Expand Up @@ -44,7 +46,12 @@ static UnitApi GenerateUnitApiClient() {
ObjectMapper mapper = cl.getObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
cl.setObjectMapper(mapper);
cl.setRequestInterceptor(r -> r.header("Authorization", "Bearer " + access_token));
Consumer<HttpRequest.Builder> defaultInterceptor = cl.getRequestInterceptor();
cl.setRequestInterceptor(r -> {
r.header("Authorization", "Bearer " + access_token);
System.err.println(r.build().headers().toString());
System.err.println("println test");
});
unitApi = new UnitApi(cl);
}

Expand Down
1 change: 0 additions & 1 deletion unit/e2e_tests/java/TokenTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class TokenTests {
// data.setAttributes(attributes);
// data.setType(CreateApiTokenRequestData.TypeEnum.APITOKEN);
// req.setData(data);
// System.err.println(data.toString());

// UnitApiTokenResponse res = unitApi.createApiToken(testUserId, req);
// assert res.getData().getType().equals(ApiToken.TypeEnum.APITOKEN);
Expand Down
Loading