Example #1
0
    def _execute_request_raw(self, conn, request):
        try:
            result = conn.getresponse()
        except:
            raise HubspotTimeout(None, request, traceback.format_exc())

        encoding = [
            i[1] for i in result.getheaders() if i[0] == 'content-encoding'
        ]
        possibly_encoded = result.read()
        try:
            possibly_encoded = zlib.decompress(possibly_encoded,
                                               16 + zlib.MAX_WBITS)
        except Exception:
            pass
        result.body = self._process_body(
            possibly_encoded,
            len(encoding) and encoding[0] == 'gzip')

        conn.close()
        if result.status in (404, 410):
            raise HubspotNotFound(result, request)
        elif result.status == 401:
            raise HubspotUnauthorized(result, request)
        elif result.status >= 400 and result.status < 500 or result.status == 501:
            raise HubspotBadRequest(result, request)
        elif result.status >= 500:
            raise HubspotServerError(result, request)

        return result
Example #2
0
    def submit_form(self,
                    portal_id: str,
                    form_guid: str,
                    data: dict,
                    context: dict = None,
                    **options) -> HTTPResponse:
        """
        submit to a form on hubspot
        does not require credentials

        this will urlencode the data automatically
        and if you pass a context:
            it will json dump for you, and place inside the data

        :see: https://developers.hubspot.com/docs/methods/forms/submit_form
        """
        if context:
            data["hs_context"] = json.dumps(context)
        subpath = "{}/{}".format(portal_id, form_guid)
        opts = {"content_type": "application/x-www-form-urlencoded"}
        options.update(opts)
        response = self._call(subpath,
                              method="POST",
                              data=urlencode(data),
                              raw=True,
                              **options)
        if response.status in [
                FormSubmissionClient.ResponseCode.SUCCESS,
                FormSubmissionClient.ResponseCode.SUCCESS_AND_REDIRECT,
        ]:
            return response
        if response.status == FormSubmissionClient.ResponseCode.NOT_FOUND:
            raise HubspotNotFound(response, None)
        if response.status == FormSubmissionClient.ResponseCode.ERROR:
            raise HubspotServerError(response, None)

        # shouldn't ever get here, but raise anyways
        raise HubspotServerError(response, None)