Esempio n. 1
0
    def execute_api_call(self, product):
        found_product = self.retrieve_product(product)
        if type(found_product) is str:
            flash("Product not found, please check the URL and try again")
            return redirect("/")

        if request.json.get("testing") or request.json.get("mock"):
            api_call = getattr(g.db, found_product.db_name).find_one({"_id": ObjectId(request.json.get("api_id"))})
        else:
            api_call = getattr(g.db, found_product.db_name).find_and_modify(
                query={"_id": ObjectId(request.json.get("api_id"))}, update={"$inc": {"accessed": 1}}
            )

        """ Change this to a jsonify call and have jquery handle it """
        if not api_call:
            flash("API Call was not found")
            return redirect("/")

        """ Retrieve all of the elements for the call """
        api_url, header, data_package = helper.generate_vars_for_call(found_product, api_call, request)

        """ Send off the request and retrieve the data elements """
        if request.json.get("mock"):
            response_headers, response_body, response_code = None, None, None
            request_headers = helper.pretty_format_data(header)
        else:
            request_headers, response_headers, response_body, response_code = helper.process_api_request(
                api_url, request.json.get("api_verb"), data_package, header
            )

        if request.json.get("mock") is None and request.json.get("testing") == "false":
            helper.log_api_call_request(
                request_headers,
                response_headers,
                response_body,
                response_code,
                api_call,
                request.json,
                data_package,
                api_url,
                found_product.title,
            )

        """ Send the data structure back to the browser """
        return jsonify(
            request_headers=request_headers,
            response_headers=response_headers,
            response_body=response_body,
            response_code=response_code,
            api_url=helper.pretty_format_url(api_url),
            data_package=helper.pretty_format_data(data_package),
        )
Esempio n. 2
0
    def execute_api_call(self, product):
        found_product = self.retrieve_product(product)
        if type(found_product) is str:
            flash('Product not found, please check the URL and try again')
            return redirect('/')

        if request.json.get('testing') or request.json.get('mock'):
            api_call = getattr(g.db, found_product.db_name).find_one(
                {'_id': ObjectId(request.json.get('api_id'))})
        else:
            api_call = getattr(g.db, found_product.db_name).find_and_modify(
                query={'_id': ObjectId(request.json.get('api_id'))},
                update={'$inc': {
                    'accessed': 1
                }})
        """ Change this to a jsonify call and have jquery handle it """
        if not api_call:
            flash('API Call was not found')
            return redirect('/')
        """ Retrieve all of the elements for the call """
        api_url, header, data_package = helper.generate_vars_for_call(
            found_product, api_call, request)
        """ Send off the request and retrieve the data elements """
        if request.json.get('mock'):
            response_headers, response_body, response_code = None, None, None
            request_headers = helper.pretty_format_data(header)
        else:
            request_headers, response_headers, response_body, response_code = (
                helper.process_api_request(api_url,
                                           request.json.get('api_verb'),
                                           data_package, header))

        if (request.json.get('mock') is None
                and request.json.get('testing') == 'false'):
            helper.log_api_call_request(request_headers, response_headers,
                                        response_body, response_code, api_call,
                                        request.json, data_package, api_url,
                                        found_product.title)
        """ Send the data structure back to the browser """
        return jsonify(request_headers=request_headers,
                       response_headers=response_headers,
                       response_body=response_body,
                       response_code=response_code,
                       api_url=helper.pretty_format_url(api_url),
                       data_package=helper.pretty_format_data(data_package))
Esempio n. 3
0
def pretty_print_url(url):
    return helper.pretty_format_url(url)