<commit_before>import requests
from flask import json, current_app
from ..model import User


class ApiClient:
    root_url = None
    token = None

    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.root_url = app.config['DM_API_URL']
        self.token = app.config['DM_API_AUTH_TOKEN']

    def headers(self):
        return {
            "content-type": "application/json",
            "Authorization": "Bearer {}".format(self.token)
        }

    def services_by_supplier_id(self, supplier_id):
        res = requests.get(
            "{}/{}?supplier_id={}".format(
                self.root_url,
                "services",
                supplier_id),
            headers=self.headers()
        )
        if res.status_code is 200:
            return res.json()
        elif res.status_code is 400:
            # TODO log bad request
            return None
        else:
            # TODO log error
            return None

    def user_by_id(self, user_id):
        res = requests.get(
            "{}/{}/{}".format(self.root_url, "users", user_id),
            headers=self.headers()
        )
        if res.status_code is 200:
            return self.user_json_to_user(res.json())
        elif res.status_code is 400:
            # TODO log bad request
            return None
        else:
            # TODO log error
            return None

    def user_by_email(self, email_address):
        res = requests.get(
            "{}/{}".format(self.root_url, "users"),
            params={"email": email_address},
            headers=self.headers()
        )
        if res.status_code is 200:
            return self.user_json_to_user(res.json())
        elif res.status_code is 400:
            # TODO log bad request
            return None
        else:
            # TODO log error
            return None

    def users_auth(self, email_address, password):
        res = requests.post(
            "{}/{}".format(self.root_url, "users/auth"),
            data=json.dumps(
                {
                    "authUsers": {
                        "emailAddress": email_address,
                        "password": password
                    }
                }
            ),
            headers=self.headers()
        )
        if res.status_code is 200:
            if self.is_supplier_user(res.json()):
                return self.user_json_to_user(res.json())
            return None
        elif res.status_code is 400:
            # TODO log bad request
            return None
        elif res.status_code is 403:
            # TODO log unauthorized
            return None
        elif res.status_code is 404:
            # TODO log not found
            return None
        else:
            # TODO log error
            return None

    def user_update_password(self, user_id, new_password):
        res = requests.post(
            "{}/{}/{}".format(self.root_url, "users", user_id),
            data=json.dumps(
                {
                    "users": {
                        "password": <PASSWORD>
                    }
                }
            ),
            headers=self.headers()
        )
        if res.status_code is 200:
            current_app.logger.info("Updated password for user %d", user_id)
            return True
        else:
            current_app.logger.info("Password update failed for user %d: %s",
                                    user_id, res.status_code)
            return False

    @staticmethod
    def user_json_to_user(user_json):
        user = user_json["users"]
        supplier_id = None
        supplier_name = None
        if "supplier" in user:
            supplier_id = user["supplier"]["supplierId"]
            supplier_name = user["supplier"]["name"]
        return User(
            user_id=user["id"],
            email_address=user['emailAddress'],
            supplier_id=supplier_id,
            supplier_name=supplier_name
        )

    @staticmethod
    def is_supplier_user(user_json):
        if "supplier" in user_json["users"]:
            return True
        return False<commit_msg>Add logging to API client<commit_after>import requests
from flask import json, current_app
from ..model import User


class ApiClient:
    root_url = None
    token = None

    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.root_url = app.config['DM_API_URL']
        self.token = app.config['DM_API_AUTH_TOKEN']

    def headers(self):
        return {
            "content-type": "application/json",
            "Authorization": "Bearer {}".format(self.token)
        }

    def services_by_supplier_id(self, supplier_id):
        res = requests.get(
            "{}/{}?supplier_id={}".format(
                self.root_url,
                "services",
                supplier_id),
            headers=self.headers()
        )
        if res.status_code == 200:
            return res.json()
        else:
            current_app.logger.error("Error getting services for %s: %s - %s",
                                    supplier_id, res.status_code,
                                    res.json()["error"])
            return {"services":{}}

    def user_by_id(self, user_id):
        res = requests.get(
            "{}/{}/{}".format(self.root_url, "users", user_id),
            headers=self.headers()
        )
        if res.status_code == 200:
            return self.user_json_to_user(res.json())
        else:
            current_app.logger.error("Error getting user by id %s: %s - %s",
                                      user_id, res.status_code,
                                      res.json()["error"])
        return None

    def user_by_email(self, email_address):
        res = requests.get(
            "{}/{}".format(self.root_url, "users"),
            params={"email": email_address},
            headers=self.headers()
        )
        if res.status_code == 200:
            return self.user_json_to_user(res.json())
        elif res.status_code != 404:
            current_app.logger.error("Error getting user by email %s: %s - %s",
                                     email_address, res.status_code,
                                     res.json()["error"])
        return None

    def users_auth(self, email_address, password):
        res = requests.post(
            "{}/{}".format(self.root_url, "users/auth"),
            data=json.dumps(
                {
                    "authUsers": {
                        "emailAddress": email_address,
                        "password": password
                    }
                }
            ),
            headers=self.headers()
        )
        code = res.status_code
        if code == 200:
            # Only log in supplier users to this app
            if self.is_supplier_user(res.json()):
                return self.user_json_to_user(res.json())
            else:
                current_app.logger.info("Login by non-supplier user %s",
                                        email_address)
        elif code == 403:
            current_app.logger.info("Login failure for email %s: %s",
                                    email_address, res.status_code)
        elif code == 404:
            current_app.logger.info("Login not found for email %s: %s",
                                    email_address, res.status_code)
        else:
            current_app.logger.error("Error logging in email %s: %s",
                                     email_address, res.status_code)
        return None

    def user_update_password(self, user_id, new_password):
        res = requests.post(
            "{}/{}/{}".format(self.root_url, "users", user_id),
            data=json.dumps(
                {
                    "users": {
                        "password": <PASSWORD>
                    }
                }
            ),
            headers=self.headers()
        )
        if res.status_code == 200:
            current_app.logger.info("Updated password for user %s", user_id)
            return True
        else:
            current_app.logger.info("Password update failed for user %s: %s",
                                    user_id, res.status_code)
            return False

    @staticmethod
    def user_json_to_user(user_json):
        user = user_json["users"]
        supplier_id = None
        supplier_name = None
        if "supplier" in user:
            supplier_id = user["supplier"]["supplierId"]
            supplier_name = user["supplier"]["name"]
        return User(
            user_id=user["id"],
            email_address=user['emailAddress'],
            supplier_id=supplier_id,
            supplier_name=supplier_name
        )

    @staticmethod
    def is_supplier_user(user_json):
        if "supplier" in user_json["users"]:
            return True
        return False
{'fraction_non_alphanumeric': 0.10201818585052118, 'fraction_numerical': 0.006320691949434464, 'mean_word_length': 1.4121422840331639, 'pattern_counts': {'":': 17, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5880278', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2423, 'n_words': 547}