<NME> api_client.py
<BEF> """A client for Modoboa's public API."""

import logging
import os

import requests
from requests.exceptions import RequestException

from versionfield.constants import DEFAULT_NUMBER_BITS
from versionfield.version import Version


class ModoAPIClient(object):

logger = logging.getLogger("modoboa.admin")


class ModoAPIClient(object):

    """A simple client for the public API."""

    def __init__(self, api_url=None):
        """Constructor."""
        if api_url is None:
            from django.conf import settings
            self._api_url = settings.MODOBOA_API_URL
        else:
            self._api_url = api_url
        self._local_core_version = None

    def __send_request(self, url, params=None):
        """Send a request to the API."""
        if params is None:
            params = {}
        try:
            resp = requests.get(url, params=params)
        except RequestException as err:
            logger.critical(
                _("Failed to communicate with public API: %s"), str(err)
            )
            return None
        if resp.status_code != 200:
            return None
        return resp.json()

    @property
    def local_core_version(self):
        """Return the version installed locally."""
        if self._local_core_version is None:
            try:
                self._local_core_version = pkg_resources.get_distribution(
                    "modoboa").version
            except pkg_resources.DistributionNotFound:

    def new_core_version(self, site):
        """Check the latest version."""
        url = os.path.join(self._api_url, "current_version/")
        params = {"client_version": self.local_core_version, "site": site}
        resp = self.__send_request(url, params)
        """Register this instance."""
        url = "{}instances/search/?hostname={}".format(
            self._api_url, hostname)
        instance = self.__send_request(url)
        if instance is None:
            url = "{}instances/".format(self._api_url)
            data = {
                "hostname": hostname, "known_version": self.local_core_version}
            try:
                response = requests.post(url, data=data)
            except RequestException as err:
                logger.critical(
                    _("Failed to communicate with public API: %s"), str(err)
                )
                return None
            if response.status_code != 201:
                return None
            instance = response.json()
        return int(instance["pk"])

    def update_instance(self, pk, data):
        """Update instance and send stats."""
        url = "{}instances/{}/".format(self._api_url, pk)
        try:
            response = requests.put(url, data=data)
        except RequestException as err:
            logger.critical(
                _("Failed to communicate with public API: %s"), str(err)
            )
            return
        if response.status_code != 200:
            logger.critical(
                _("Failed to communicate with public API: %s"), response.text
            )

    def versions(self):
        """Fetch core and extension versions."""
        url = "{}versions/".format(self._api_url)
        return self.__send_request(url)

<MSG> Same here.

<DFF> @@ -6,9 +6,6 @@ import pkg_resources
 import requests
 from requests.exceptions import RequestException
 
-from versionfield.constants import DEFAULT_NUMBER_BITS
-from versionfield.version import Version
-
 
 class ModoAPIClient(object):
 
@@ -54,6 +51,9 @@ class ModoAPIClient(object):
 
     def new_core_version(self, site):
         """Check the latest version."""
+        from versionfield.constants import DEFAULT_NUMBER_BITS
+        from versionfield.version import Version
+
         url = os.path.join(self._api_url, "current_version/")
         params = {"client_version": self.local_core_version, "site": site}
         resp = self.__send_request(url, params)

Same here.