/* Copyright (C) 2018 Canonical Ltd. */
'use strict';

var module = module;

(function(exports) {

  const jujulib = exports.jujulib;

  /**
    Identity service client.

    Provides access to the Identity API.
  */

  const identityAPIVersion = 'v1';

  /**
    Initializer.

    @function identity
    @param url {String} The URL of the identity instance, including
      scheme and port, and excluding the API version.
    @param bakery {Object} A bakery object for communicating with the identity
      instance.
    @returns {Object} A client object for making identity API calls.
  */
  function identity(url, bakery) {
    // Store the API URL (including version) handling missing trailing slash.
    this.url = url.replace(/\/?$/, '/') + identityAPIVersion;
    this.bakery = bakery;
  };

  identity.prototype = {

    /**
      Fetch the user data from the identity service
      @param {String} userName The user name to fetch the data for.
      @param {Function} callback
        Called with the call signature (error <Object>, userData <Object>).
    */
    getUser: function(userName, callback) {
      return this.bakery.get(`${this.url}/u/${userName}`, null, (err, resp) => {
        if (err) {
          callback(err, null);
          return;
        }
        try {
          const data = JSON.parse(resp.target.responseText);
          callback(null, data);
        } catch (e) {
          callback(e.toString(), null);
        }
      });
    }
  };

  jujulib.identity = identity;

}((module && module.exports) ? module.exports : this));

@@ -310,116 +310,75 @@
 am u
-rl %7BString%7D The URL of the identity instance, including%0A      scheme and port, and excluding the API version
+serStorage %7BObject%7D An object containing access to the identity url
 .%0A  
@@ -565,18 +565,26 @@
 entity(u
-rl
+serStorage
 , bakery
@@ -679,58 +679,32 @@
 is.u
-rl = url.replace(/%5C/?$/, '/') + identityAPIVersion
+serStorage = userStorage
 ;%0A  
@@ -759,16 +759,231 @@
 pe = %7B%0A%0A
+    getIdentityURL: function() %7B%0A      const identityURL = this.userStorage.identityURL();%0A      if (identityURL === null) %7B%0A        return null;%0A      %7D%0A      return %60$%7BidentityURL%7D/$%7BidentityAPIVersion%7D%60;%0A    %7D,%0A%0A
     /**%0A
@@ -1258,24 +1258,189 @@
 callback) %7B%0A
+      const identityURL = this.getIdentityURL();%0A      if (identityURL === null) %7B%0A        callback('no identity url available', null);%0A        return null;%0A      %7D%0A
       return
@@ -1463,16 +1463,19 @@
 (%60$%7B
-this.url
+identityURL
 %7D/u/

Update the identity jujulib api to pull the identityURL from the user instance.