from .rest import RestClient


class Connection(object):
    """Auth0 connection endpoints

    Args:
        domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
        jwt_token (str): An API token created with your account's global
            keys. You can create one by using the token generator in the
            API Explorer: https://auth0.com/docs/api/v2
    """

    def __init__(self, domain, jwt_token):
        url = 'https://%s/api/v2/connections' % domain

        self.client = RestClient(endpoint=url, jwt=jwt_token)

    def all(self, strategy=None, fields=[], include_fields=True):
        """Retrieves all connections.

        Args:
           strategy (str, optional): Only retrieve connections of
              this strategy type. (e.g: strategy='amazon')

           fields (list of str, optional): A list of fields to include or
              exclude from the result (depending on include_fields). Empty to
              retrieve all fields.

           include_fields (bool, optional): True if the fields specified are
              to be include in the result, False otherwise.

        Returns:
           A list of connection objects.
        """

        params = {'strategy': strategy or None,
                  'fields': ','.join(fields) or None,
                  'include_fields': str(include_fields).lower()}

        return self.client.get(params=params)

    def get(self, id, fields=[], include_fields=True):
        """Retrieve connection by id.

        Args:
           id (str): Id of the connection to get.

           fields (list of str, optional): A list of fields to include or
              exclude from the result (depending on include_fields). Empty to
              retrieve all fields.

           include_fields (bool, optional): True if the fields specified are
              to be include in the result, False otherwise.

        Returns:
            A connection object.
        """

        params = {'fields': ','.join(fields) or None,
                  'include_fields': str(include_fields).lower()}

        return self.client.get(params=params, id=id)

    def delete(self, id):
        """Deletes a connection and all its users.

        Args:
           id: Id of the connection to delete.

        Returns:
           An empty dict.
        """

        return self.client.delete(id=id)

    def update(self, id, body):
        """Modifies a connection.

        Args:
           id: Id of the connection.

           body (dict): Specifies which fields are to be modified, and to what
              values.

        Returns:
           The modified connection object.
        """

        return self.client.patch(id=id, data=body)

    def create(self, body):
        """Creates a new connection.

        Args:
            body (dict): Attributes used to create the connection. Mandatory
                attributes are: 'name' and 'strategy'.
        """

        return self.client.post(data=body)

@@ -416,16 +416,123 @@
 token):%0A
+        self.domain = domain%0A        self.client = RestClient(jwt=jwt_token)%0A%0A    def _url(self, id=None):%0A
         
@@ -575,24 +575,28 @@
 ' %25 
+self.
 domain%0A
-%0A
         
 self
@@ -595,61 +595,79 @@
     
-self.client = RestClient(endpoint=url, jwt=jwt_token)
+if id is not None:%0A            return url + '/' + id%0A        return url
 %0A%0A  
@@ -1497,32 +1497,45 @@
 self.client.get(
+self._url(), 
 params=params)%0A%0A
@@ -2233,16 +2233,31 @@
 ent.get(
+self._url(id), 
 params=p
@@ -2265,15 +2265,8 @@
 rams
-, id=id
 )%0A%0A 
@@ -2498,14 +2498,22 @@
 ete(
-id=
+self._url(
 id)
+)
 %0A%0A  
@@ -2841,13 +2841,21 @@
 tch(
-id=
+self._url(
 id
+)
 , da
@@ -3121,16 +3121,29 @@
 nt.post(
+self._url(), 
 data=bod

Refactor Connection for new RestClient api