Beispiel #1
0
def endpoint_schema_update(data):
    new_fields = []
    endpoint = RelativeEndpoint.objects.get(id=data['id'])
    endpoint.fields.exclude(id__in=[
        field['id'] for field in data['fields']
        if 'id' in field and field['id'] > 0
    ]).delete()  # delete fields which are no longer needed
    fields_to_change = []  # contains fields which need to be changed
    available_url_params = endpoint.url_params  # available url parameters
    schemas = list(Schema.objects.all().values_list(
        'name', flat=True))  # available schemas
    for field in data['fields']:
        if 'isChanged' not in field:
            continue
        if field['type'] == Field.SCHEMA:
            if field[
                    'value'] not in schemas:  # check if that data type is acceptable
                raise NotAllowed(
                    f"Please enter valid schema name for '{field['key']}', i.e. one of "
                    f"{', '.join(schemas)}")
        elif field['type'] == Field.VALUE:
            if field[
                    'value'] not in PrimitiveDataType.CHOICES:  # check if that data type is acceptable
                raise NotAllowed(
                    f"Please enter valid data type for '{field['key']}', i.e. one of "
                    f"{', '.join(PrimitiveDataType.CHOICES)}")
        elif field['type'] == Field.URL_PARAM:
            if field['value'] not in available_url_params:
                raise NotAllowed(
                    f"Please enter valid url param for '{field['key']}', i.e. one of "
                    f"{', '.join(available_url_params)}")
        elif field['type'] == Field.QUERY_PARAM:
            if not field['value']:
                raise NotAllowed(
                    f"Please enter valid string for '{field['key']}")
        else:
            raise NotAllowed(
                f'The field type should be one of {Field.SCHEMA}, {Field.VALUE}, {Field.URL_PARAM}'
            )
        if field['isChanged']:  # old fields
            fields_to_change.append(field)
        else:  # new fields
            new_fields.append(field)
    for field in fields_to_change:
        Field.objects.filter(id=field['id']).update(key=field['key'],
                                                    value=field['value'],
                                                    type=field['type'],
                                                    is_array=field['is_array'])
    Field.objects.bulk_create([
        Field(key=field['key'],
              value=field['value'],
              type=field['type'],
              relative_endpoint_id=data['id'],
              is_array=field['is_array']) for field in new_fields
    ])
    endpoint.meta_data = data['meta_data']
    endpoint.save()
    return endpoint
Beispiel #2
0
def relative_endpoint_add(data):
    methods = [k[0] for k in RelativeEndpoint.METHODS]
    if data['method'] not in methods:
        raise NotAllowed(
            f"Please enter valid method, i.e. one of {', '.join(methods)}")
    data['endpoint'], data['regex_endpoint'] = format_and_regex_endpoint(
        data['endpoint'])
    if RelativeEndpoint.objects.filter(base_endpoint_id=data['id'],
                                       endpoint=data['endpoint'],
                                       method=data['method']).exists():
        raise NotAllowed("Endpoint with same method already exists")
    relative_endpoint = RelativeEndpoint.objects.create(
        base_endpoint_id=data['id'],
        endpoint=data['endpoint'],
        method=data['method'],
        regex_endpoint=data['regex_endpoint'])
    return relative_endpoint
Beispiel #3
0
def get_random_value(_key, _type):
	if _type == 'boolean':
		return fake_data_generator.boolean()
	if _type == 'number':
		return fake_data_generator.random_int()
	if _type == 'string':
		if _key.lower() in faker_functions:
			return faker_functions[_key.lower()]()
		return fake_data_generator.word()
	raise NotAllowed('Only value of type string, number or boolean is allowed')
def schema_add(data):
    if Schema.objects.filter(name=data['name']).exists():
        raise NotAllowed(f"{data['name']} schema already exists")
    schema = Schema.objects.create(name=data['name'])
    schema_datas = []
    for field in data['fields']:
        schema_data = SchemaData(schema=schema,
                                 key=field['key'],
                                 type=field['type'])
        if field['type'] == 'schema':
            schema_data.value = Schema.objects.get(name=field['value']).id
        else:
            if field['value'] not in PrimitiveDataType.CHOICES:
                raise NotAllowed(
                    f"Please enter valid data type, i.e. one of {', '.join(PrimitiveDataType.CHOICES)}"
                )
            schema_data.value = PrimitiveDataType.CHOICES.index(field['value'])
        schema_datas.append(schema_data)
    SchemaData.objects.bulk_create(schema_datas)
Beispiel #5
0
def relative_endpoint_update(data):
    relative_endpoint = RelativeEndpoint.objects.select_related(
        'base_endpoint').get(id=data['id'])
    data['endpoint'], data['regex_endpoint'] = format_and_regex_endpoint(
        data['endpoint'])
    if RelativeEndpoint.objects.filter(
            base_endpoint=relative_endpoint.base_endpoint_id,
            endpoint=data['endpoint'],
            method=data['method']).exists():
        raise NotAllowed("Endpoint with same url exists")
    RelativeEndpoint.objects.filter(id=data['id']).update(
        endpoint=data['endpoint'],
        method=data['method'],
        regex_endpoint=data['regex_endpoint'])
def abc(request, route):
    endpoints = RelativeEndpoint.objects.annotate(
        final_endpoint=Concat('base_endpoint__endpoint', 'regex_endpoint')).all()
    endpoint = None
    #   metas = RelativeEndpoint.objects.filter()
    url_params = {}
    for _route in endpoints:
        regex = _route_to_regex(_route.final_endpoint)
        f = RegexPattern(regex[0])
        is_match = f.match(route)
        if is_match is not None:
            url_params = is_match[2]
            endpoint = _route
            break
    if endpoint is None:
        raise NotFound('Matching api endpoint not found')
    if request.method != endpoint.method:
        raise NotAllowed("This method is not allowed")
    response = Response(endpoint.fields.all(), endpoint.meta_data, request.GET.get('pageNo', '1'), url_params, request.GET.dict())
    return JsonResponse(response.create_response())