def test_agent_env_with_trailing_slash_is_handled_correctly(monkeypatch):
    monkeypatch.setenv("TP_AGENT_URL", "http://localhost:8585/")
    agent_address = ConfigHelper.get_agent_service_address()
    assert (
        urljoin(agent_address, Endpoint.DevelopmentSession.value)
        == "http://127.0.0.1:8585/api/development/session"
    )
    def __init__(self, token: str, capabilities: dict,
                 report_settings: ReportSettings):
        self._remote_address = ConfigHelper.get_agent_service_address()
        self._capabilities = capabilities
        self._agent_session = None
        self._token = token
        self._report_settings = report_settings
        self._queue = queue.Queue()

        self._running = True
        self._reporting_thread = threading.Thread(target=self.__report_worker,
                                                  daemon=True)
        self._reporting_thread.start()

        if not self.__start_session():
            raise SdkException("Failed to start development mode session")
 def __init__(self, token: str, capabilities: dict,
              report_settings: ReportSettings):
     self._agent_session = None
     self._agent_response = None
     self._close_socket = False
     self._remote_address = ConfigHelper.get_agent_service_address()
     self._report_settings = report_settings
     self._capabilities = capabilities
     self._token = token
     # Attempt to start the session
     self.__start_session()
     # Make sure local reports are supported
     self.__verify_local_reports_supported(report_settings.report_type)
     # Running after all is initialized successfully
     self._running = True
     # After session started and is running, start the reporting thread
     self._queue = queue.Queue()
     self._reporting_thread = threading.Thread(target=self.__report_worker,
                                               daemon=True)
     self._reporting_thread.start()
    def get_agent_version(token: str):
        """Requests the current Agent status

        Args:
            token (str): The developer token used to communicate with the Agent

        Returns:
            AgentStatusResponse: contains the response to the sent Agent status request
        """

        with requests.Session() as session:
            response = session.get(
                urljoin(ConfigHelper.get_agent_service_address(),
                        Endpoint.GetStatus.value),
                headers={"Authorization": token},
            )

        try:
            response.raise_for_status()
            try:
                response_json = response.json()
                agent_version = response_json["tag"]
            except ValueError:
                raise SdkException(
                    "Could not parse Agent status response: no JSON response body present"
                )
            except KeyError:
                raise SdkException(
                    "Could not parse Agent status response: element 'tag' not found in JSON response body"
                )
        except HTTPError:
            raise AgentConnectException(
                f"Agent returned HTTP {response.status_code} when trying to retrieve Agent status"
            )

        return AgentStatusResponse(agent_version)
def test_predefined_agent_env_variable_resolves_to_specified_value(monkeypatch):
    monkeypatch.setenv("TP_AGENT_URL", "some_address")
    assert ConfigHelper.get_agent_service_address() == "some_address"
def test_undefined_agent_env_variable_resolves_to_default_agent_service_address(
    monkeypatch,
):
    monkeypatch.delenv("TP_AGENT_URL", raising=False)
    assert ConfigHelper.get_agent_service_address() == "http://127.0.0.1:8585"