Пример #1
0
    def request_response(self, formspec=None):
        self.start_server()
        if formspec:
            formdata = open(formspec, "rb")
        else:
            formdata = json.dumps(self.custom_form)
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        requests.post(
            f"{self.attended_server}/formspec",
            data=formdata,
            headers=headers,
        )
        br = Browser()
        br.open_browser(f"{self.attended_server}/form.html")
        br.set_window_size(600, 800)

        headers = {"Prefer": "wait=120"}
        response_json = None
        # etag = None
        while True:
            # if etag:
            #    headers['If-None-Match'] = etag
            headers["If-None-Match"] = "2434432243"
            response = requests.get(f"{self.attended_server}/requestresponse",
                                    headers=headers)
            # etag = response.headers.get("ETag")
            if response.status_code == 200:
                try:
                    response_json = response.json()
                    break
                except JSONDecodeError:
                    pass
            elif response.status_code != 304:
                # back off if the server is throwing errors
                time.sleep(60)
                continue
            time.sleep(1)

        br.close_browser()
        return response_json
Пример #2
0
    def request_response(self,
                         formspec: str = None,
                         window_width: int = 600,
                         window_height: int = 1000) -> dict:
        """Start server and show form. Waits for user response.

        :param formspec: form json specification file, defaults to None
        :param window_width: window width in pixels, defaults to 600
        :param window_height: window height in pixels, defaults to 1000
        :return: form response

        Example.

        .. code-block:: robotframework

            Create Form    ${CURDIR}/${/}myform.json
            &{response}    Request Response

        """
        self._start_attended_server()
        if self.custom_form is None:
            self.create_form("Requesting response")
        if formspec:
            formdata = open(formspec, "rb")
        else:
            formdata = json.dumps(self.custom_form)
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        requests.post(
            f"{self.server_address}/formspec",
            data=formdata,
            headers=headers,
        )

        response_json = {}
        try:
            br = Browser()
            br.open_available_browser(f"{self.server_address}/form.html")
            br.set_window_size(window_width, window_height)

            headers = {"Prefer": "wait=120"}
            # etag = None
            while True:
                # if etag:
                #    headers['If-None-Match'] = etag
                headers["If-None-Match"] = "2434432243"
                response = requests.get(
                    f"{self.server_address}/requestresponse", headers=headers)
                # etag = response.headers.get("ETag")
                if response.status_code == 200:
                    try:
                        response_json = response.json()
                        break
                    except JSONDecodeError:
                        break
                elif response.status_code != 304:
                    # back off if the server is throwing errors
                    time.sleep(10)
                    continue
                time.sleep(1)
        finally:
            br.close_browser()
            self._stop_attended_server()
        return response_json