Exemplo n.º 1
0
class GenericRestClient(object):
    def __init__(self, credentials, subscription_id, base_url=None):
        self.config = GenericRestClientConfiguration(credentials,
                                                     subscription_id, base_url)
        self._client = ServiceClient(self.config.credentials, self.config)
        self.models = None

    def query(self, url, method, query_parameters, header_parameters, body,
              expected_status_codes, polling_timeout, polling_interval):
        # Construct and send request
        operation_config = {}

        request = None

        if header_parameters is None:
            header_parameters = {}

        header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())

        if method == 'GET':
            request = self._client.get(url, query_parameters)
        elif method == 'PUT':
            request = self._client.put(url, query_parameters)
        elif method == 'POST':
            request = self._client.post(url, query_parameters)
        elif method == 'HEAD':
            request = self._client.head(url, query_parameters)
        elif method == 'PATCH':
            request = self._client.patch(url, query_parameters)
        elif method == 'DELETE':
            request = self._client.delete(url, query_parameters)
        elif method == 'MERGE':
            request = self._client.merge(url, query_parameters)

        response = self._client.send(request, header_parameters, body,
                                     **operation_config)

        if response.status_code not in expected_status_codes:
            exp = CloudError(response)
            exp.request_id = response.headers.get('x-ms-request-id')
            raise exp
        elif response.status_code == 202 and polling_timeout > 0:

            def get_long_running_output(response):
                return response

            poller = LROPoller(
                self._client, ClientRawResponse(None, response),
                get_long_running_output,
                ARMPolling(polling_interval, **operation_config))
            response = self.get_poller_result(poller, polling_timeout)

        return response

    def get_poller_result(self, poller, timeout):
        try:
            poller.wait(timeout=timeout)
            return poller.result()
        except Exception as exc:
            raise
class GenericRestClient(object):
    def __init__(self, credentials, subscription_id, base_url=None):
        self.config = GenericRestClientConfiguration(credentials,
                                                     subscription_id, base_url)
        self._client = ServiceClient(self.config.credentials, self.config)
        self.models = None

    def query(self, url, method, query_parameters, header_parameters, body,
              expected_status_codes):
        # Construct and send request
        operation_config = {}

        request = None

        if method == 'GET':
            request = self._client.get(url, query_parameters)
        elif method == 'PUT':
            request = self._client.put(url, query_parameters)
        elif method == 'POST':
            request = self._client.post(url, query_parameters)
        elif method == 'HEAD':
            request = self._client.head(url, query_parameters)
        elif method == 'PATCH':
            request = self._client.patch(url, query_parameters)
        elif method == 'DELETE':
            request = self._client.delete(url, query_parameters)
        elif method == 'MERGE':
            request = self._client.merge(url, query_parameters)

        response = self._client.send(request, header_parameters, body,
                                     **operation_config)

        if response.status_code not in expected_status_codes:
            exp = CloudError(response)
            exp.request_id = response.headers.get('x-ms-request-id')
            raise exp

        return response
