# Path: fmiapi/fmierrors.py
# class RequestException(Exception):
# 
#     def __init__(self, text, error_code, html=""):
#         self.error_code = 0
#         self.message = text
#         self.error_code = error_code
#         self.html = html
# 
#     def __str__(self):
#         if self.html != "":
#             return self.html
#         else:
#             return self.message
# 
# class NoDataSetsException(Exception):
# 
#     def __init__(self, fmisid):
#         self.message = "No data sets found for {}".format(fmisid)
#         self.error_code = "NODATASETS"
# 
#     def __str__(self):
#         return self.message


import http.client
import urllib.request
from lxml import etree
from datetime import datetime
from fmiapi.fmierrors import RequestException, NoDataSetsException
    params = {
        "service": "CSW",
        "version": "2.0.2",
        "request": "GetRecords",
        "resultType": "results",
        "outputSchema": "http://www.isotc211.org/2005/gmd",
        "NAMESPACE": "xmlns(gmd=http://www.isotc211.org/2005/gmd)",
        "typeNames": "gmd:MD_Metadata",
        "elementSetName": "full",
        "startPosition": 1,
        "maxRecords": 100,
        "CONSTRAINTLANGUAGE": "CQL_TEXT",
        "CONSTRAINT": "AnyText ='" + fmisid + "'",
        "CONSTRAINT_LANGUAGE_VERSION": "1.1.0"
    }

    connection.request("GET", "/geonetwork/srv/csw?{query_params}"
                       .format(query_params=urllib.parse.urlencode(params)),
                       headers=headers)

    response = connection.getresponse()
    if response.status == 200:
        data = etree.XML(response.read())

        if _response_was_exception(data):
            # Looks like we got an exception. Most likely this is random error on FMI's side, so try
            # again couple of times before giving up
            if request_retry < _RETRIEVAL_RETRY_COUNT:
                return _retrieve_metadata_by_fmisid(fmisid, request_retry=request_retry+1)
            else:

# Path: fmiapi/fmierrors.py
# class RequestException(Exception):
# 
#     def __init__(self, text, error_code, html=""):
#         self.error_code = 0
#         self.message = text
#         self.error_code = error_code
#         self.html = html
# 
#     def __str__(self):
#         if self.html != "":
#             return self.html
#         else:
#             return self.message
# 
# class NoDataSetsException(Exception):
# 
#     def __init__(self, fmisid):
#         self.message = "No data sets found for {}".format(fmisid)
#         self.error_code = "NODATASETS"
# 
#     def __str__(self):
#         return self.message


# Path: fmiapi/fmicatalogservice.py
import http.client
import urllib.request
from lxml import etree
from datetime import datetime
from fmiapi.fmierrors import RequestException, NoDataSetsException

    params = {
        "service": "CSW",
        "version": "2.0.2",
        "request": "GetRecords",
        "resultType": "results",
        "outputSchema": "http://www.isotc211.org/2005/gmd",
        "NAMESPACE": "xmlns(gmd=http://www.isotc211.org/2005/gmd)",
        "typeNames": "gmd:MD_Metadata",
        "elementSetName": "full",
        "startPosition": 1,
        "maxRecords": 100,
        "CONSTRAINTLANGUAGE": "CQL_TEXT",
        "CONSTRAINT": "AnyText ='" + fmisid + "'",
        "CONSTRAINT_LANGUAGE_VERSION": "1.1.0"
    }

    connection.request("GET", "/geonetwork/srv/csw?{query_params}"
                       .format(query_params=urllib.parse.urlencode(params)),
                       headers=headers)

    response = connection.getresponse()
    if response.status == 200:
        data = etree.XML(response.read())

        if _response_was_exception(data):
            # Looks like we got an exception. Most likely this is random error on FMI's side, so try
            # again couple of times before giving up
            if request_retry < _RETRIEVAL_RETRY_COUNT:
                return _retrieve_metadata_by_fmisid(fmisid, request_retry=request_retry+1)
            else:

                raise RequestException("Error in metadata retrieval for fmisid " + fmisid, 'METADATA_RETRIEVAL')