示例#1
0
 def from_json(cls, json_map):
     if type(json_map) is str:
         json_map = json.loads(json_map)
     c = cls(Student.from_json(json_map["student"]),
             Book.from_json(json_map["book"]), json_map["created_at"],
             json_map["redeemed"], json_map["redemption_key"])
     return c
示例#2
0
 def _fetch_data(self):
     # We wrap it in a try/except so the app doesn't crash
     try:
         # Emits that fetching is about to occur
         self.network_state.emit({'state': NetworkState.active})
         # Gets the data in a GET request from the server
         r = requests.get(self.url)
         # Raises an exception if the response returned a non-200 status code
         r.raise_for_status()
         # Parses the response as JSON
         js = r.json()
         # Makes a list of books from the JSON
         self.books = [Book.from_json(j) for j in js["books"]]
         # And emits that the fetching has been completed, alongside the list of books
         # These will later be passed to the list model
         self.network_state.emit({
             "state": NetworkState.done,
             "data": self.books
         })
     # In the case something bad happens...
     except Exception as e:
         # ...print the error...
         print(e)
         # ...and emit it.
         self.network_state.emit({
             "state": NetworkState.error,
             "error": str(e)
         })
示例#3
0
 def _fetch_data(self):
   # We wrap it in a try/except in case it fails
   try:
     # Emit that we're fethcing data
     self.network_state.emit({'state': NetworkState.active})
     # Get the data from the server in a GET request
     r = requests.get(self.url)
     # Raise if the response code is anything non-200
     r.raise_for_status()
     # Parse the response as JSON
     js = r.json()
     # Parse the JSON as a Book
     self.book = Book.from_json(js["book"])
     # And emit the book while saying the fetching is done
     self.network_state.emit(
       {
         "state": NetworkState.done, 
         "data": self.book.__dict__
       }
     )
   # If it is not successful...
   except Exception as e:
     # ...show the error...
     print(e)
     # ...and emit it
     self.network_state.emit(
       {
         "state": NetworkState.error,
         "error": str(e)
       }
     )