def get_tables_names(event, context): dynamodb = boto3.client('dynamodb') tables = dynamodb.list_tables() return build_response(200, {"message": "Succeeded", "tables": tables})
def main(): port = 1053 # DNS operates on port 53 by default, we use 1053 to mock traditional behavior ip = "127.0.0.1" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # using IPv4, UDP sock.bind((ip, port)) print("Listening on port 1053") while True: query, addr = sock.recvfrom( 512 ) # Queries are DNS Messages which may be sent to a name server to provoke a Response response_ = response.build_response( query) # Responses are DNS Messages sent back to answer the Query sock.sendto(response_, addr)
def handle(): """ Handler for `/api_name` methods **api_name = configured in api_hander hooks ### Examples: `/api_name/{methodname}` will call a whitelisted method """ try: validate_and_get_json_request() return handler.handle() except Exception, e: import traceback print traceback.format_exc() frappe.response["X_ERROR_CODE"] = "03" if "XML Request" in cstr(e) else "01" frappe.response["X_ERROR_DESC"] = cstr(e) return build_response("xml")
op = frappe.local.form_dict.op method = None try: method = get_attr(cmd) except AttributeError, e: return report_error(500, "Invalid API-URL") if op == 'login': login_user() elif cmd != 'login': is_guest = True if (method in frappe.guest_methods) else False if is_valid_request(is_guest=is_guest): execute_cmd(cmd) return build_response("json") def execute_cmd(cmd, async=False): """execute a request as python module""" method = get_attr(cmd) try: #check if whitelisted if frappe.session['user'] == 'Guest': if (method not in frappe.guest_methods): return report_error(403, "Not Allowed") else: if not method in frappe.whitelisted: return report_error(403, "Not Allowed")
from response import build_response,report_error from omnitechapp.doctype.package_detail.package_detail import update_user_package from validate import validate_request, get_user_package_in_json_format def handle(): """ Handler for `/api_name` methods **api_name = configured in api_hander hooks ### Examples: `/api_name/{methodname}` will call a whitelisted method """ print "in handle" error_code = "02" error_desc = "Success" try: validate_request() pkg = get_user_package_in_json_format(frappe.local.form_dict.data) if not update_user_package(pkg): raise Exception("Error while updating package") except Exception, e: import traceback print traceback.format_exc() error_code = "01" error_desc = cstr(e) finally: frappe.response['X_ERROR_CODE'] = error_code frappe.response['X_ERROR_DESC'] = error_desc return build_response("json")
def search(): db_conn = connect_to_psql() for arg in (x.name for x in fields(QueryArgs)): missing = [] if not request.args.get(arg): missing.append(arg) if missing: return Response(f"Missing GET parameter(s): {missing}", status=400) try: page = int(request.args.get("page")) rpp = int(request.args.get("rpp")) tnps = request.args.get("tnps").split(",") tnps_ints = list(map(int, tnps)) tnps_ints[1] += 1 # increase range to include end intervening = request.args.get("intervening").split(",") intervening_ints = tuple(map(int, intervening)) inexact = request.args.get("inexact").split(",") inexact_ints = tuple(map(int, inexact)) collection = int(request.args.get("collection")) query_str = request.args.get("query") qargs = QueryArgs(rpp, page, tnps, intervening, inexact, collection, query_str) except ValueError as e: return Response( f"Failed to parse parameter(s) to integer, got exception {str(e)}", status=400) query_score = indexers.parse(query_str) query_pb_notes = indexers.pb_notes(query_score) with db_conn.cursor() as cur: if collection == 0: # all pieces cur.execute("SELECT pid FROM Piece;") else: cur.execute( f"SELECT pid FROM Piece WHERE collection_id={collection}") collection_pids = [x[0] for x in cur.fetchall()] channel_opts = [('grpc.max_message_length', 1024**3), ('grpc.max_receive_message_length', 1024**3)] try: with grpc.insecure_channel(application.config['SMR_URI'], options=channel_opts) as channel: stub = smr_pb2_grpc.SmrStub(channel) response = stub.Search( smr_pb2.SearchRequest(notes=query_pb_notes, pids=collection_pids)) except Exception as e: return Response(f"failed to search: {str(e)}", status=500) print("smr service returned #" + str(len(response.occurrences)) + " occurrences") occfilters = occurrence.OccurrenceFilters(transpositions=range(*tnps_ints), intervening=intervening_ints, inexact=inexact_ints) search_response = build_response( db_conn, occurrence.filter_occurrences(response.occurrences, query_pb_notes, occfilters), qargs) if request.content_type == "application/json": return jsonify(search_response) else: return render_template("search.html", searchResponse=search_response)