Skip to content

Commit b35bda5

Browse files
Tomas.Pail@dlubal.comTomas.Pail@dlubal.com
authored andcommitted
Merge remote-tracking branch 'remotes/origin/main' into 336-bug-accessing-spectralanalysismembersinternalforces-for-different-envelopes-not-possible
2 parents 930cb5c + 9f019a7 commit b35bda5

17 files changed

+131
-23
lines changed

RFEM/connectionGlobals.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
12
url = "http://127.0.0.1"
23
port = "8081"
34
connected = False
5+
api_key = None
6+
verify = True # This can be a boolean or a path to a CA bundle file (in case of self-signed certificate it's required)
47

58
# Is filled in the initModel
69
client = None
710
ca = None
811
cacheLoc = ""
9-
session = None
12+
session = None

RFEM/initModel.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import RFEM.dependencies # dependency check ahead of imports
44
import socket
5+
import ssl
56
import requests
67
import xmltodict
78
from urllib import request
@@ -29,10 +30,27 @@ def connectToServer(url=connectionGlobals.url, port=connectionGlobals.port):
2930
# local machine url format: 'http://127.0.0.1'
3031
urlAndPort = f'{url}:{port}'
3132

32-
# Check if port is listening
33-
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
33+
# Parse the hostname from the URL
34+
if url.startswith('https://'):
35+
hostname = url[8:] # Remove 'https://'
36+
context = ssl.create_default_context()
37+
if isinstance(connectionGlobals.verify, str):
38+
context.load_verify_locations(cafile=connectionGlobals.verify)
39+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40+
a_socket = context.wrap_socket(sock, server_hostname=hostname)
41+
new_wsdl = request.urlopen(urlAndPort+'/wsdl', context=context)
42+
elif url.startswith('http://'):
43+
hostname = url[7:] # Remove 'http://'
44+
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
45+
new_wsdl = request.urlopen(urlAndPort+'/wsdl')
46+
else:
47+
hostname = url
48+
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49+
new_wsdl = request.urlopen(urlAndPort+'/wsdl')
3450

35-
location = (url[7:], int(port))
51+
location = (hostname, int(port))
52+
53+
# Check if port is listening
3654
result_of_check = a_socket.connect_ex(location)
3755

3856
if result_of_check == 0:
@@ -46,7 +64,7 @@ def connectToServer(url=connectionGlobals.url, port=connectionGlobals.port):
4664

4765
# Delete old cache if the version or mode doesn't correlate
4866
connectionGlobals.cacheLoc = os.path.join(gettempdir(), 'WSDL')
49-
new_wsdl = request.urlopen(urlAndPort+'/wsdl')
67+
5068
new_wsdl_data = new_wsdl.read()
5169
new_wsdl.close()
5270
new_tns = xmltodict.parse(new_wsdl_data)['definitions']['@targetNamespace']
@@ -65,10 +83,15 @@ def connectToServer(url=connectionGlobals.url, port=connectionGlobals.port):
6583
# Check for issues locally and remotely
6684
try:
6785
connectionGlobals.ca = DocumentCache(location=connectionGlobals.cacheLoc)
68-
connectionGlobals.client = Client(urlAndPort+'/wsdl', location = urlAndPort, cache=connectionGlobals.ca)
86+
trans = RequestsTransport(
87+
api_key=connectionGlobals.api_key,
88+
session=connectionGlobals.session,
89+
verify=connectionGlobals.verify
90+
)
91+
connectionGlobals.client = Client(urlAndPort+'/wsdl', location = urlAndPort, cache=connectionGlobals.ca, transport=trans)
6992
connectionGlobals.connected = True
7093

71-
except:
94+
except Exception:
7295
print('Error: Connection to server failed!')
7396
print('Please check:')
7497
print('- If you have started RFEM application')
@@ -82,7 +105,7 @@ def connectToServer(url=connectionGlobals.url, port=connectionGlobals.port):
82105

