示例#1
0
def create_vessels_api(response: requests.Response) \
        -> Tuple[VesselsAPI, MagicMock]:
    connection = Connection('', '')
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = VesselsAPI(connection)
    return api, mocked_make_request
示例#2
0
def test_uses_provided_api_key():
    provided_key = "provided key"
    connection = Connection(api_key=provided_key)

    with patch.dict("os.environ", clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.kwargs["headers"]["Api-Key"] == provided_key
示例#3
0
def test_host_can_be_overridden_via_environment_variable(overridden_host: str):
    connection = Connection()

    with patch.dict("os.environ", {"SIGNAL_OCEAN_API_HOST": overridden_host},
                    clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.args == ("host/", )
示例#4
0
def test_host_can_be_overridden_via_parameter(overridden_host: str):
    connection = Connection(api_host=overridden_host)

    with patch.dict("os.environ", {},
                    clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.args == ("host/", )
示例#5
0
def test_targets_production_host_by_default():
    connection = Connection()

    with patch.dict("os.environ", {},
                    clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.args == ("https://api-gateway.signalocean.com/", )
示例#6
0
def test_has_no_api_key_if_not_provided_and_no_environment_variable():
    connection = Connection()

    with patch.dict("os.environ", {},
                    clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.kwargs["headers"]["Api-Key"] is None
示例#7
0
def test_uses_api_key_from_environment_variables_by_default():
    key_from_env_var = "key from env var"
    connection = Connection()

    with patch.dict("os.environ", {"SIGNAL_OCEAN_API_KEY": key_from_env_var},
                    clear=True), patch("requests.get") as get:
        connection._make_get_request(None)

    assert get.call_args.kwargs["headers"]["Api-Key"] == key_from_env_var
示例#8
0
def create_voyages_api(response_data: Union[Dict, List] = None) \
        -> Tuple[VoyagesAPI, MagicMock]:
    connection = Connection('', '')
    response = MagicMock()
    response.json.return_value = response_data
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = VoyagesAPI(connection)
    return api, mocked_make_request
示例#9
0
def create_voyages_api_multiple_requests(
        responses_data: List[Union[Dict, List]]) \
        -> Tuple[VoyagesAPI, MagicMock]:
    connection = Connection('', '')
    response = MagicMock()
    response.json = MagicMock(side_effect=responses_data)
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = VoyagesAPI(connection)
    return api, mocked_make_request
def create_sf_api(
    response_data: List = None, ) -> Tuple[ScrapedFixturesAPI, MagicMock]:
    connection = Connection("", "")
    response = MagicMock()
    response.json.return_value = response_data

    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = ScrapedFixturesAPI(connection, max_pages=2)
    return api, mocked_make_request
示例#11
0
def test_get_vessels():
    connection = Connection('', '')
    response = MagicMock()
    response.json.return_value = __mock_vessels_response
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = VoyagesAPI(connection)

    vessels = api.get_imos(VesselFilter("Mit"))

    assert [v.imo for v in vessels
            ] == [vessel.imo for vessel in __mock_vessels_response_model]
示例#12
0
def create_mock_connection(response_data:
                           Union[Dict, Tuple[Dict, ...], None]) \
        -> Tuple[Connection, MagicMock]:

    connection = Connection('', '')
    response = MagicMock()
    if response_data is None:
        response.status_code = requests.codes.not_found
    response.json.return_value = response_data
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    return connection, mocked_make_request
示例#13
0
def test_get_vessel_classes():
    connection = Connection('', '')
    response = MagicMock()
    response.json.return_value = __mock_vessel_classes_response
    mocked_make_request = MagicMock(return_value=response)
    connection._make_get_request = mocked_make_request
    api = VoyagesAPI(connection)

    vessel_classes = api.get_vessel_classes()

    assert [v.vessel_class_id for v in vessel_classes] == [
        vessel_class.vessel_class_id
        for vessel_class in __mock_vessel_classes_response_model
    ]
示例#14
0
def test_makes_get_requests_with_specified_parameters():
    connection = Connection("api_key", "api_host")
    query_string = {"key": "value"}

    with patch("requests.get") as get:
        connection._make_get_request("relative_url", query_string)

    get.assert_called_with(
        "api_host/relative_url",
        params=query_string,
        headers={
            "Api-Key": "api_key",
            "Content-Type": "application/json"
        },
    )
示例#15
0
def get_multiple(connection: Connection, relative_url: str, cls: Type[TModel],
                 query_string: Optional[QueryString] = None,
                 rename_keys: Optional[Dict[str, str]] = None) \
        -> Tuple[TModel, ...]:
    """Get a multiple objects from the API.

    Make a get request to the specified URL to retrieve a sequence of results
    and return a list of objects of the provided class instantiated with the
    retrieved data. If the API responds with an empty sequence an empty list
    is returned.

    Args:
        connection: The connection object to use to make the appropriate get
            request to the API.
        relative_url: The relative URL to make the request to.
        cls: The class to instantiate the object for the retrieved data.
        query_string: Query parameters for the request.
        rename_keys: Key names to rename to match model attribute names,
            used when an automated translation of the name from CapsWords
            to snake_case is to sufficient. Renaming must provide the name
            in CapsWords.
    """
    response = connection._make_get_request(relative_url,
                                            query_string=query_string)
    response.raise_for_status()
    data = response.json()
    return tuple(parse_model(d, cls, rename_keys=rename_keys) for d in data)
示例#16
0
def get_single(connection: Connection, relative_url: str, cls: Type[TModel],
               query_string: Optional[QueryString] = None,
               rename_keys: Optional[Dict[str, str]] = None) \
        -> Optional[TModel]:
    """Get a single object from the API.

    Make a get request to the specified URL and return an object of the
    provided class instantiated with the retrieved data. If the API responds
    with a "Not Found" status code, return None.

    Args:
        connection: The connection object to use to make the appropriate get
            request to the API.
        relative_url: The relative URL to make the request to.
        cls: The class to instantiate the object for the retrieved data.
        query_string: Query parameters for the request.
        rename_keys: Key names to rename to match model attribute names,
            used when an automated translation of the name from CapsWords
            to snake_case is to sufficient. Renaming must provide the name
            in CapsWords.

    Returns:
        An object of the provided class instantiated with the data retrieved
        from the specified URL, or None if the API responds with a "Not Found"
        status code.
    """
    response = connection._make_get_request(relative_url,
                                            query_string=query_string)

    if response.status_code == requests.codes.not_found:
        return None

    response.raise_for_status()
    data = response.json()
    return parse_model(data, cls, rename_keys=rename_keys)
示例#17
0
    def __init__(self, connection: Optional[Connection] = None):
        """Initializes VesselsAPI.

        Args:
            connection: API connection configuration. If not provided, the
                default connection method is used.
        """
        self.__connection = connection or Connection()
示例#18
0
from datetime import date, timedelta, time
from signal_ocean import PortAPI, VesselClassAPI, PortFilter, VesselClassFilter, Connection
from signal_ocean.historical_tonnage_list import VesselFilter, PushType, MarketDeployment, CommercialStatus, VesselSubclass
from signal_ocean.historical_tonnage_list import HistoricalTonnageListAPI
from signal_ocean.historical_tonnage_list import Column, IndexLevel

signal_ocean_api_key = 'NotValid'
connection = Connection(signal_ocean_api_key)

port_api = PortAPI(connection)
vessel_class_api = VesselClassAPI(connection)
htl_api = HistoricalTonnageListAPI(connection)
vessel_class = vessel_class_api.get_vessel_classes(
    VesselClassFilter(name_like='aframax'))[0]
port = port_api.get_ports(PortFilter(name_like='escravos'))[0]
laycanEndInDays = 20
start_date = date.today() - timedelta(days=5)
end_date = date.today()

vessel_filter = VesselFilter(
    push_types=[PushType.PUSHED, PushType.PUSHED_POSS],
    market_deployments=[MarketDeployment.RELET, MarketDeployment.SPOT],
    commercial_statuses=[
        CommercialStatus.AVAILABLE, CommercialStatus.CANCELLED,
        CommercialStatus.FAILED
    ],
    vessel_subclass=VesselSubclass.DIRTY,
    latest_ais_since=5)

htl_for_supply_trend = htl_api.get_historical_tonnage_list(
    port,