# 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
            return datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%SZ')
        else:
            return datestr

    for item in records:
        ds = {
            "title_fi": item.find(".//gmd:title/gco:CharacterString", namespaces=_NAMESPACES).text,
            "starttime": convert_to_date(
                item.find(".//gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition",
                          namespaces=_NAMESPACES).text),
            "endtime": convert_to_date(item.find(".//gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition",
                                                 namespaces=_NAMESPACES).text),
            "longitude": float(item.find(".//gmd:EX_GeographicBoundingBox/gmd:westBoundLongitude/gco:Decimal",
                                         namespaces=_NAMESPACES).text),
            "latitude": float(item.find(".//gmd:EX_GeographicBoundingBox/gmd:southBoundLatitude/gco:Decimal",
                                        namespaces=_NAMESPACES).text),
            "link": item.find(".//gmd:distributionInfo//gmd:CI_OnlineResource/gmd:linkage/gmd:URL",
                              namespaces=_NAMESPACES).text,
            "identifier": item.find(".//gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString",
                                    namespaces=_NAMESPACES).text
        }
        result.append(ds)

    return sorted(list({v["title_fi"]: v for v in result}.values()), key=lambda x: x[
        'title_fi'])  # Remove duplicates with temporary dict where unique attribute is the key


def get_station_metadata(fmisid):
    result = _parse_data(_retrieve_metadata_by_fmisid(fmisid))
    if len(result) == 0:

# 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

            return datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%SZ')
        else:
            return datestr

    for item in records:
        ds = {
            "title_fi": item.find(".//gmd:title/gco:CharacterString", namespaces=_NAMESPACES).text,
            "starttime": convert_to_date(
                item.find(".//gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition",
                          namespaces=_NAMESPACES).text),
            "endtime": convert_to_date(item.find(".//gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition",
                                                 namespaces=_NAMESPACES).text),
            "longitude": float(item.find(".//gmd:EX_GeographicBoundingBox/gmd:westBoundLongitude/gco:Decimal",
                                         namespaces=_NAMESPACES).text),
            "latitude": float(item.find(".//gmd:EX_GeographicBoundingBox/gmd:southBoundLatitude/gco:Decimal",
                                        namespaces=_NAMESPACES).text),
            "link": item.find(".//gmd:distributionInfo//gmd:CI_OnlineResource/gmd:linkage/gmd:URL",
                              namespaces=_NAMESPACES).text,
            "identifier": item.find(".//gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString",
                                    namespaces=_NAMESPACES).text
        }
        result.append(ds)

    return sorted(list({v["title_fi"]: v for v in result}.values()), key=lambda x: x[
        'title_fi'])  # Remove duplicates with temporary dict where unique attribute is the key


def get_station_metadata(fmisid):
    result = _parse_data(_retrieve_metadata_by_fmisid(fmisid))
    if len(result) == 0:

        raise NoDataSetsException(fmisid)