# Path: smc/api/exceptions.py
# class SMCOperationFailure(SMCException):
#     """ Exception class for storing results from calls to the SMC
#     This is thrown for HTTP methods that do not return the expected HTTP
#     status code. See each http_* method in :py:mod:`smc.api.web` for
#     expected success status
# 
#     :param response: response object returned from HTTP method
#     :param msg: optional msg to insert
# 
#     Instance attributes:
# 
#     :ivar response: http request response object
#     :ivar code: http status code
#     :ivar status: status from SMC API
#     :ivar message: message attribute from SMC API
#     :ivar details: details list from SMC API (may not always exist)
#     :ivar smcresult: :py:class:`smc.api.web.SMCResult` object for consistent returns
#     """
# 
#     def __init__(self, response=None):
#         # Response is type <class 'requests.models.Response'>
#         self.response = response
#         self.code = None
#         self.smcresult = smc.api.web.SMCResult()
#         if response is not None:
#             self._unpack_response()
# 
#     def _unpack_response(self):
#         details = None
#         self.code = self.response.status_code
#         if self.response.headers.get('content-type') == 'application/json':
#             try:
#                 data = self.response.json()
#             except ValueError:
#                 message = 'No valid message returned from SMC server'
#             else:
#                 message = data.get('message', None)
#                 details = data.get('details', None)
#         else:  # it's not json
#             if self.response.text:
#                 message = self.response.text
#             else:
#                 message = "No message returned from SMC server"
# 
#         self.smcresult.code = self.code
# 
#         if details:
#             details = unicode_to_bytes(' '.join(details)) \
#                 if isinstance(details, list) else unicode_to_bytes(details)
#             # Some error messages from SMC include line breaks
#             details = details.replace('\n', ' ').rstrip()
# 
#         if message:
#             message = unicode_to_bytes(message)
# 
#         if message and details:
#             self.smcresult.msg = '{} {}'.format(message, details)
#         elif details:
#             self.smcresult.msg = details
#         else:
#             self.smcresult.msg = message
# 
#     def __repr__(self):
#         return 'SMCOperationFailure(response=%s)' % (self.response)
# 
# class SMCConnectionError(SMCException):
#     """
#     Thrown when there are connection related issues with the SMC.
#     This could be that the underlying http requests library could not connect
#     due to wrong IP address, wrong port, or time out
#     """


import json
import os.path
import collections
import logging
import requests
from smc.api.exceptions import SMCOperationFailure, SMCConnectionError
    
    :param Session user_session: session object
    :param str method: method for request
    :param SMCRequest request: request object
    :raises SMCOperationFailure: failure with reason
    :rtype: SMCResult
    """
    if user_session.session:
        session = user_session.session # requests session
        try:
            method = method.upper() if method else ''
            
            if method == GET:
                if request.filename:  # File download request
                    return file_download(user_session, request)
                
                response = session.get(
                    request.href,
                    params=request.params,
                    headers=request.headers,
                    timeout=user_session.timeout)
                
                response.encoding = 'utf-8'
                
                counters.update(read=1)

                if logger.isEnabledFor(logging.DEBUG):
                    debug(response)
                
                if response.status_code not in (200, 204, 304):

# Path: smc/api/exceptions.py
# class SMCOperationFailure(SMCException):
#     """ Exception class for storing results from calls to the SMC
#     This is thrown for HTTP methods that do not return the expected HTTP
#     status code. See each http_* method in :py:mod:`smc.api.web` for
#     expected success status
# 
#     :param response: response object returned from HTTP method
#     :param msg: optional msg to insert
# 
#     Instance attributes:
# 
#     :ivar response: http request response object
#     :ivar code: http status code
#     :ivar status: status from SMC API
#     :ivar message: message attribute from SMC API
#     :ivar details: details list from SMC API (may not always exist)
#     :ivar smcresult: :py:class:`smc.api.web.SMCResult` object for consistent returns
#     """
# 
#     def __init__(self, response=None):
#         # Response is type <class 'requests.models.Response'>
#         self.response = response
#         self.code = None
#         self.smcresult = smc.api.web.SMCResult()
#         if response is not None:
#             self._unpack_response()
# 
#     def _unpack_response(self):
#         details = None
#         self.code = self.response.status_code
#         if self.response.headers.get('content-type') == 'application/json':
#             try:
#                 data = self.response.json()
#             except ValueError:
#                 message = 'No valid message returned from SMC server'
#             else:
#                 message = data.get('message', None)
#                 details = data.get('details', None)
#         else:  # it's not json
#             if self.response.text:
#                 message = self.response.text
#             else:
#                 message = "No message returned from SMC server"
# 
#         self.smcresult.code = self.code
# 
#         if details:
#             details = unicode_to_bytes(' '.join(details)) \
#                 if isinstance(details, list) else unicode_to_bytes(details)
#             # Some error messages from SMC include line breaks
#             details = details.replace('\n', ' ').rstrip()
# 
#         if message:
#             message = unicode_to_bytes(message)
# 
#         if message and details:
#             self.smcresult.msg = '{} {}'.format(message, details)
#         elif details:
#             self.smcresult.msg = details
#         else:
#             self.smcresult.msg = message
# 
#     def __repr__(self):
#         return 'SMCOperationFailure(response=%s)' % (self.response)
# 
# class SMCConnectionError(SMCException):
#     """
#     Thrown when there are connection related issues with the SMC.
#     This could be that the underlying http requests library could not connect
#     due to wrong IP address, wrong port, or time out
#     """


# Path: smc/api/web.py
import json
import os.path
import collections
import logging
import requests
from smc.api.exceptions import SMCOperationFailure, SMCConnectionError

    
    :param Session user_session: session object
    :param str method: method for request
    :param SMCRequest request: request object
    :raises SMCOperationFailure: failure with reason
    :rtype: SMCResult
    """
    if user_session.session:
        session = user_session.session # requests session
        try:
            method = method.upper() if method else ''
            
            if method == GET:
                if request.filename:  # File download request
                    return file_download(user_session, request)
                
                response = session.get(
                    request.href,
                    params=request.params,
                    headers=request.headers,
                    timeout=user_session.timeout)
                
                response.encoding = 'utf-8'
                
                counters.update(read=1)

                if logger.isEnabledFor(logging.DEBUG):
                    debug(response)
                
                if response.status_code not in (200, 204, 304):

                    raise SMCOperationFailure(response)