# Path: partycrasher/crash.py
# class Crash(PCDict):
#     
#     __slots__ = tuple()
# 
#     synonyms = {
#         'crash_id': 'database_id', # Mozilla
#         'os_ver' : 'os_version', # Mozilla
#         'cpu_arch' : 'cpu', # Mozilla
#         'frames' : 'stacktrace', # Mozilla
#     }
# 
#     canonical_fields = {
#         'date': mustbe_date,
#         'stacktrace': PCType(
#             checker=Stacktrace,
#             converter=Stacktrace,
#             ),
#         'database_id': mustbe_string,
#         'project': mustbe_project,
#         'type': mustbe_crash_type,
#         'buckets': mustbe_buckets,
#     }
# 
#     def get_bucket_id(self, threshold):
#         key = Threshold(threshold).to_elasticsearch()
#         try:
#             buckets = self['buckets']
#         except KeyError:
#             raise Exception('No assigned buckets for: {!r}'.format(self))
#         try:
#             return buckets[key]
#         except KeyError:
#             raise Exception('Buckets threshold {} not assigned for: '
#                             '{!r}'.format(key, self))
# 
#     @classmethod
#     def load_from_file(cls, path):
#         from partycrasher import launchpad_crash
#         crash_classes = [ launchpad_crash.LaunchpadCrash ]
# 
#         crash = None
#         for crash_class in crash_classes:
#             try:
#                 crash = crash_class.load_from_file(path)
#             except NotImplementedError:
#                 raise
#             else:
#                 break
#         if crash is None:
#             raise NotImplementedError("I don't know how to load this!")
#         return crash
# 
#     @staticmethod
#     def make_id(project, database_id):
#         raise NotImplementedError("make_id removed")
# 
#     @property
#     def id(self):
#         return self['database_id']
#         #elif isinstance(o, Buckets):
#             #return o.json_serializable()
#     
#     def jsonify(self):
#         return self.as_dict()
#     
#     @classmethod
#     def fromjson(cls, s):
#         d = json.loads(s)
#         assert isinstance(d, dict)
#         c = cls(d)
#         return c


import requests
from partycrasher.crash import Crash
class RestClient:
    
    def __init__(self, root_url="http://localhost:5000/"):
        self.origin = root_url.rstrip('/')

    @property
    def root_url(self):
        """
        The root URL of the REST service.
        """
        return self.origin + '/'

    def path_to(self, *args):
        """
        Create a URL path, relative to the current origin.
        """
        return '/'.join((self.origin,) + args)

    def get_a_bunch_of_crashes(self, date_range_start, limit):
      bunch = []
      step = 100
      for from_ in range(0, limit, step):
          query = {
            'from': from_,
            'since': date_range_start,
            'size': step,
          }
          response = requests.get(self.path_to('*', 'search'), params=query)
          response.raise_for_status()
          for crash in response.json():

# Path: partycrasher/crash.py
# class Crash(PCDict):
#     
#     __slots__ = tuple()
# 
#     synonyms = {
#         'crash_id': 'database_id', # Mozilla
#         'os_ver' : 'os_version', # Mozilla
#         'cpu_arch' : 'cpu', # Mozilla
#         'frames' : 'stacktrace', # Mozilla
#     }
# 
#     canonical_fields = {
#         'date': mustbe_date,
#         'stacktrace': PCType(
#             checker=Stacktrace,
#             converter=Stacktrace,
#             ),
#         'database_id': mustbe_string,
#         'project': mustbe_project,
#         'type': mustbe_crash_type,
#         'buckets': mustbe_buckets,
#     }
# 
#     def get_bucket_id(self, threshold):
#         key = Threshold(threshold).to_elasticsearch()
#         try:
#             buckets = self['buckets']
#         except KeyError:
#             raise Exception('No assigned buckets for: {!r}'.format(self))
#         try:
#             return buckets[key]
#         except KeyError:
#             raise Exception('Buckets threshold {} not assigned for: '
#                             '{!r}'.format(key, self))
# 
#     @classmethod
#     def load_from_file(cls, path):
#         from partycrasher import launchpad_crash
#         crash_classes = [ launchpad_crash.LaunchpadCrash ]
# 
#         crash = None
#         for crash_class in crash_classes:
#             try:
#                 crash = crash_class.load_from_file(path)
#             except NotImplementedError:
#                 raise
#             else:
#                 break
#         if crash is None:
#             raise NotImplementedError("I don't know how to load this!")
#         return crash
# 
#     @staticmethod
#     def make_id(project, database_id):
#         raise NotImplementedError("make_id removed")
# 
#     @property
#     def id(self):
#         return self['database_id']
#         #elif isinstance(o, Buckets):
#             #return o.json_serializable()
#     
#     def jsonify(self):
#         return self.as_dict()
#     
#     @classmethod
#     def fromjson(cls, s):
#         d = json.loads(s)
#         assert isinstance(d, dict)
#         c = cls(d)
#         return c


# Path: partycrasher/rest/client.py
import requests
from partycrasher.crash import Crash

class RestClient:
    
    def __init__(self, root_url="http://localhost:5000/"):
        self.origin = root_url.rstrip('/')

    @property
    def root_url(self):
        """
        The root URL of the REST service.
        """
        return self.origin + '/'

    def path_to(self, *args):
        """
        Create a URL path, relative to the current origin.
        """
        return '/'.join((self.origin,) + args)

    def get_a_bunch_of_crashes(self, date_range_start, limit):
      bunch = []
      step = 100
      for from_ in range(0, limit, step):
          query = {
            'from': from_,
            'since': date_range_start,
            'size': step,
          }
          response = requests.get(self.path_to('*', 'search'), params=query)
          response.raise_for_status()
          for crash in response.json():

              crash = Crash(crash)