Exemplo n.º 1
0
def explorer(request):
    if request.POST:
        form = RequestForm(request.POST)
        if form.is_valid():
            method = form.cleaned_data['method']
        else:
            method = "get"
    else:
        form = RequestForm()
        method = "get"

    post_endpoint = request.POST.get("endpoint", False)
    querystring_endpoint = request.GET.get("endpoint", "/v1/")
    endpoint = post_endpoint or querystring_endpoint
    form.fields.get('endpoint').initial = endpoint
    
    user = form.data.get("user", default_user[0])
    pwd = test_users[user]
    headers = copy(HEADERS)
    headers["HTTP_AUTHORIZATION"] = create_auth_string(user, pwd)
    print "Relaying {method} request for {user} to {endpoint}".format(user=user, endpoint=endpoint, method=method.upper())
    
    response = getattr(api, method)(endpoint, data=form.data.get('data', ''), content_type='application/json', **headers)
    status_code = response.status_code
    try:
        response = prettify(response.content)
    except simplejson.JSONDecodeError:
        response = response.content

    return direct_to_template(request, "explorer.html", {
        "starting_points": get_base_uris('/v1/'),
        "path": request.META["HTTP_HOST"],
        "endpoint": endpoint,
        "form": form,
        "headers": headers,
        "status": status_code,
        "response": response,
        "method": method,
        "username": user,
        "password": get_password(user),
        "data": form.data.get("data", ""),
        })
Exemplo n.º 2
0
test_users = {}
for user in User.objects.all():
    test_users[user.username] = get_password(user.username)
user_choices = zip(test_users.keys(), test_users.keys())


def get_base_uris(base):
    content = api.get(base).content
    data = simplejson.loads(content)
    uridict = data.values()
    return [uri["list_endpoint"] for uri in uridict]

default_user = ("dunno", "yet")

HEADERS = {
    "HTTP_AUTHORIZATION": create_auth_string(*default_user),
    "HTTP_CONTENT_TYPE": "application/json",
    "HTTP_ACCEPT": "application/json",
    }

METHODS = (
    ('get', 'GET'),
    ('post', 'POST'),
    ('put', 'PUT'),
    ('delete', 'DELETE'),
    )


class RequestForm(forms.Form):
    method = forms.ChoiceField(choices=METHODS, initial='GET', required=False)
    endpoint = forms.CharField(max_length=200, initial='/v1/', required=False)