text: # Copyright (C) 2018 Riedel Communications GmbH & Co. KG
  #
  # Modifications Copyright 2018 British Broadcasting Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  
  import requests
  
  
  def ordered(obj):
      if isinstance(obj, dict):
          return sorted((k, ordered(v)) for k, v in obj.items())
      if isinstance(obj, list):
          return sorted(ordered(x) for x in obj)
      else:
          return obj
  
  
  def compare_json(json1, json2):
      """Compares two json objects for equality"""
      return ordered(json1) == ordered(json2)
  
  
  def do_request(method, url, data=None):
      """Perform a basic HTTP request with appropriate error handling"""
      try:
          s = requests.Session()
          req = None
          if data is not None:
              req = requests.Request(method, url, json=data)
          else:
              req = requests.Request(method, url)
          prepped = req.prepare()
          r = s.send(prepped)
          return True, r
      except requests.exceptions.Timeout:
          return False, "Connection timeout"
      except requests.exceptions.TooManyRedirects:
          return False, "Too many redirects"
      except requests.exceptions.ConnectionError as e:
          return False, str(e)
      except requests.exceptions.RequestException as e:
          return False, str(e)
  