83106
try:
84107
modelLst = connectionGlobals.client.service.get_model_list()
85-
except:
108+
except Exception:
86109
print('Error: Please check if all RFEM dialogs are closed.')
87110
input('Press Enter to exit...')
88111
sys.exit()
@@ -169,7 +192,12 @@ def __init__(self,
169192
connectionGlobals.session = requests.Session()
170193
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
171194
connectionGlobals.session.mount('http://', adapter)
172-
trans = RequestsTransport(connectionGlobals.session)
195+
connectionGlobals.session.mount('https://', adapter)
196+
trans = RequestsTransport(
197+
api_key=connectionGlobals.api_key,
198+
session = connectionGlobals.session,
199+
verify=connectionGlobals.verify
200+
)
173201

174202
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
175203

@@ -201,7 +229,12 @@ def __init__(self,
201229
connectionGlobals.session = requests.Session()
202230
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
203231
connectionGlobals.session.mount('http://', adapter)
204-
trans = RequestsTransport(connectionGlobals.session)
232+
connectionGlobals.session.mount('https://', adapter)
233+
trans = RequestsTransport(
234+
api_key=connectionGlobals.api_key,
235+
session = connectionGlobals.session,
236+
verify=connectionGlobals.verify
237+
)
205238

206239
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
207240
elif model_name == "":
@@ -213,7 +246,12 @@ def __init__(self,
213246
connectionGlobals.session = requests.Session()
214247
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
215248
connectionGlobals.session.mount('http://', adapter)
216-
trans = RequestsTransport(connectionGlobals.session)
249+
connectionGlobals.session.mount('https://', adapter)
250+
trans = RequestsTransport(
251+
api_key=connectionGlobals.api_key,
252+
session = connectionGlobals.session,
253+
verify=connectionGlobals.verify
254+
)
217255

218256
cModel = Client(modelCompletePath, transport=trans, location = modelUrlPort, cache=connectionGlobals.ca, timeout=360)
219257
else:

RFEM/suds_requests.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,31 @@ def wrapper(*args, **kwargs):
4646

4747

4848
class RequestsTransport(transport.Transport):
49-
def __init__(self, session=None):
49+
def __init__(self, session=None, api_key=None, verify=True):
5050
transport.Transport.__init__(self)
5151
self._session = session or requests.Session()
52+
self.api_key = api_key
53+
self.verify = verify
5254

5355
@handle_errors
5456
def open(self, request):
55-
resp = self._session.get(request.url)
57+
headers = {'Authorization': f'Bearer {self.api_key}'} if self.api_key else {}
58+
resp = self._session.get(request.url, headers=headers, verify=self.verify)
5659
resp.raise_for_status()
5760
return BytesIO(resp.content)
5861

5962
@handle_errors
6063
def send(self, request):
64+
headers = request.headers
65+
if self.api_key:
66+
headers['Authorization'] = f'Bearer {self.api_key}'
6167
resp = self._session.post(
6268
request.url,
6369
data=request.message,
64-
headers=request.headers,
70+
headers=headers,
71+
verify=self.verify,
6572
)
66-
if resp.headers.get('content-type') not in ('text/xml',
67-
'application/soap+xml'):
73+
if resp.headers.get('content-type') not in ('text/xml','application/soap+xml'):
6874
resp.raise_for_status()
6975
return transport.Reply(
7076
resp.status_code,

UnitTests/test_DesignOverview.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import os
3+
import pytest
34
PROJECT_ROOT = os.path.abspath(os.path.join(
45
os.path.dirname(__file__),
56
os.pardir)
@@ -8,6 +9,7 @@
89

910
from RFEM.enums import AddOn
1011
from RFEM.initModel import Model, SetAddonStatus, getPathToRunningRFEM
12+
from RFEM.connectionGlobals import url
1113
from RFEM.Results.designOverview import GetDesignOverview, GetPartialDesignOverview
1214
from RFEM.Reports.partsList import GetPartsListAllByMaterial, GetPartsListMemberRepresentativesByMaterial
1315
from RFEM.Reports.partsList import GetPartsListMemberSetsByMaterial, GetPartsListMembersByMaterial
@@ -16,6 +18,10 @@
1618
if Model.clientModel is None:
1719
Model()
1820

21+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
22+
Althought it is easy to change, it would not be easy to update on every remote computer.\
23+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
24+
1925
def test_designOverview():
2026

2127
Model.clientModel.service.delete_all()

UnitTests/test_Export.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
if Model.clientModel is None:
1919
Model()
2020

21+
@pytest.mark.skipif(connectionGlobals.url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file paths. \
22+
Althought it is easy to change, it would not be easy to update on every remote computer.\
23+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
2124
def test_export():
2225

2326
Model.clientModel.service.delete_all()
@@ -89,4 +92,4 @@ def test_import():
8992

9093
#assert getSAFSettings().property_general_run_excel_application == False
9194
assert getSAFSettings().property_export_saf_version == '1_0_5'
92-
assert getSAFSettings().property_import_show_conversion_tables_after_import == False
95+
assert getSAFSettings().property_import_show_conversion_tables_after_import == False

UnitTests/test_GetAllObjects.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
)
77
sys.path.append(PROJECT_ROOT)
88
from RFEM.initModel import Model, getPathToRunningRFEM
9+
from RFEM.connectionGlobals import url
910
from RFEM.Tools.GetObjectNumbersByType import GetAllObjects
11+
import pytest
1012

1113
if Model.clientModel is None:
1214
Model()
1315

16+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
17+
Althought it is easy to change, it would not be easy to update on every remote computer.\
18+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
1419
def test_GetAllObjects():
1520

1621
Model.clientModel.service.delete_all()

UnitTests/test_GlobalParameters_Test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from RFEM.enums import GlobalParameterUnitGroup, GlobalParameterDefinitionType, ObjectTypes
1414
from RFEM.globalParameter import GlobalParameter
1515
from RFEM.initModel import Model, getPathToRunningRFEM
16+
from RFEM.connectionGlobals import url
17+
import pytest
1618

1719
if Model.clientModel is None:
1820
Model()
@@ -79,6 +81,10 @@ def test_global_parameters():
7981
assert gp_2.steps == 4
8082
assert gp_2.unit_group == 'LOADS_DENSITY'
8183

84+
85+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
86+
Althought it is easy to change, it would not be easy to update on every remote computer.\
87+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
8288
def test_set_and_get_formula():
8389

8490
Model.clientModel.service.delete_all()

UnitTests/test_Reports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
if Model.clientModel is None:
1717
Model()
1818

19-
2019
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
2120
Althought it is easy to change, it would not be easy to update on every remote computer.\
2221
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
@@ -34,6 +33,9 @@ def test_html_report():
3433

3534
assert os.path.exists(folderPath)
3635

36+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
37+
Althought it is easy to change, it would not be easy to update on every remote computer.\
38+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
3739
def test_printout_report():
3840
# Remove any previous results if they exist
3941
dirname = os.path.join(os.getcwd(), os.path.dirname(__file__))

UnitTests/test_ResultTables.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
)
1010
sys.path.append(PROJECT_ROOT)
1111
from RFEM.initModel import Model, getPathToRunningRFEM
12+
from RFEM.connectionGlobals import url
1213
from RFEM.enums import CaseObjectType
1314
from RFEM.Results.resultTables import ResultTables
1415

1516
if Model.clientModel is None:
1617
Model()
1718

19+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
20+
Althought it is easy to change, it would not be easy to update on every remote computer.\
21+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
1822
def test_result_tables():
1923
Model.clientModel.service.delete_all()
2024
Model.clientModel.service.run_script(os.path.join(getPathToRunningRFEM(),'scripts\\internal\\Demos\\Demo-004 Bus Station-Concrete Design.js'))

UnitTests/test_SectionDialogue.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
sys.path.append(PROJECT_ROOT)
88

99
from RFEM.initModel import Model
10+
from RFEM.connectionGlobals import url
1011
from RFEM.Tools.sectionDialogue import *
1112
from RFEM.BasicObjects.section import Section
1213
from RFEM.BasicObjects.material import Material
14+
import pytest
1315

1416
if Model.clientModel is None:
1517
Model()
1618

19+
@pytest.mark.skipif(url != 'http://127.0.0.1', reason="This test fails on remote PC due to incorrect file path. \
20+
Althought it is easy to change, it would not be easy to update on every remote computer.\
21+
It is not necessary to evaluate Client as functional. Localy this tests still gets executed.")
1722
def test_section_dialogue():
1823

1924
Model.clientModel.service.delete_all()

0 commit comments

Comments
 (0)