# 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/itis/itis.py
# def _df(x, as_dataframe=False):
#     if as_dataframe and pd:
#         if isinstance(x, dict):
#             x = [x]
#         df = pd.DataFrame.from_records(x)
#         return df
#     else:
#         return x


import sys
import warnings
import requests
import re
import json
import csv
    import pandas as pd
    import doctest
from lxml import etree
from pkg_resources import resource_filename
from pytaxize.refactor import Refactor
from pytaxize.itis.itis import _df
    :param raw: Raw data or not (default)
    :param callopts: Further args passed to request

    Usage::

        import pytaxize
        pytaxize.vascan_search(q = ["Helianthus annuus"])
        pytaxize.vascan_search(q = ["Helianthus annuus"], raw=True)
        pytaxize.vascan_search(q = ["Helianthus annuus", "Crataegus dodgei"], raw=True)

        # format type
        ## json
        pytaxize.vascan_search(q = ["Helianthus annuus"], format="json", raw=True)

        ## xml
        pytaxize.vascan_search(q = ["Helianthus annuus"], format="xml", raw=True)

        # lots of names, in this case 50
        splist = pytaxize.names_list(rank='species', size=50)
        pytaxize.vascan_search(q = splist)
    """
    if format == "json":
        url = "http://data.canadensys.net/vascan/api/0.1/search.json"
    else:
        url = "http://data.canadensys.net/vascan/api/0.1/search.xml"

    if len(q) > 1:
        query = "\n".join(q)
        payload = {"q": query}
        if format == "json":

# 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/itis/itis.py
# def _df(x, as_dataframe=False):
#     if as_dataframe and pd:
#         if isinstance(x, dict):
#             x = [x]
#         df = pd.DataFrame.from_records(x)
#         return df
#     else:
#         return x


# Path: pytaxize/tax.py
import sys
import warnings
import requests
import re
import json
import csv
    import pandas as pd
    import doctest
from lxml import etree
from pkg_resources import resource_filename
from pytaxize.refactor import Refactor
from pytaxize.itis.itis import _df

    :param raw: Raw data or not (default)
    :param callopts: Further args passed to request

    Usage::

        import pytaxize
        pytaxize.vascan_search(q = ["Helianthus annuus"])
        pytaxize.vascan_search(q = ["Helianthus annuus"], raw=True)
        pytaxize.vascan_search(q = ["Helianthus annuus", "Crataegus dodgei"], raw=True)

        # format type
        ## json
        pytaxize.vascan_search(q = ["Helianthus annuus"], format="json", raw=True)

        ## xml
        pytaxize.vascan_search(q = ["Helianthus annuus"], format="xml", raw=True)

        # lots of names, in this case 50
        splist = pytaxize.names_list(rank='species', size=50)
        pytaxize.vascan_search(q = splist)
    """
    if format == "json":
        url = "http://data.canadensys.net/vascan/api/0.1/search.json"
    else:
        url = "http://data.canadensys.net/vascan/api/0.1/search.xml"

    if len(q) > 1:
        query = "\n".join(q)
        payload = {"q": query}
        if format == "json":

            out = Refactor(url, payload, request="post").json()