<commit_before>import json
import urllib.parse
from http.server import SimpleHTTPRequestHandler

from provider.ns import NSDepartureTimesProvider, NSTravelAdviceProvider
from provider.buienradar import BuienRadarDataProvider
from provider.fx import FxDataProvider
from provider.ovapi import OvApiDataProvider

# Route-to-handler mappings
ROUTE_MAP = {
    '/ns/dep-times':     NSDepartureTimesProvider,
    '/ns/travel-advice': NSTravelAdviceProvider,
    '/buienradar':       BuienRadarDataProvider,
    '/fx':               FxDataProvider,
    '/ovapi':            OvApiDataProvider,
}


class InfoPiRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        # Parse the request
        url_components = urllib.parse.urlsplit(self.path)
        url_path = url_components[2]

        # Parse the query string
        url_params = urllib.parse.parse_qs(url_components[3])
        # De-list single-item value lists
        for name, value in url_params.items():
            if type(value) is list and len(value) == 1:
                url_params[name] = value[0]

        # No appropriate handler found, consider it's a static resource: hand over to the original handler
        if url_path not in ROUTE_MAP:
            return SimpleHTTPRequestHandler.do_GET(self)

        # Instantiate and run the handler
        data = ROUTE_MAP[url_path](**url_params).get()

        # Send response status code
        self.send_response(200)

        # Send headers
        self.send_header('Content-type', 'application/json')
        self.end_headers()

        # Write the response
        self.wfile.write(bytes(json.dumps(data), 'utf8'))
<commit_msg>Return 500 HTTP error on server's data fetching failure<commit_after>import json
import urllib.parse
from http.server import SimpleHTTPRequestHandler

from provider.ns import NSDepartureTimesProvider, NSTravelAdviceProvider
from provider.buienradar import BuienRadarDataProvider
from provider.fx import FxDataProvider
from provider.ovapi import OvApiDataProvider

# Route-to-handler mappings
ROUTE_MAP = {
    '/ns/dep-times':     NSDepartureTimesProvider,
    '/ns/travel-advice': NSTravelAdviceProvider,
    '/buienradar':       BuienRadarDataProvider,
    '/fx':               FxDataProvider,
    '/ovapi':            OvApiDataProvider,
}


class InfoPiRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        # Parse the request
        url_components = urllib.parse.urlsplit(self.path)
        url_path = url_components[2]

        # Parse the query string
        url_params = urllib.parse.parse_qs(url_components[3])
        # De-list single-item value lists
        for name, value in url_params.items():
            if type(value) is list and len(value) == 1:
                url_params[name] = value[0]

        # No appropriate handler found, consider it's a static resource: hand over to the original handler
        if url_path not in ROUTE_MAP:
            return SimpleHTTPRequestHandler.do_GET(self)

        # Instantiate and run the handler
        data = ROUTE_MAP[url_path](**url_params).get()

        # None means data fetching failed - return a 500 error
        if data is None:
            self.send_response(500)

            # Send headers
            self.send_header('Content-type', 'text/plain')
            self.end_headers()

            # Write the response
            self.wfile.write(b'Data fetching failed')

        # Data fetched successfully
        else:
            # Send response status code
            self.send_response(200)

            # Send headers
            self.send_header('Content-type', 'application/json')
            self.end_headers()

            # Write the response
            self.wfile.write(bytes(json.dumps(data), 'utf8'))

{'fraction_non_alphanumeric': 0.08184958809460537, 'fraction_numerical': 0.006643635397289397, 'mean_word_length': 2.647286821705426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460475', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1058, 'n_words': 271}