I tried to link the Python module for Front and the API created by Spring Boot, so I will leave a note of the flow.
The Python module uses a web framework called ** Pyramid **.
Send an HTTP request from your browser
↓
Receive in Python and make a request
↓
Send a request to the API and receive a response
↓
Process the response and display the template file in the browser
Invoke the Python module to receive the request from the browser In order to receive the request from Python, the API is started.
(env) username@host:~/dirname/PythonProject1$ pserve development.ini
Starting server in PID 6660.
Serving on http://0.0.0.0:50001
Enter the URL below into your browser.
http://0.0.0.0:50001/hotel/
context/view.config
Manage routing here.
When / hotel
is accessed, the processing of ** view_hotel_list ** of ** HotelListContext ** is started.
def set_detail_setting(config):
RootContext.set_child(
name='hotel',
factory=HotelListContext,
)
config.add_view(
context=HotelListContext,
view=context_as_view,
attr='view_hotel_list',
renderer='hotel/common/hotel/listHotel.jinja2',
request_method='GET',
name=''
)
context/list/list.py
The process is passed to the Executor, the result is validated and data is processed, and the result is passed.
class HotelListContext(HotelListBaseContext):
def view_hotel_list(self):
logger.info('Start search hotel result.')
list_executor = Executor(self.list_service)
list_form = self.validated_form(HotelSchema)
response = list_executor.execute_search_products(list_form.data)
logger.info(response)
return {
'data': {
'hotels': response['body']['items'],
'pagination': response['body']['pagination']
}
}
context/list/executor/__init__.py
After creating JSON for the request, send the request to the API. Return the response to /context/list/list.py.
class HotelSearchListExecutor(object):
def __init__(self, service):
self.service = service
def execute_search_products(self, condition):
detail_condition = {
'header': executor_create_header_request(condition),
'body': {
'query': condition.get('query')
'searchBy': condition.get('searchBy'),
'pageNum': condition.get('pageNum'),
'sortBy': condition.get('pageNum'),
'sortOrder': condition.get('sortOrder')
}
}
return self.service.search_hotel(detail_condition)
listHotel_jinja2
Display JSON passed from /context/list/list.py in jinja2
<!DOCTYPE html>
<html lang=
"en">
<head>
<title>Test page</title>
</head>
<body>
<table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>cityCode</td>
</tr>
{% for item in data.hotels %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.cityCode }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
development.ini
Define the URL when making a request to the API here
URL = http://localhost:8080/hotel/
executor_helper.py
context/list/executor/__init__.py
-> executor_create_header_request(condition)
This is the request header template used in the above process.
def executor_create_header_request(condition):
return {
'languageCode': 'en',
'countryCode': 'US',
'currencyCode': 'USD'
}
schema/hotel/__init__.py
/context/list/list.py ⇒ self.validated_form(HotelSchema)
Here, the validate process is performed.
class HotelSchema(Schema):
allow_extra_fields = True
filter_extra_fields = True
query = validators.UnicodeString(not_empty=False, if_missing='')
sortBy = validators.UnicodeString(not_empty=False, if_missing='name')
pageNum = validators.UnicodeString(not_empty=False, if_missing='1')
sortOrder = validators.UnicodeString(not_empty=False, if_missing='ASC')
DB data is displayed on the browser screen through the API.
With this, it was confirmed that the Python module and API could be linked.
Recommended Posts