Skip to content

Commit 1ed2dd3

Browse files
authored
Merge pull request #149 from CyberSource/feature/handling-links-field-to-api
Feature/handling links field to api
2 parents 90a71ed + 016f116 commit 1ed2dd3

File tree

3 files changed

+108
-45
lines changed

3 files changed

+108
-45
lines changed

CyberSource/models/pbl_payment_links_all_get200_response.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,4 @@ def __ne__(self, other):
200200
Returns true if both objects are not equal
201201
"""
202202
return not self == other
203+

generator/cybersource_python_sdk_gen.bat

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,32 @@ rd /s /q ..\CyberSource\apis
1010
rd /s /q ..\CyberSource\models
1111
rd /s /q ..\test
1212

13+
setlocal enabledelayedexpansion
14+
python replaceFieldNamesForPaths.py -i cybersource-rest-spec.json -o cybersource-rest-spec-python.json > replaceFieldLogs.log
15+
del replaceFieldLogs.log
16+
endlocal
17+
1318
REM Command to generate SDK
1419

1520
java -jar swagger-codegen-cli-2.4.38.jar generate -t cybersource-python-template -i cybersource-rest-spec-python.json -l python -o ../ -c cybersource-python-config.json
1621

17-
REM Fixing file names
18-
powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .models.error__links import ErrorLinks', 'from .models.error_links import ErrorLinks'} | Set-Content ..\CyberSource\__init__.py"
19-
20-
powershell -Command "(Get-Content ..\CyberSource\models\__init__.py) | ForEach-Object { $_ -replace 'from .error__links import ErrorLinks', 'from .error_links import ErrorLinks'} | Set-Content ..\CyberSource\models\__init__.py"
21-
22-
powershell -Command "(Get-Content ..\CyberSource\api\secure_file_share_api.py) | ForEach-Object { $_ -replace 'select_header_content_type\(\[''\*_\/_\*;charset=utf-8', 'select_header_content_type([''*/*;charset=utf-8'} | Set-Content ..\CyberSource\api\secure_file_share_api.py"
23-
24-
powershell -Command "(Get-Content ..\docs\SecureFileShareApi.md) | ForEach-Object { $_ -replace '\*\*Content-Type\*\*: \*_\/_\*;charset=utf-8', '**Content-Type**: */*;charset=utf-8'} | Set-Content ..\docs\SecureFileShareApi.md"
25-
26-
REM powershell -Command "(Get-Content ..\CyberSource\api\__init__.py) | ForEach-Object { $_ -replace 'from .download_dtd_api import DownloadDTDApi', ''} | ForEach-Object { $_ -replace 'from .download_xsd_api import DownloadXSDApi', ''} | Set-Content ..\CyberSource\api\__init__.py"
27-
28-
REM powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .apis.download_dtd_api import DownloadDTDApi', ''} | ForEach-Object { $_ -replace 'from .apis.download_xsd_api import DownloadXSDApi', ''} | Set-Content ..\CyberSource\__init__.py"
29-
3022
REM To Change the Long file names
3123
REM Change filenames in models\__init__.py
3224
powershell -Command "(Get-Content ..\CyberSource\models\__init__.py) | ForEach-Object { $_ -replace 'from .ptsv2payments_processing_information_authorization_options_initiator_merchant_initiated_transaction import Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'from .ptsv2payments_merchant_initiated_transaction import Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction'} | Set-Content ..\CyberSource\models\__init__.py"
33-
34-
powershell -Command "(Get-Content ..\CyberSource\models\__init__.py) | ForEach-Object { $_ -replace 'from .tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator_merchant_initiated_transaction import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'from .tmsv2customers__embedded_merchant_initiated_transaction import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction'} | Set-Content ..\CyberSource\models\__init__.py"
35-
36-
powershell -Command "(Get-Content ..\CyberSource\models\__init__.py) | ForEach-Object { $_ -replace 'from .tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator', 'from .tmsv2customers__embedded_authorization_options_initiator import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator'} | Set-Content ..\CyberSource\models\__init__.py"
37-
38-
REM powershell -Command "(Get-Content ..\CyberSource\models\__init__.py) | ForEach-Object { $_ -replace 'from .risk_v1_authentication_exemptions_post201_response_consumer_authentication_information_strong_authentication import RiskV1AuthenticationExemptionsPost201ResponseConsumerAuthenticationInformationStrongAuthentication', 'from .risk_v1_authentication_exemptions_post201_response_strong_authentication import RiskV1AuthenticationExemptionsPost201ResponseConsumerAuthenticationInformationStrongAuthentication'} | Set-Content ..\CyberSource\models\__init__.py"
39-
4025
REM Change filenames in __init__.py
4126
powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .models.ptsv2payments_processing_information_authorization_options_initiator_merchant_initiated_transaction import Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'from .models.ptsv2payments_merchant_initiated_transaction import Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction'} | Set-Content ..\CyberSource\__init__.py"
42-
43-
powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .models.tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator_merchant_initiated_transaction import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction', 'from .models.tmsv2customers__embedded_merchant_initiated_transaction import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction'} | Set-Content ..\CyberSource\__init__.py"
44-
45-
powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .models.tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator', 'from .models.tmsv2customers__embedded_authorization_options_initiator import Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator'} | Set-Content ..\CyberSource\__init__.py"
46-
47-
REM powershell -Command "(Get-Content ..\CyberSource\__init__.py) | ForEach-Object { $_ -replace 'from .models.risk_v1_authentication_exemptions_post201_response_consumer_authentication_information_strong_authentication import RiskV1AuthenticationExemptionsPost201ResponseConsumerAuthenticationInformationStrongAuthentication', 'from .models.risk_v1_authentication_exemptions_post201_response_strong_authentication import RiskV1AuthenticationExemptionsPost201ResponseConsumerAuthenticationInformationStrongAuthentication'} | Set-Content ..\CyberSource\__init__.py"
48-
4927
REM Change filenames in models
5028
powershell -Command " rename-item -Path ..\CyberSource\models\ptsv2payments_processing_information_authorization_options_initiator_merchant_initiated_transaction.py -newname ptsv2payments_merchant_initiated_transaction.py"
51-
52-
powershell -Command " rename-item -Path ..\CyberSource\models\tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator_merchant_initiated_transaction.py -newname tmsv2customers__embedded_merchant_initiated_transaction.py"
53-
54-
powershell -Command " rename-item -Path ..\CyberSource\models\tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator.py -newname tmsv2customers__embedded_authorization_options_initiator.py"
55-
56-
REM powershell -Command " rename-item -Path ..\CyberSource\models\risk_v1_authentication_exemptions_post201_response_consumer_authentication_information_strong_authentication.py -newname risk_v1_authentication_exemptions_post201_response_strong_authentication.py"
57-
5829
REM Change filenames in test
5930
powershell -Command " rename-item -Path ..\test\test_ptsv2payments_processing_information_authorization_options_initiator_merchant_initiated_transaction.py -newname test_ptsv2payments_merchant_initiated_transaction.py"
60-
61-
powershell -Command " rename-item -Path ..\test\test_tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator_merchant_initiated_transaction.py -newname test_tmsv2customers__embedded_merchant_initiated_transaction.py"
62-
63-
powershell -Command " rename-item -Path ..\test\test_tmsv2customers__embedded_default_payment_instrument__embedded_instrument_identifier_processing_information_authorization_options_initiator.py -newname test_tmsv2customers__embedded_authorization_options_initiator.py"
64-
65-
REM powershell -Command " rename-item -Path ..\test\test_risk_v1_authentication_exemptions_post201_response_consumer_authentication_information_strong_authentication.py -newname test_risk_v1_authentication_exemptions_post201_response_strong_authentication.py"
66-
6731
REM Change filenames in docs
6832
powershell -Command " rename-item -Path ..\docs\Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.md -newname Ptsv2paymentsMerchantInitiatedTransaction.md"
6933

70-
powershell -Command " rename-item -Path ..\docs\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiatorMerchantInitiatedTransaction.md -newname Tmsv2customersEmbeddedMerchantInitiatedTransaction.md"
71-
72-
powershell -Command " rename-item -Path ..\docs\Tmsv2customersEmbeddedDefaultPaymentInstrumentEmbeddedInstrumentIdentifierProcessingInformationAuthorizationOptionsInitiator.md -newname Tmsv2customersEmbeddedAuthorizationOptionsInitiator.md"
7334

74-
REM powershell -Command " rename-item -Path ..\docs\RiskV1AuthenticationExemptionsPost201ResponseConsumerAuthenticationInformationStrongAuthentication.md -newname RiskV1AuthenticationExemptionsPost201ResponseStrongAuthentication.md"
35+
@REM replace sdkLinks fieldName to links for supporting links field name in request/response body
36+
echo "starting of replacing the links keyword in pbl_payment_links_all_get200_response.py model"
37+
powershell -Command "Set-Content ..\CyberSource\models\pbl_payment_links_all_get200_response.py ((Get-Content ..\CyberSource\models\pbl_payment_links_all_get200_response.py -Raw) -replace '''sdk_links'': ''sdkLinks''', '''sdk_links'': ''links''')"
38+
echo "completed the task of replacing the links keyword in pbl_payment_links_all_get200_response.py model"
7539

7640
git checkout ..\README.md
7741

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import json
2+
import os
3+
import argparse
4+
5+
6+
def traverse_and_replace(obj, path, new_field_name):
7+
if len(path) == 1:
8+
if path[0] in obj:
9+
keys = list(obj.keys())
10+
index = keys.index(path[0])
11+
obj[new_field_name] = obj.pop(path[0])
12+
keys = list(obj.keys()) # Update keys after replacing the key
13+
reordered = {k: obj[k] for k in keys[:index] + [new_field_name] + keys[index:]}
14+
obj.clear()
15+
obj.update(reordered)
16+
else:
17+
if path[0] in obj and isinstance(obj[path[0]], dict):
18+
traverse_and_replace(obj[path[0]], path[1:], new_field_name)
19+
elif path[0] in obj and isinstance(obj[path[0]], list):
20+
if len(path) > 1 and isinstance(path[1], int) and path[1] < len(obj[path[0]]):
21+
traverse_and_replace(obj[path[0]][path[1]], path[2:], new_field_name)
22+
23+
def replace_field_names(input_file_path, output_file_path, changes):
24+
"""
25+
Replace field names in the JSON file based on the given changes.
26+
27+
:param input_file: Path to the input JSON file.
28+
:param output_file: Path to the output JSON file.
29+
:param changes: List of dictionaries containing paths and new field names.
30+
"""
31+
32+
with open(input_file_path, 'r', encoding='utf-8') as file:
33+
data = json.load(file)
34+
35+
for change in changes:
36+
key_path = change['path']
37+
new_field_name = change['new_name']
38+
traverse_and_replace(data, key_path, new_field_name)
39+
40+
with open(output_file_path, 'w', encoding='utf-8') as file:
41+
json.dump(data, file, indent=4)
42+
43+
def convert_input_paths_to_changes(input_paths):
44+
"""
45+
Convert input paths to a list of changes with parsed paths and new field names.
46+
47+
:param input_paths: List of dictionaries containing string paths and new field names.
48+
:return: List of dictionaries with parsed paths and new field names.
49+
"""
50+
changes = []
51+
for item in input_paths:
52+
path_str = item["path"]
53+
path_parts = []
54+
for part in path_str.split('.'):
55+
if '[' in part and ']' in part:
56+
field, index = part.split('[')
57+
path_parts.append(field)
58+
path_parts.append(int(index.strip(']')))
59+
else:
60+
path_parts.append(part)
61+
changes.append({
62+
"path": path_parts,
63+
"new_name": item["new_field_name"]
64+
})
65+
return changes
66+
67+
68+
if __name__ == "__main__":
69+
parser = argparse.ArgumentParser(description="Replace field names in a JSON file.")
70+
parser.add_argument("-i", "--input", help="Path to the input JSON file.")
71+
parser.add_argument("-o", "--output", help="Path to the output JSON file.")
72+
args = parser.parse_args()
73+
74+
input_file = args.input if args.input else "cybersource-rest-spec.json"
75+
76+
output_file = args.output if args.output else "cybersource-rest-spec-output.json"
77+
78+
79+
inputPaths =[
80+
{
81+
"path": "paths./ipl/v2/payment-links.get.responses.200.schema.properties.links",
82+
"new_field_name": "sdkLinks"
83+
}
84+
85+
# example of how to add more paths
86+
# ,{
87+
# "path": "paths./pts/v2/payments.post.parameters[0].schema.properties.clientReferenceInformation.properties.code",
88+
# "new_field_name": "sdkCode"
89+
# }
90+
]
91+
92+
# Convert inputPaths to changes
93+
changes = convert_input_paths_to_changes(inputPaths)
94+
95+
script_dir = os.path.dirname(os.path.abspath(__file__))
96+
input_file_path = os.path.join(script_dir, input_file)
97+
output_file_path = os.path.join(script_dir, output_file)
98+
replace_field_names(input_file_path, output_file_path, changes)

0 commit comments

Comments
 (0)