<commit_before># -*- coding: utf-8 -*-

"""
gmncurses.api.client
~~~~~~~~~~~~~~~~~~~~
"""

from urllib.parse import urljoin
import json

import requests


class BaseClient(object):
    """ The base class for an API client. """

    BASE_HEADERS = {
        "content-type": "application/json; charset: utf8",
        "X-DISABLE-PAGINATION": "true",
    }

    def __init__(self, host):
        self._host = host
        self._headers = self.BASE_HEADERS
        self.last_error = {}

    def _get(self, url, params, **kwargs):
        response = requests.get(url, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _post(self, url, data_dict, params, **kwargs):
        rdata = json.dumps(data_dict)

        response = requests.post(url, data=rdata, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _patch(self, url, data_dict, params, **kwargs):
        rdata = json.dumps(data_dict)

        response = requests.patch(url, data=rdata, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _delete(self, url, params, **kwargs):
        response = requests.delete(url, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 204:
            return True

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None


class GreenMineClient(BaseClient):
    """ A Greenmine Api Client.

    >>> from gmncurses.api.client import *
    >>> api = GreenMineClient("http://localhost:8000")
    >>> api.login("admin", "123123")
    {...}
    >>> api.get_projects()
    [...]
    >>> api.get_project(1)
    {...}
    >>> api.get_user_stories(params={'project': 1})
    [...]
    >>> api.get_issue(1234)
    None
    >>> api.last_error
    {'detail': 'Not found', 'status_code': 404}

    """

    URLS = {
        "auth": "/api/v1/auth",
        "users": "/api/v1/users",
        "user":  "/api/v1/users/{}",
        "projects": "/api/v1/projects",
        "project":  "/api/v1/projects/{}",
        "project-stats": "/api/v1/projects/{}/stats",
        "project-issues-stats": "/api/v1/projects/{}/issues_stats",
        "milestones": "/api/v1/milestones",
        "milestone":  "/api/v1/milestones/{}",
        "user_stories": "/api/v1/userstories",
        "user_story":  "/api/v1/userstories/{}",
        "tasks": "/api/v1/tasks",
        "task":  "/api/v1/tasks/{}",
        "issues": "/api/v1/issues",
        "issue":  "/api/v1/issues/{}",
        "wiki_pages": "/api/v1/wiki_pages",
        "wiki_page":  "/api/v1/wiki_pages/{}",
    }

    # AUTHENTICATIONS<commit_msg>Fixed wiki urls<commit_after># -*- coding: utf-8 -*-

"""
gmncurses.api.client
~~~~~~~~~~~~~~~~~~~~
"""

from urllib.parse import urljoin
import json

import requests


class BaseClient(object):
    """ The base class for an API client. """

    BASE_HEADERS = {
        "content-type": "application/json; charset: utf8",
        "X-DISABLE-PAGINATION": "true",
    }

    def __init__(self, host):
        self._host = host
        self._headers = self.BASE_HEADERS
        self.last_error = {}

    def _get(self, url, params, **kwargs):
        response = requests.get(url, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _post(self, url, data_dict, params, **kwargs):
        rdata = json.dumps(data_dict)

        response = requests.post(url, data=rdata, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _patch(self, url, data_dict, params, **kwargs):
        rdata = json.dumps(data_dict)

        response = requests.patch(url, data=rdata, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 200:
            return data

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None

    def _delete(self, url, params, **kwargs):
        response = requests.delete(url, params=params, headers=self._headers)
        data = json.loads(response.content.decode())

        if response.status_code == 204:
            return True

        self.last_error = {
            "status_code": response.status_code,
            "detail": data.get("detail", "")
        }
        return None


class GreenMineClient(BaseClient):
    """ A Greenmine Api Client.

    >>> from gmncurses.api.client import *
    >>> api = GreenMineClient("http://localhost:8000")
    >>> api.login("admin", "123123")
    {...}
    >>> api.get_projects()
    [...]
    >>> api.get_project(1)
    {...}
    >>> api.get_user_stories(params={'project': 1})
    [...]
    >>> api.get_issue(1234)
    None
    >>> api.last_error
    {'detail': 'Not found', 'status_code': 404}

    """

    URLS = {
        "auth": "/api/v1/auth",
        "users": "/api/v1/users",
        "user":  "/api/v1/users/{}",
        "projects": "/api/v1/projects",
        "project":  "/api/v1/projects/{}",
        "project-stats": "/api/v1/projects/{}/stats",
        "project-issues-stats": "/api/v1/projects/{}/issues_stats",
        "milestones": "/api/v1/milestones",
        "milestone":  "/api/v1/milestones/{}",
        "user_stories": "/api/v1/userstories",
        "user_story":  "/api/v1/userstories/{}",
        "tasks": "/api/v1/tasks",
        "task":  "/api/v1/tasks/{}",
        "issues": "/api/v1/issues",
        "issue":  "/api/v1/issues/{}",
        "wiki_pages": "/api/v1/wiki",
        "wiki_page":  "/api/v1/wiki/{}",
    }

    # AUTHENTICATIONS
{'fraction_non_alphanumeric': 0.1715786402100963, 'fraction_numerical': 0.014590020426028597, 'mean_word_length': 2.189855746859004, 'pattern_counts': {'":': 54, '<': 3, '<?xml version=': 0, '>': 51, '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': '7451195', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2155, 'n_words': 395}