# Path: extras/python/iotsaControl/consts.py
# class UserIntervention(Exception):
#     pass
# 
# class IotsaError(RuntimeError):
#     pass
# 
# class CoapError(Exception):
#     pass
# 
# VERBOSE=[]


from future import standard_library
from builtins import str
from builtins import object
from .consts import UserIntervention, IotsaError, CoapError, VERBOSE
import sys
import socket
import time
import urllib.parse
import urllib.request
import json as jsonmod
import coapthon.client.helperclient
import coapthon.defines
import requests
import urllib3
            pass
        self.client = coapthon.client.helperclient.HelperClient(server=(host, port))
        
    def __del__(self):
        self.close()

    def close(self):
        if self.client:
            self.client.stop()
        self.client = None
        
    def _raiseIfError(self, reply):
        if not reply:
            raise CoapError("reply is None")
        if reply.code >= 128:
            codeMajor = (reply.code >> 5) & 0x07
            codeMinor = (reply.code & 0x1f)
            codeName = coapthon.defines.Codes.LIST.get(reply.code, None)
            if codeName:
                codeName = codeName.name
            else:
                codeName = 'unknown'
            raise CoapError('%d.%02d %s' % (codeMajor, codeMinor, codeName))
            
    def get(self, endpoint, json=None):
        assert json is None
        endpoint = self.basePath+endpoint
        if VERBOSE: print('COAP GET coap://%s:%d%s' % (self.client.server[0], self.client.server[1], endpoint))
        rv = self.client.get(endpoint)
        self._raiseIfError(rv)

# Path: extras/python/iotsaControl/consts.py
# class UserIntervention(Exception):
#     pass
# 
# class IotsaError(RuntimeError):
#     pass
# 
# class CoapError(Exception):
#     pass
# 
# VERBOSE=[]


# Path: extras/python/iotsaControl/protocols.py
from future import standard_library
from builtins import str
from builtins import object
from .consts import UserIntervention, IotsaError, CoapError, VERBOSE
import sys
import socket
import time
import urllib.parse
import urllib.request
import json as jsonmod
import coapthon.client.helperclient
import coapthon.defines
import requests
import urllib3

            pass
        self.client = coapthon.client.helperclient.HelperClient(server=(host, port))
        
    def __del__(self):
        self.close()

    def close(self):
        if self.client:
            self.client.stop()
        self.client = None
        
    def _raiseIfError(self, reply):
        if not reply:
            raise CoapError("reply is None")
        if reply.code >= 128:
            codeMajor = (reply.code >> 5) & 0x07
            codeMinor = (reply.code & 0x1f)
            codeName = coapthon.defines.Codes.LIST.get(reply.code, None)
            if codeName:
                codeName = codeName.name
            else:
                codeName = 'unknown'
            raise CoapError('%d.%02d %s' % (codeMajor, codeMinor, codeName))
            
    def get(self, endpoint, json=None):
        assert json is None
        endpoint = self.basePath+endpoint
        if VERBOSE: print('COAP GET coap://%s:%d%s' % (self.client.server[0], self.client.server[1], endpoint))
        rv = self.client.get(endpoint)
        self._raiseIfError(rv)

        if VERBOSE: print('COAP GET returned', rv.code, len(rv.payload), repr(rv.payload))