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

import logging
import os

import pkg_resources
import requests
from requests.exceptions import RequestException

from django.utils.translation import ugettext as _

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:
            settings.MODOBOA_API_URL if api_url is None
            else api_url
        )

    def __send_request(self, url, params=None):
        """Send a request to the API."""
            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
        return resp.json()

    def current_version(self, site):
        """Check the latest version."""
        client_version = pkg_resources.get_distribution("modoboa").version
        url = os.path.join(self._api_url, "current_version/")
        params = {"client_version": client_version, "site": site}
        resp = self.__send_request(url, params)
        if resp is None:
            return None
        version = Version(resp["version"], DEFAULT_NUMBER_BITS)
        if version <= Version(client_version, DEFAULT_NUMBER_BITS):
            return None
        return resp

    def list_extensions(self):
        """List all official extensions."""
                    _("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> Merge extensions and information pages.

Check updates for extensions too.

<DFF> @@ -22,6 +22,9 @@ class ModoAPIClient(object):
             settings.MODOBOA_API_URL if api_url is None
             else api_url
         )
+        self._local_core_version = None
+        self._latest_core_version = None
+        self.changelog_url = None
 
     def __send_request(self, url, params=None):
         """Send a request to the API."""
@@ -35,18 +38,34 @@ class ModoAPIClient(object):
             return None
         return resp.json()
 
-    def current_version(self, site):
+    @property
+    def local_core_version(self):
+        """Return the version installed locally."""
+        if self._local_core_version is None:
+            self._local_core_version = pkg_resources.get_distribution(
+                "modoboa").version
+        return self._local_core_version
+
+    @property
+    def latest_core_version(self):
+        """Return the latest core version."""
+        if self._latest_core_version is None:
+            return self._local_core_version
+        return self._latest_core_version
+
+    def new_core_version(self, site):
         """Check the latest version."""
-        client_version = pkg_resources.get_distribution("modoboa").version
         url = os.path.join(self._api_url, "current_version/")
-        params = {"client_version": client_version, "site": site}
+        params = {"client_version": self.local_core_version, "site": site}
         resp = self.__send_request(url, params)
         if resp is None:
             return None
         version = Version(resp["version"], DEFAULT_NUMBER_BITS)
-        if version <= Version(client_version, DEFAULT_NUMBER_BITS):
-            return None
-        return resp
+        if version <= Version(self.local_core_version, DEFAULT_NUMBER_BITS):
+            return False
+        self._latest_core_version = resp["version"]
+        self.changelog_url = resp["changelog_url"]
+        return True
 
     def list_extensions(self):
         """List all official extensions."""

Merge extensions and information pages.