Example #1
0
    def delete_(self, name, id):
        if name not in self.data:
            return ctx.error("Entry not found", 1, 404)

        if id not in self.data[name]:
            return ctx.error("Entry not found", 1, 404)

        del self.data[name][id]
        return None
Example #2
0
    def get_(self, name, id):
        if name not in self.data:
            return ctx.error(
                "Entry '{}' not found for resource '{}'".format(id, name), 1,
                404)

        if id not in self.data[name]:
            return ctx.error("Entry not found", 1, 404)

        return self.data[name][id]
Example #3
0
    def replace_(self, name, id, entry):
        if name not in self.data:
            return ctx.error("Entry not found", 1, 404)

        if id not in self.data[name]:
            return ctx.error("Entry not found", 1, 404)

        self.data[name][id] = entry

        return
Example #4
0
    def update(self, crud):
        if crud.location not in self.data:
            self.data[crud.location] = {}

        if crud.id not in self.data[crud.location]:
            return ctx.error("Entry not found", 1, 404)

        current_data = self.data[crud.location][crud.id]
        for key in crud.data:
            if key not in current_data:
                return ctx.error("Bad field {}".format(key), 1, 400)
            current_data[key] = crud.data[key]

        self.data[crud.location][crud.id] = current_data

        crud.data = current_data
        return ctx.success(crud.dump(), 200)
Example #5
0
    def get(self, crud):
        if crud.location not in self.data:
            self.data[crud.location] = {}

        if crud.id not in self.data[crud.location]:
            return ctx.error("Entry not found", 1, 404)

        return ctx.success(self.data[crud.location][crud.id], 200)
Example #6
0
    def update_(self, name, id, entry):
        if name not in self.data:
            return ctx.error("Entry not found", 1, 404)

        if id not in self.data[name]:
            return ctx.error("Entry not found", 1, 404)

        data = self.data[name][id]

        for key in entry:
            if key.startswith("@"):
                continue
            if key not in data:
                return ctx.error("Unknown field '{}'".format(key), 1, 400)
            data[key] = entry[key]

        self.data[name][id] = data
        return data
Example #7
0
    def delete(self, crud):
        if crud.location not in self.data:
            self.data[crud.location] = {}

        if crud.id not in self.data[crud.location]:
            return ctx.error("Entry not found", 1, 404)

        del self.data[crud.location][crud.id]

        return ctx.success()
Example #8
0
    def add(self, crud):
        if crud.location not in self.data:
            self.data[crud.location] = {}

        if crud.id in self.data[crud.location]:
            return ctx.error("Entry already exists", 1, 400)

        self.data[crud.location][crud.id] = crud.data

        return ctx.success(crud.dump(), 201)
def onOperation(id=None):
    global registered_resources
    method = str(request.method).lower()
    rule = str(request.url_rule)

    if rule not in registered_resources:
        return ctx.error("unknown resource", 1, 404)

    obj = registered_resources[rule]
    foundOperations = [
        operation for operation in obj.getOperations()
        if operation.getMethod().lower() == method
    ]

    if len(foundOperations) == 0:
        return ctx.error("Method not allowed", 1, 405)

    for op in obj.getOperations():
        print(obj.resource_name, op.getMethod())

    if len(foundOperations) != 1:
        return ctx.error("Mutiple operations found", 1, 500)

    opObj = foundOperations[0]
    op = foundOperations[0].operation
    if method == "get" and id:
        return op(id, opObj)
    if method == "get":
        return op(opObj)
    if method == "option":
        return op(opObj)
    elif method == "post":
        return op(request.get_json(), opObj)
    elif method == "put" and id:
        return op(request.get_json(), id, opObj)
    elif method == "delete" and id:
        return op(id, opObj)
Example #10
0
def invoke(location, method, data=None, id=None):
    global registered_resources

    location = "/" + location

    if location not in registered_resources:
        return ctx.error("unknown resource", 1, 404)

    obj = registered_resources[location]()
    obj.setup()
    if method == "GET" and id:
        return obj.get(id)
    if method == "GET":
        return obj.getAll()
    elif method == "POST":
        return obj.post(data)
    elif method == "PUT":
        return obj.put(data, id)
    elif method == "DELETE":
        return obj.delete(id)
def onContext():
    global registered_classes

    method = str(request.method).lower()
    rule = str(request.url_rule)
    ctxName = rule.replace("/api/contexts/", "").replace(".jsonld", "")

    classes = [
        obj for obj in registered_classes.values()
        if obj.contextName == ctxName
    ]

    if len(classes) == 0:
        return ctx.error("unknown resource", 1, 404)

    obj = classes[0]
    context_json = {"@context": obj.apiContext}

    context_json["@context"]["hydra"] = "http://www.w3.org/ns/hydra/core#"
    context_json["@context"]["vocab"] = VOCAB_URL + "#"

    return ctx.success(context_json, 200, headers=LINK_HEADER)
Example #12
0
def run(id=None):
    global registered_resources
    method = str(request.method)
    rule = str(request.url_rule)
    print(rule)
    if rule not in registered_resources:
        return ctx.error("unknown resource", 1, 404)

    obj = registered_resources[rule]()
    obj.setup()
    if method == "GET" and id:
        return obj.get(id)
    if method == "GET":
        return obj.getAll()
    if method == "OPTION":
        return obj.option()
    elif method == "POST":
        return obj.post(request.get_json())
    elif method == "PUT" and id:
        return obj.put(request.get_json(), id)
    elif method == "DELETE" and id:
        return obj.delete(id)