# Path: pytaxize/refactor.py
# class Refactor:
#     def __init__(self, url, payload={}, request="get"):
#         self.url = url
#         self.payload = payload
#         self.request = request
# 
#     def return_requests(self, **kwargs):
#         if self.request == "get":
#             return requests.get(self.url, params=self.payload, **kwargs)
#         else:
#             return requests.post(self.url, params=self.payload, **kwargs)
# 
#     def xml(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             try:
#                 # If entrez api 'X-RateLimit-Remaining' header is 1 or below, pause for a second to allow rate limit to reset
#                 if (int(out.headers['X-RateLimit-Remaining'])<=1):
#                     time.sleep(1)
#             except:
#                 pass 
#             return tt
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             return tt
# 
#     def json(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
# 
#     def raw(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text


import sys
import requests
import datetime
import re
import json
import pkg_resources
    import doctest
from lxml import etree
from pytaxize.refactor import Refactor
from pytaxize.utils import *

        # Pass in many names or many id's
        out = col.children(name=["Buteo","Apis","Accipiter"], checklist="2012")
        # get just one element in list of output
        out[0]
    """

    assert_range_numeric(checklist, 2010, datetime.datetime.now().year)

    def func(x, y, checklist):
        url = "https://www.catalogueoflife.org/col/webservice"

        if checklist is None:
            pass
        else:
            checklist = str(checklist)
            if checklist in ["2012", "2011", "2010"]:
                url = re.sub("col", "annual-checklist/" + checklist, url)
            else:
                url = "https://www.catalogueoflife.org/annual-checklist/year/webservice"
                url = re.sub("year", checklist, url)

        payload = {
            "name": x,
            "id": y,
            "format": format,
            "response": "full",
            "start": start,
        }
        payload = {k: v for k, v in payload.items() if v is not None}

# Path: pytaxize/refactor.py
# class Refactor:
#     def __init__(self, url, payload={}, request="get"):
#         self.url = url
#         self.payload = payload
#         self.request = request
# 
#     def return_requests(self, **kwargs):
#         if self.request == "get":
#             return requests.get(self.url, params=self.payload, **kwargs)
#         else:
#             return requests.post(self.url, params=self.payload, **kwargs)
# 
#     def xml(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             try:
#                 # If entrez api 'X-RateLimit-Remaining' header is 1 or below, pause for a second to allow rate limit to reset
#                 if (int(out.headers['X-RateLimit-Remaining'])<=1):
#                     time.sleep(1)
#             except:
#                 pass 
#             return tt
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             return tt
# 
#     def json(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
# 
#     def raw(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text


# Path: pytaxize/col/col.py
import sys
import requests
import datetime
import re
import json
import pkg_resources
    import doctest
from lxml import etree
from pytaxize.refactor import Refactor
from pytaxize.utils import *


        # Pass in many names or many id's
        out = col.children(name=["Buteo","Apis","Accipiter"], checklist="2012")
        # get just one element in list of output
        out[0]
    """

    assert_range_numeric(checklist, 2010, datetime.datetime.now().year)

    def func(x, y, checklist):
        url = "https://www.catalogueoflife.org/col/webservice"

        if checklist is None:
            pass
        else:
            checklist = str(checklist)
            if checklist in ["2012", "2011", "2010"]:
                url = re.sub("col", "annual-checklist/" + checklist, url)
            else:
                url = "https://www.catalogueoflife.org/annual-checklist/year/webservice"
                url = re.sub("year", checklist, url)

        payload = {
            "name": x,
            "id": y,
            "format": format,
            "response": "full",
            "start": start,
        }
        payload = {k: v for k, v in payload.items() if v is not None}

        tt = Refactor(url, payload, request="get").xml()