I have been trying to get a section of code to work and can get 1 way to search by the lane column, but the api wants to call by Origin City and Destination fields in the mssql database.
This is returned in the when it is searched in the browser with https://IPaddr:5000/rates/city?origin_city=FARGO&destination=ND
Browser:
{
“error”: “HTTPConnectionPool(host=‘127.0.0.1’, port=5000): Max retries exceeded with url: /rates/city?origin_city=FARGO&destination=ND (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000002821152BA50>: Failed to establish a new connection: [WinError 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full’))”
}
Python App
{
127.0.0.1 - - [17/Oct/2023 14:11:50] “GET /rates/city?origin_city=FARGO&destination=ND HTTP/1.1” 200 -
Request URL: http://127.0.0.1:5000/rates/city?origin_city=FARGO&destination=ND
Response status code: 200
Response content: {
“error”: “HTTPConnectionPool(host=‘127.0.0.1’, port=5000): Max retries exceeded with url: /rates/city?origin_city=FARGO&destination=ND (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000001A70127DD50>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted’))”
}
My Routes.py looks like this:
from flask import Flask, jsonify, request
import requests
from models import Rates # Import your model
BASE_URL = 'http://127.0.0.1:5000'
def get_rates_by_city():
origin_city = request.args.get('origin_city')
destination = request.args.get('destination')
try:
response = requests.get(f'{BASE_URL}/rates/city', params={'origin_city': origin_city, 'destination': destination})
# Log the URL and response
print('Request URL:', response.url)
print('Response status code:', response.status_code)
print('Response content:', response.text)
# Return the JSON response
return response.json()
except requests.exceptions.RequestException as e:
return jsonify({'error': str(e)}), 500
def setup_routes(app):
@app.route('/rates', methods=['GET'])
def get_rates():
try:
rates = Rates.query.all()
rates_data = [{'lane': rate.lane, 'origin_city': rate.origin_city, 'destination': rate.destination, 'min_rate': rate.min_rate} for rate in rates]
return jsonify(rates_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/rates/city', methods=['GET'])
def handle_get_rates_by_city():
return get_rates_by_city()
@app.route('/rates/lane/<lane>', methods=['GET'])
def get_rates_by_lane(lane):
try:
rates = Rates.query.filter_by(lane=lane).all()
rates_data = [{'lane': rate.lane, 'origin_city': rate.origin_city, 'destination': rate.destination, 'min_rate': rate.min_rate, 'per_mile_rate': rate.per_mile_rate} for rate in rates]
return jsonify(rates_data)
except Exception as e:
return jsonify({'error': str(e)}), 500