from ..api import APIClient
from .resource import Model, Collection


class Secret(Model):
    """A secret."""
    id_attribute = 'ID'

    def __repr__(self):
        return "<%s: '%s'>" % (self.__class__.__name__, self.name)

    @property
    def name(self):
        return self.attrs['Spec']['Name']

    def remove(self):
        """
        Remove this secret.

        Raises:
            :py:class:`docker.errors.APIError`
                If secret failed to remove.
        """
        return self.client.api.remove_secret(self.id)


class SecretCollection(Collection):
    """Secrets on the Docker server."""
    model = Secret

    def create(self, **kwargs):
        obj = self.client.api.create_secret(**kwargs)
        return self.prepare_model(obj)
    create.__doc__ = APIClient.create_secret.__doc__

    def get(self, secret_id):
        """
        Get a secret.

        Args:
            secret_id (str): Secret ID.

        Returns:
            (:py:class:`Secret`): The secret.

        Raises:
            :py:class:`docker.errors.NotFound`
                If the secret does not exist.
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        """
        return self.prepare_model(self.client.api.inspect_secret(secret_id))

    def list(self, **kwargs):
        """
        List secrets. Similar to the ``docker secret ls`` command.

        Args:
            filters (dict): Server-side list filtering options.

        Returns:
            (list of :py:class:`Secret`): The secrets.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        """
        resp = self.client.api.secrets(**kwargs)
        return [self.prepare_model(obj) for obj in resp]

docker/models/secrets.py
from ..api import APIClient
from .resource import Model, Collection


class Secret(Model):
    """A secret."""
    id_attribute = 'ID'

    def __repr__(self):
        return "<%s: '%s'>" % (self.__class__.__name__, self.name)

    @property
    def name(self):
        return self.attrs['Spec']['Name']

    def remove(self):
        """
        Remove this secret.

        Raises:
            :py:class:`docker.errors.APIError`
                If secret failed to remove.
        """
        return self.client.api.remove_secret(self.id)


class SecretCollection(Collection):
    """Secrets on the Docker server."""
    model = Secret

    def create(self, **kwargs):
        obj = self.client.api.create_secret(**kwargs)
        obj.setdefault("Spec", {})["Name"] = kwargs.get("name")
        return self.prepare_model(obj)
    create.__doc__ = APIClient.create_secret.__doc__

    def get(self, secret_id):
        """
        Get a secret.

        Args:
            secret_id (str): Secret ID.

        Returns:
            (:py:class:`Secret`): The secret.

        Raises:
            :py:class:`docker.errors.NotFound`
                If the secret does not exist.
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        """
        return self.prepare_model(self.client.api.inspect_secret(secret_id))

    def list(self, **kwargs):
        """
        List secrets. Similar to the ``docker secret ls`` command.

        Args:
            filters (dict): Server-side list filtering options.

        Returns:
            (list of :py:class:`Secret`): The secrets.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        """
        resp = self.client.api.secrets(**kwargs)
        return [self.prepare_model(obj) for obj in resp]

docker/models/secrets.py
diff --git a/docker/models/secrets.py b/docker/models/secrets.py
index ca11edeb0..e2ee88af0 100644
--- a/docker/models/secrets.py
+++ b/docker/models/secrets.py
@@ -30,6 +30,7 @@ class SecretCollection(Collection):
 
     def create(self, **kwargs):
         obj = self.client.api.create_secret(**kwargs)
+        obj.setdefault("Spec", {})["Name"] = kwargs.get("name")
         return self.prepare_model(obj)
     create.__doc__ = APIClient.create_secret.__doc__
 
diff --git a/tests/unit/fake_api.py b/tests/unit/fake_api.py
index 27e463d27..4fd4d1138 100644
--- a/tests/unit/fake_api.py
+++ b/tests/unit/fake_api.py
@@ -17,6 +17,8 @@
 FAKE_PATH = '/path'
 FAKE_VOLUME_NAME = 'perfectcherryblossom'
 FAKE_NODE_ID = '24ifsmvkjbyhk'
+FAKE_SECRET_ID = 'epdyrw4tsi03xy3deu8g8ly6o'
+FAKE_SECRET_NAME = 'super_secret'
 
 # Each method is prefixed with HTTP method (get, post...)
 # for clarity and readability
@@ -512,6 +514,12 @@ def post_fake_network_disconnect():
     return 200, None
 
 
+def post_fake_secret():
+    status_code = 200
+    response = {'ID': FAKE_SECRET_ID}
+    return status_code, response
+
+
 # Maps real api url to fake response callback
 prefix = 'http+docker://localhost'
 if constants.IS_WINDOWS_PLATFORM:
@@ -643,4 +651,6 @@ def post_fake_network_disconnect():
         CURRENT_VERSION, prefix, FAKE_NETWORK_ID
     ), 'POST'):
     post_fake_network_disconnect,
+    '{1}/{0}/secrets/create'.format(CURRENT_VERSION, prefix):
+    post_fake_secret,
 }
diff --git a/tests/unit/fake_api_client.py b/tests/unit/fake_api_client.py
index e85001dbb..5825b6ec0 100644
--- a/tests/unit/fake_api_client.py
+++ b/tests/unit/fake_api_client.py
@@ -40,6 +40,7 @@ def make_fake_api_client(overrides=None):
             fake_api.post_fake_create_container()[1],
         'create_host_config.side_effect': api_client.create_host_config,
         'create_network.return_value': fake_api.post_fake_network()[1],
+        'create_secret.return_value': fake_api.post_fake_secret()[1],
         'exec_create.return_value': fake_api.post_fake_exec_create()[1],
         'exec_start.return_value': fake_api.post_fake_exec_start()[1],
         'images.return_value': fake_api.get_fake_images()[1],
diff --git a/tests/unit/models_secrets_test.py b/tests/unit/models_secrets_test.py
new file mode 100644
index 000000000..4ccf4c638
--- /dev/null
+++ b/tests/unit/models_secrets_test.py
@@ -0,0 +1,11 @@
+import unittest
+
+from .fake_api_client import make_fake_client
+from .fake_api import FAKE_SECRET_NAME
+
+
+class CreateServiceTest(unittest.TestCase):
+    def test_secrets_repr(self):
+        client = make_fake_client()
+        secret = client.secrets.create(name="super_secret", data="secret")
+        assert secret.__repr__() == "<Secret: '{}'>".format(FAKE_SECRET_NAME)

Couldn't create secret object
I couldn't create secret object, the problem seemed to boil down to the way that a secret was being created from the docker daemon response. 

https://github.com/docker/docker-py/blob/467cacb00d8dce68aa8ff2bdacc85acecd2d1207/docker/models/secrets.py#L31-L33

Docker version 18.03.1-ce and python version 3.5 had the following error:

````
File "docker/models/secrets.py", line 10 in __repr__
return "<%s: %s'>" % (self.__class__.__name__, self.name)
File "docker/models/secrets.py", line 14 in name
return self.attrs['Spec']['Name']
KeyError: 'Spec'

````

When calling: 

````
import docker

client -docker.from_env()
mySecret = client.secrets.create(name='randomName', data='platform_node_requirements.md')

````

Changing the code to the following seemed to fix it. 
````
obj = self.client.api.create_secret(**kwargs)
secret = self.client.secrets.get(obj.get('ID'))
return self.prepare_model(secret)
````



