# Path: services/orcid/pyorcid/constants.py
# ORCID_PUBLIC_BASE_URL = 'http://pub.orcid.org/'
# 
# Path: services/orcid/pyorcid/utils.py
# def dictmapper(typename, mapping):
#     """
#     A factory to create `namedtuple`-like classes from a field-to-dict-path
#     mapping::
# 
#         Person = dictmapper({'person':('person','name')})
#         example_dict = {'person':{'name':'John'}}
#         john = Person(example_dict)
#         assert john.name == 'John'
# 
#     If a function is specified as a mapping value instead of a dict "path", it
#     will be run with the backing dict as its first argument.
#     """
#     def init(self, d, *args, **kwargs):
#         """
#         Initialize `dictmapper` classes with a dict to back getters.
#         """
#         self._original_dict = d
# 
#     def getter_from_dict_path(path):
#         if not callable(path) and len(path) < 1:
#             raise ValueError('Dict paths should be iterables with at least one'
#                              ' key or callable objects that take one argument.')
#         def getter(self):
#             cur_dict = self._original_dict
#             if callable(path):
#                 return path(cur_dict)
#             return dict_value_from_path(cur_dict, path)
#         return getter
# 
#     prop_mapping = dict((k, property(getter_from_dict_path(v))) 
#                         for k, v in mapping.iteritems())
#     prop_mapping['__init__'] = init
#     return type(typename, tuple(), prop_mapping)
# 
# class MappingRule(object):
#     def __init__(self, path, further_func = lambda x : x):
#         self.path = path
#         self.further_func = further_func
# 
#     def __call__(self, d):
#         return self.further_func(dict_value_from_path(d, self.path))
# 
# Path: services/orcid/pyorcid/exceptions.py
# class NotFoundException(ORCIDException):
#     pass


import requests
import json
import logging
from .constants import ORCID_PUBLIC_BASE_URL
from .utils import dictmapper, MappingRule as to
from .exceptions import NotFoundException

Citation = dictmapper('Citation', {
    'citation':['citation'],
    'citation_type':['work-citation-type']
})

def write_logs(resp):
    logger.debug(json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': ')))

def get(orcid_id):
    """
    Get an author based on an ORCID identifier.
    """
    resp = requests.get(ORCID_PUBLIC_BASE_URL + unicode(orcid_id),
                        headers=BASE_HEADERS)
    write_logs(resp)
    json_body = resp.json()
    return Author(json_body)

# def get_with_json(orcid_id):
#     """
#     Get an author based on an ORCID identifier and json
#     """
#     resp = requests.get(ORCID_PUBLIC_BASE_URL + unicode(orcid_id),
#                         headers=BASE_HEADERS)
#     json_body = resp.json()
#     write_logs(resp)
#     return Author(json_body), json_body

def search(query):

# Path: services/orcid/pyorcid/constants.py
# ORCID_PUBLIC_BASE_URL = 'http://pub.orcid.org/'
# 
# Path: services/orcid/pyorcid/utils.py
# def dictmapper(typename, mapping):
#     """
#     A factory to create `namedtuple`-like classes from a field-to-dict-path
#     mapping::
# 
#         Person = dictmapper({'person':('person','name')})
#         example_dict = {'person':{'name':'John'}}
#         john = Person(example_dict)
#         assert john.name == 'John'
# 
#     If a function is specified as a mapping value instead of a dict "path", it
#     will be run with the backing dict as its first argument.
#     """
#     def init(self, d, *args, **kwargs):
#         """
#         Initialize `dictmapper` classes with a dict to back getters.
#         """
#         self._original_dict = d
# 
#     def getter_from_dict_path(path):
#         if not callable(path) and len(path) < 1:
#             raise ValueError('Dict paths should be iterables with at least one'
#                              ' key or callable objects that take one argument.')
#         def getter(self):
#             cur_dict = self._original_dict
#             if callable(path):
#                 return path(cur_dict)
#             return dict_value_from_path(cur_dict, path)
#         return getter
# 
#     prop_mapping = dict((k, property(getter_from_dict_path(v))) 
#                         for k, v in mapping.iteritems())
#     prop_mapping['__init__'] = init
#     return type(typename, tuple(), prop_mapping)
# 
# class MappingRule(object):
#     def __init__(self, path, further_func = lambda x : x):
#         self.path = path
#         self.further_func = further_func
# 
#     def __call__(self, d):
#         return self.further_func(dict_value_from_path(d, self.path))
# 
# Path: services/orcid/pyorcid/exceptions.py
# class NotFoundException(ORCIDException):
#     pass


# Path: services/orcid/pyorcid/rest.py
import requests
import json
import logging
from .constants import ORCID_PUBLIC_BASE_URL
from .utils import dictmapper, MappingRule as to
from .exceptions import NotFoundException


Citation = dictmapper('Citation', {
    'citation':['citation'],
    'citation_type':['work-citation-type']
})

def write_logs(resp):
    logger.debug(json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': ')))

def get(orcid_id):
    """
    Get an author based on an ORCID identifier.
    """
    resp = requests.get(ORCID_PUBLIC_BASE_URL + unicode(orcid_id),
                        headers=BASE_HEADERS)
    write_logs(resp)
    json_body = resp.json()
    return Author(json_body)

# def get_with_json(orcid_id):
#     """
#     Get an author based on an ORCID identifier and json
#     """
#     resp = requests.get(ORCID_PUBLIC_BASE_URL + unicode(orcid_id),
#                         headers=BASE_HEADERS)
#     json_body = resp.json()
#     write_logs(resp)
#     return Author(json_body), json_body

def search(query):

    resp = requests.get(ORCID_PUBLIC_BASE_URL + 'search/orcid-bio',