<commit_before>import hashlib

from lettuce import world
from tornado.escape import json_decode
from tornado.httpclient import HTTPClient, HTTPRequest

from newebe.settings import TORNADO_PORT
from newebe.profile.models import UserManager, User

ROOT_URL = "http://localhost:%d/" % TORNADO_PORT

class NewebeClient(HTTPClient):
    '''
    Tornado client wrapper to write requests to Newebe faster.
    '''


    def login(self, password):
        '''
        Grab authentication cookie from login request.
        '''
        response = self.fetch(self.root_url + "login/json/", 
                method="POST", body='{"password":"%s"}' % password)
        
        assert response.headers["Set-Cookie"].startswith("password=")
        self.cookie = response.headers["Set-Cookie"]


    def set_default_user(self, url=ROOT_URL):
        '''
        Set to DB default user. This is useful for automatic login.
        '''
        self.root_url = url

        user = UserManager.getUser()
        if user:
            user.delete()
        
        user = User(
            name = "<NAME>",
            password = <PASSWORD>("<PASSWORD>(),
            key = None,
            authorKey = "authorKey",
            url = url,
            description = "my description"
        )
        user.save()


    def get(self, url):
        '''
        Perform a GET request.
        '''
        request = HTTPRequest(url)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def post(self, url, body):
        '''
        Perform a POST request.
        '''

        request = HTTPRequest(url, method="POST", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def put(self, url, body):
        '''
        Perform a PUT request.
        '''

        request = HTTPRequest(url, method="PUT", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def delete(self, url):
        '''
        Perform a DELETE request.
        '''

        request = HTTPRequest(url, method="DELETE")
        if self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def fetch_document_from_url(self, url):
        '''
        Retrieve newebe document from a givent url
        '''

        response = self.get(url)

        assert response.code == 200<commit_msg>simplify put requests<commit_after>import hashlib

from lettuce import world
from tornado.escape import json_decode
from tornado.httpclient import HTTPClient, HTTPRequest

from newebe.settings import TORNADO_PORT
from newebe.profile.models import UserManager, User

ROOT_URL = "http://localhost:%d/" % TORNADO_PORT

class NewebeClient(HTTPClient):
    '''
    Tornado client wrapper to write requests to Newebe faster.
    '''


    def login(self, password):
        '''
        Grab authentication cookie from login request.
        '''
        response = self.fetch(self.root_url + "login/json/", 
                method="POST", body='{"password":"%s"}' % password)
        
        assert response.headers["Set-Cookie"].startswith("password=")
        self.cookie = response.headers["Set-Cookie"]


    def set_default_user(self, url=ROOT_URL):
        '''
        Set to DB default user. This is useful for automatic login.
        '''
        self.root_url = url

        user = UserManager.getUser()
        if user:
            user.delete()
        
        user = User(
            name = "<NAME>",
            password = <PASSWORD>.<PASSWORD>4("<PASSWORD>(),
            key = None,
            authorKey = "authorKey",
            url = url,
            description = "my description"
        )
        user.save()


    def get(self, url):
        '''
        Perform a GET request.
        '''
        request = HTTPRequest(url)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def post(self, url, body):
        '''
        Perform a POST request.
        '''

        request = HTTPRequest(url, method="POST", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def put(self, url, body):
        '''
        Perform a PUT request.
        '''

        if hasattr(self, "root_url") and self.root_url:
            url = self.root_url + url

        request = HTTPRequest(url, method="PUT", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
            
        return HTTPClient.fetch(self, request)


    def delete(self, url):
        '''
        Perform a DELETE request.
        '''

        request = HTTPRequest(url, method="DELETE")
        if self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def fetch_document_from_url(self, url):
        '''
        Retrieve newebe document from a givent url
        '''

        response = self.get(url)

        assert response.code == 200
{'fraction_non_alphanumeric': 0.11455625696249536, 'fraction_numerical': 0.0012996658002228, 'mean_word_length': 2.0400677200902932, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3344220', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1457, 'n_words': 387}