Exemplo n.º 3
0
class DeviceTwinAPI(object):
    """Use this REST API to manage device twins in the identity registry of an IoT hub.
    This API enables you to create and update device twins and invoke direct methods on devices.
    Each API call must include the api-version query parameter as well the authorization header
     containing a Shared Access Signature token with the appropriate permissions.

    :ivar config: Configuration for client.
    :vartype config: DeviceTwinAPIConfiguration

    :param credentials: Credentials needed for the client to connect to Azure.
    :type credentials: :mod:`A msrestazure Credentials
     object<msrestazure.azure_active_directory>`
    :param str base_url: Service URL
    """
    def __init__(self, credentials, base_url=None):

        self.config = DeviceTwinAPIConfiguration(credentials, base_url)
        self._client = ServiceClient(self.config.credentials, self.config)

        self.api_version = "2017-11-08-preview"

        client_models = {
            k: v
            for k, v in models.__dict__.items() if isinstance(v, type)
        }
        self._serialize = Serializer(client_models)
        self._deserialize = Deserializer(client_models)

    def get_device_twin(self,
                        deviceid,
                        custom_headers=None,
                        raw=False,
                        **operation_config):
        """Get a device twin.

        Get a device twin. See
        https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-device-twins
        for more information.

        :param deviceid:
        :type deviceid: str
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>`
         or :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>` if
         raw=true
        :rtype: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>` or
         :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
        """
        # Construct URL
        url = '/twins/{id}'
        path_format_arguments = {
            'id': self._serialize.url("id", deviceid, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}
        query_parameters['api-version'] = self._serialize.query(
            "self.api_version", self.api_version, 'str')

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if self.config.generate_client_request_id:
            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
        if custom_headers:
            header_parameters.update(custom_headers)
        if self.config.accept_language is not None:
            header_parameters['accept-language'] = self._serialize.header(
                "self.config.accept_language", self.config.accept_language,
                'str')

        # Construct and send request
        request = self._client.get(url, query_parameters)
        response = self._client.send(request, header_parameters,
                                     **operation_config)

        if response.status_code not in [200]:
            raise models.error_details.ErrorDetailsException(
                self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('object', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized

    def update_device_twin(self,
                           deviceid,
                           device_twin_info,
                           custom_headers=None,
                           raw=False,
                           **operation_config):
        """Updates tags and desired properties of a device twin.

        Update a device twin. See
        https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-device-twins
        for more information.

        :param deviceid:
        :type deviceid: str
        :param device_twin_info:
        :type device_twin_info: :class:`DeviceTwinInfo
         <device_twin.models.DeviceTwinInfo>`
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>`
         or :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>` if
         raw=true
        :rtype: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>` or
         :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
        """
        # Construct URL
        url = '/twins/{id}'
        path_format_arguments = {
            'id': self._serialize.url("id", deviceid, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}
        query_parameters['api-version'] = self._serialize.query(
            "self.api_version", self.api_version, 'str')

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if self.config.generate_client_request_id:
            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
        if custom_headers:
            header_parameters.update(custom_headers)
        if self.config.accept_language is not None:
            header_parameters['accept-language'] = self._serialize.header(
                "self.config.accept_language", self.config.accept_language,
                'str')

        # Construct body
        body_content = self._serialize.body(device_twin_info, 'object')

        # Construct and send request
        request = self._client.patch(url, query_parameters)
        response = self._client.send(request, header_parameters, body_content,
                                     **operation_config)

        if response.status_code not in [200]:
            raise models.error_details.ErrorDetailsException(
                self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('object', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized

    def replace_device_twin(self,
                            deviceid,
                            device_twin_info,
                            custom_headers=None,
                            raw=False,
                            **operation_config):
        """Updates tags and desired properties of a device twin.

        Update a device twin. See
        https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-device-twins
        for more information.

        :param deviceid:
        :type deviceid: str
        :param device_twin_info:
        :type device_twin_info: :class:`DeviceTwinInfo
         <device_twin.models.DeviceTwinInfo>`
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>`
         or :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>` if
         raw=true
        :rtype: :class:`DeviceTwinInfo <device_twin.models.DeviceTwinInfo>` or
         :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
        """
        # Construct URL
        url = '/twins/{id}'
        path_format_arguments = {
            'id': self._serialize.url("id", deviceid, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}
        query_parameters['api-version'] = self._serialize.query(
            "self.api_version", self.api_version, 'str')

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if self.config.generate_client_request_id:
            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
        if custom_headers:
            header_parameters.update(custom_headers)
        if self.config.accept_language is not None:
            header_parameters['accept-language'] = self._serialize.header(
                "self.config.accept_language", self.config.accept_language,
                'str')

        # Construct body
        body_content = self._serialize.body(device_twin_info, 'object')

        # Construct and send request
        request = self._client.put(url, query_parameters)
        response = self._client.send(request, header_parameters, body_content,
                                     **operation_config)

        if response.status_code not in [200]:
            raise models.error_details.ErrorDetailsException(
                self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('object', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized

    def invoke_device_method(self,
                             deviceid,
                             direct_method_request,
                             custom_headers=None,
                             raw=False,
                             **operation_config):
        """Invoke a direct method on a device.

        Invoke a direct method on a device. See
        https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-direct-methods
        for more information.

        :param deviceid:
        :type deviceid: str
        :param direct_method_request:
        :type direct_method_request: :class:`CloudToDeviceMethod
         <device_twin.models.CloudToDeviceMethod>`
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: :class:`CloudToDeviceMethodResult
         <device_twin.models.CloudToDeviceMethodResult>` or
         :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>` if
         raw=true
        :rtype: :class:`CloudToDeviceMethodResult
         <device_twin.models.CloudToDeviceMethodResult>` or
         :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
        """
        # Construct URL
        url = '/twins/{id}/methods'
        path_format_arguments = {
            'id': self._serialize.url("id", deviceid, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}
        query_parameters['api-version'] = '2016-11-14'

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if self.config.generate_client_request_id:
            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
        if custom_headers:
            header_parameters.update(custom_headers)
        if self.config.accept_language is not None:
            header_parameters['accept-language'] = self._serialize.header(
                "self.config.accept_language", self.config.accept_language,
                'str')

        # Construct body
        body_content = self._serialize.body(direct_method_request,
                                            'CloudToDeviceMethod')

        # Construct and send request
        request = self._client.post(url, query_parameters)
        response = self._client.send(request, header_parameters, body_content,
                                     **operation_config)

        if response.status_code not in [200]:
            raise models.error_details.ErrorDetailsException(
                self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('CloudToDeviceMethodResult',
                                             response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized
Exemplo n.º 4
0
class UsersUser(object):
    """UsersUser

    :ivar config: Configuration for client.
    :vartype config: UsersUserConfiguration

    :ivar users: Users operations
    :vartype users: users.operations.UsersOperations

    :param top: Show only the first n items
    :type top: int
    :param search: Search items by search phrases
    :type search: str
    :param str base_url: Service URL
    """

    def __init__(
            self, top=None, search=None, base_url=None):

        self.config = UsersUserConfiguration(top, search, base_url)
        self._client = ServiceClient(None, self.config)

        client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
        self.api_version = 'v1.0-Beta'
        self._serialize = Serializer(client_models)
        self._deserialize = Deserializer(client_models)

        self.users = UsersOperations(
            self._client, self.config, self._serialize, self._deserialize)

    def list_user(
            self, orderby=None, select=None, custom_headers=None, raw=False, **operation_config):
        """Get entities from users.

        :param orderby: Order items by property values
        :type orderby: list[str]
        :param select: Select properties to be returned
        :type select: list[str]
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: Pathsusersgetresponses200contentapplicationJsonschema or
         ClientRawResponse if raw=true
        :rtype:
         ~users.models.Pathsusersgetresponses200contentapplicationJsonschema or
         ~msrest.pipeline.ClientRawResponse
        :raises:
         :class:`OdataerrorException<users.models.OdataerrorException>`
        """
        # Construct URL
        url = self.list_user.metadata['url']

        # Construct parameters
        query_parameters = {}
        if self.config.top is not None:
            query_parameters['$top'] = self._serialize.query("self.config.top", self.config.top, 'int', minimum=0)
        if self.config.search is not None:
            query_parameters['$search'] = self._serialize.query("self.config.search", self.config.search, 'str')
        if orderby is not None:
            query_parameters['$orderby'] = self._serialize.query("orderby", orderby, '[str]', div=',', unique=True)
        if select is not None:
            query_parameters['$select'] = self._serialize.query("select", select, '[str]', div=',', unique=True)

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if custom_headers:
            header_parameters.update(custom_headers)

        # Construct and send request
        request = self._client.get(url, query_parameters)
        response = self._client.send(request, header_parameters, stream=False, **operation_config)

        if response.status_code not in [200]:
            raise models.OdataerrorException(self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('Pathsusersgetresponses200contentapplicationJsonschema', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized
    list_user.metadata = {'url': '/users'}

    def create_user(
            self, body, custom_headers=None, raw=False, **operation_config):
        """Add new entity to users.

        :param body: New entity
        :type body: ~users.models.Microsoftgraphuser
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: Microsoftgraphuser or ClientRawResponse if raw=true
        :rtype: ~users.models.Microsoftgraphuser or
         ~msrest.pipeline.ClientRawResponse
        :raises:
         :class:`OdataerrorException<users.models.OdataerrorException>`
        """
        # Construct URL
        url = self.create_user.metadata['url']

        # Construct parameters
        query_parameters = {}

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if custom_headers:
            header_parameters.update(custom_headers)

        # Construct body
        body_content = self._serialize.body(body, 'Microsoftgraphuser')

        # Construct and send request
        request = self._client.post(url, query_parameters)
        response = self._client.send(
            request, header_parameters, body_content, stream=False, **operation_config)

        if response.status_code not in [201]:
            raise models.OdataerrorException(self._deserialize, response)

        deserialized = None

        if response.status_code == 201:
            deserialized = self._deserialize('Microsoftgraphuser', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized
    create_user.metadata = {'url': '/users'}

    def get_user(
            self, user_id, select=None, custom_headers=None, raw=False, **operation_config):
        """Get entity from users by key.

        :param user_id: key: user-id of user
        :type user_id: str
        :param select: Select properties to be returned
        :type select: list[str]
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: Microsoftgraphuser or ClientRawResponse if raw=true
        :rtype: ~users.models.Microsoftgraphuser or
         ~msrest.pipeline.ClientRawResponse
        :raises:
         :class:`OdataerrorException<users.models.OdataerrorException>`
        """
        # Construct URL
        url = self.get_user.metadata['url']
        path_format_arguments = {
            'user-id': self._serialize.url("user_id", user_id, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}
        if select is not None:
            query_parameters['$select'] = self._serialize.query("select", select, '[str]', div=',', unique=True)

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if custom_headers:
            header_parameters.update(custom_headers)

        # Construct and send request
        request = self._client.get(url, query_parameters)
        response = self._client.send(request, header_parameters, stream=False, **operation_config)

        if response.status_code not in [200]:
            raise models.OdataerrorException(self._deserialize, response)

        deserialized = None

        if response.status_code == 200:
            deserialized = self._deserialize('Microsoftgraphuser', response)

        if raw:
            client_raw_response = ClientRawResponse(deserialized, response)
            return client_raw_response

        return deserialized
    get_user.metadata = {'url': '/users/{user-id}'}

    def update_user(
            self, body, user_id, custom_headers=None, raw=False, **operation_config):
        """Update entity in users.

        :param body: New property values
        :type body: ~users.models.Microsoftgraphuser
        :param user_id: key: user-id of user
        :type user_id: str
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: None or ClientRawResponse if raw=true
        :rtype: None or ~msrest.pipeline.ClientRawResponse
        :raises:
         :class:`OdataerrorException<users.models.OdataerrorException>`
        """
        # Construct URL
        url = self.update_user.metadata['url']
        path_format_arguments = {
            'user-id': self._serialize.url("user_id", user_id, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if custom_headers:
            header_parameters.update(custom_headers)

        # Construct body
        body_content = self._serialize.body(body, 'Microsoftgraphuser')

        # Construct and send request
        request = self._client.patch(url, query_parameters)
        response = self._client.send(
            request, header_parameters, body_content, stream=False, **operation_config)

        if response.status_code not in [204]:
            raise models.OdataerrorException(self._deserialize, response)

        if raw:
            client_raw_response = ClientRawResponse(None, response)
            return client_raw_response
    update_user.metadata = {'url': '/users/{user-id}'}

    def delete_user(
            self, user_id, if_match=None, custom_headers=None, raw=False, **operation_config):
        """Delete entity from users.

        :param user_id: key: user-id of user
        :type user_id: str
        :param if_match: ETag
        :type if_match: str
        :param dict custom_headers: headers that will be added to the request
        :param bool raw: returns the direct response alongside the
         deserialized response
        :param operation_config: :ref:`Operation configuration
         overrides<msrest:optionsforoperations>`.
        :return: None or ClientRawResponse if raw=true
        :rtype: None or ~msrest.pipeline.ClientRawResponse
        :raises:
         :class:`OdataerrorException<users.models.OdataerrorException>`
        """
        # Construct URL
        url = self.delete_user.metadata['url']
        path_format_arguments = {
            'user-id': self._serialize.url("user_id", user_id, 'str')
        }
        url = self._client.format_url(url, **path_format_arguments)

        # Construct parameters
        query_parameters = {}

        # Construct headers
        header_parameters = {}
        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
        if custom_headers:
            header_parameters.update(custom_headers)
        if if_match is not None:
            header_parameters['If-Match'] = self._serialize.header("if_match", if_match, 'str')

        # Construct and send request
        request = self._client.delete(url, query_parameters)
        response = self._client.send(request, header_parameters, stream=False, **operation_config)

        if response.status_code not in [204]:
            raise models.OdataerrorException(self._deserialize, response)

        if raw:
            client_raw_response = ClientRawResponse(None, response)
            return client_raw_response
    delete_user.metadata = {'url': '/users/{user-id}'}