<commit_before># Copyright (C) 2014 cybojenix <<EMAIL>>
#
# This file is part of XdaPy.
#
# XdaPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# XdaPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XdaPy.  If not, see <http://www.gnu.org/licenses/>.

from .base import XdaBase
from .decorators import requires_login
from . import serialize

from getpass import getpass
import sys


class User(XdaBase):

    @requires_login
    def user(self):
        method = "GET"
        url = "/v1/user"
        r = self.xda.requests.make_request(method, url)
        return serialize.str_to_dict(r.read())

    @requires_login
    def mentions(self, page=1):
        method = "GET"
        url = "/v1/user/mentions"
        d = {"page": page}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def quotes(self, page=1):
        method = "GET"
        url = "/v1/user/quotes"
        d = {"page": page}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    def userinfo(self, user_id=None):
        if user_id is None:
            return self.user()
        method = "GET"
        url = "/v1/user/userinfo"
        d = {"userid": user_id}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    def login(self, username, password):
        method = "POST"
        url = "/v1/user/login"
        d = {"username": username,
             "password": password}
        r = self.xda.requests.make_request(method, url, body=d)
        assert r.status == 200
        cookies = self.xda.requests.get_cookies(r)
        self.xda.session.set_session(username, cookies)

    @requires_login
    def logout(self):
        method = "GET"
        url = "/v1/user/logout"
        r = self.xda.requests.make_request(method, url)
        assert r.status == 200
        self.xda.session.remove_session()

    def default_login(self):
        """Call for the `requires_login` decorator

        override this to use custom methods for getting user input
        """
        sys.stdout.write("What is your username?\n")
        u = sys.stdin.readline().strip()
        p = getpass("What is your password: ")
        self.login(u, p)<commit_msg>add the POST, PUT and DELETE requests<commit_after># Copyright (C) 2014 cybojenix <<EMAIL>>
#
# This file is part of XdaPy.
#
# XdaPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# XdaPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XdaPy.  If not, see <http://www.gnu.org/licenses/>.

from .base import XdaBase
from .decorators import requires_login
from . import serialize

from getpass import getpass
import sys


class User(XdaBase):

    @requires_login
    def user(self):
        method = "GET"
        url = "/v1/user"
        r = self.xda.requests.make_request(method, url)
        return serialize.str_to_dict(r.read())

    @requires_login
    def logout(self):
        method = "GET"
        url = "/v1/user/logout"
        r = self.xda.requests.make_request(method, url)
        assert r.status == 200
        self.xda.session.remove_session()

    @requires_login
    def mentions(self, page=1):
        method = "GET"
        url = "/v1/user/mentions"
        d = {"page": page}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def quotes(self, page=1):
        method = "GET"
        url = "/v1/user/quotes"
        d = {"page": page}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    def userinfo(self, user_id=None):
        if user_id is None:
            return self.user()
        method = "GET"
        url = "/v1/user/userinfo"
        d = {"userid": user_id}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def addmydevice(self, device_id):
        method = "POST"
        url = "/v1/user/addmydevice"
        d = {"deviceid": device_id}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    def login(self, username, password):
        method = "POST"
        url = "/v1/user/login"
        d = {"username": username,
             "password": password}
        r = self.xda.requests.make_request(method, url, body=d)
        assert r.status == 200
        cookies = self.xda.requests.get_cookies(r)
        self.xda.session.set_session(username, cookies)

    def register(self, username, password, email,
                 captcha_chal, captcha_resp):
        method = "POST"
        url = "/v1/user/register"
        d = {"username": username,
             "password": password,
             "email": email,
             "captcha_chal": captcha_chal,
             "captcha_resp": captcha_resp}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def updateemail(self, email):
        method = "PUT"
        url = "/v1/user/updateemail"
        d = {"email": email}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def updatepassword(self, cur_password, new_password):
        method = "PUT"
        url = "/v1/user/updatepassword"
        d = {"current_password": <PASSWORD>,
             "newpassword": <PASSWORD>,
             "newpasswordconfirm": <PASSWORD>}
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def delete_mentions(self):
        method = "DELETE"
        url = "/v1/user/notifications/mentions"
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())

    @requires_login
    def delete_quotes(self):
        method = "DELETE"
        url = "/v1/user/notifications/quotes"
        r = self.xda.requests.make_request(method, url, body=d)
        return serialize.str_to_dict(r.read())


    def default_login(self):
        """Call for the `requires_login` decorator

        override this to use custom methods for getting user input
        """
        sys.stdout.write("What is your username?\n")
        u = sys.stdin.readline().strip()
        p = getpass("What is your password: ")
        self.login(u, p)
{'fraction_non_alphanumeric': 0.11345503718728871, 'fraction_numerical': 0.005949966193373901, 'mean_word_length': 2.4903256252949504, 'pattern_counts': {'":': 20, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6559351', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2277, 'n_words': 641}