예제 #1
0
    def get_gtfs_data(self, csv_filenames: List[str]) -> Dict[str, BytesIO]:
        """
        Retrieve GTFS files of the STIB network.
        The data is cached for two weeks following Open Data API recommendations.
        """

        try:
            logger.info(
                {"operation": "Getting GTFS files", "csv_filenames": csv_filenames,}
            )
            api_request = ApiClientRequest(
                url=self.GTFS_FILES_SUFFIX,
                method="GET",
                headers=[("Accept", "application/zip")],
            )
            response = self.api_client.invoke(api_request)
            file = BytesIO(response.body.content)
            if is_zipfile(file):
                with ZipFile(file) as gtfs_zip_file:
                    logger.info(
                        {
                            "operation": "Inspecting GTFS data zip file content",
                            "zip_file_content": gtfs_zip_file.namelist(),
                        }
                    )
                    csv_files = {
                        csv_filename: BytesIO(gtfs_zip_file.read(name=csv_filename))
                        for csv_filename in csv_filenames
                    }
                    return csv_files
        except ApiClientException as e:
            raise GTFSDataError("Error getting GTFS files", e)

        except Exception as e:
            raise GTFSDataError("Error getting GTFS files", e)
예제 #2
0
    def get_stops_by_line_id(self, line_id: str) -> Optional[List[LineDetails]]:
        """
        Retrieve line information based on a line ID of the STIB network.
        The data is cached for one day following Open Data API recommendations.
.       """

        try:
            logger.info(
                {"operation": "Getting line details", "line_id": line_id,}
            )
            tracer.put_metadata(key="line_id", value=line_id)
            tracer.put_annotation("STIB_LINE_ID", line_id)

            request_url = self.STOPS_BY_LINE_SUFFIX + line_id
            api_request = ApiClientRequest(url=request_url, method="GET")
            response = self.api_client.invoke(api_request)
            raw_lines_info = response.body.json()
            line_details = LineDetails.schema().load(raw_lines_info["lines"], many=True)
            self._enrich_line_details_with_gtfs_data(line_details)
            return line_details

        except (ApiClientException, ValidationError) as e:
            raise NetworkDescriptionError(e, line_id)

        except Exception as e:
            raise NetworkDescriptionError(e, line_id)
예제 #3
0
 def setUp(self):
     self.valid_request = ApiClientRequest(method="GET",
                                           url="https://test.com",
                                           body=None,
                                           headers=None)
     self.valid_mock_response = MockResponse(json_data="some test data",
                                             status_code=200)
     self.test_api_client = DefaultApiClient()
    def test_resolve_invalid_http_method_throw_exception(self):
        test_invalid_method_request = ApiClientRequest(
            method="GET_TEST", url="http://test.com", body=None, headers=None)

        with mock.patch("requests.get",
                        side_effect=lambda *args, **kwargs:
                        self.valid_mock_response):
            with self.assertRaises(ApiClientException) as exc:
                self.test_api_client.invoke(test_invalid_method_request)

            assert "Invalid request method: GET_TEST" in str(exc.exception)
    def test_api_client_invoke_with_no_url_schema_throw_error(self):
        test_invalid_url_scheme_request = ApiClientRequest(
            method="GET", url="test.com", body=None, headers=None)

        with mock.patch("requests.get",
                        side_effect=lambda *args, **kwargs:
                        self.valid_mock_response):
            with self.assertRaises(ApiClientException) as exc:
                self.test_api_client.invoke(test_invalid_url_scheme_request)

            assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
예제 #6
0
    def get_passing_times_for_stop_id_and_line_id(
        self, stop_id: str, line_id: str
    ) -> Optional[List[PassingTime]]:
        """
        Retrieve arrival times at a given stop based on the stop ID and line ID of the STIB network.
        The data is cached for 20 seconds following Open Data API recommendations.
        """

        try:
            logger.info(
                {
                    "operation": "Getting arrival times",
                    "line_id": line_id,
                    "stop_id": stop_id,
                }
            )

            tracer.put_metadata(key="line_id", value=line_id)
            tracer.put_metadata(key="stop_id", value=stop_id)
            tracer.put_annotation("STIB_LINE_ID", line_id)
            tracer.put_annotation("STIB_STOP_ID", stop_id)

            request_url = self.PASSING_TIME_BY_POINT_SUFFIX + stop_id
            api_request = ApiClientRequest(url=request_url, method="GET")
            response = self.api_client.invoke(api_request)
            raw_passages = response.body.json()
            point_passing_times = PointPassingTimes.schema().load(
                raw_passages["points"], many=True
            )
            return self._filter_passing_times_by_line_id(point_passing_times, line_id)

        except (ApiClientException, ValidationError) as e:
            raise OperationMonitoringError(e, line_id=line_id, stop_id=stop_id)

        except Exception as e:
            raise OperationMonitoringError(e, line_id=line_id, stop_id=stop_id)