mercredi 16 janvier 2019

Should I use adapters to convert my object to JSON and terminal output?

I am making a simple weather API that gets as input a city name and a date and outputs the weather for that city and date:

class Weather:

    def __init__(self, city, date):
        self.city = city
        self.date = date

    def get_rain_amount(self):
        "code that finds the current weather"
         return "10 mm"

    def get_sun_amount(self):
        "code that finds the amount of sun"
        return "8 hours"

I also have a RESTFUL wrapper that accepts HTTP requests and returns Weather as JSON.

Where is it best to convert the Weather object into JSON? Should I do that in the RESTFUL wrapper layer or should I create an extra adapter layer that converts the Weather object into JSON?

I also want to have a terminal interface as well, so I think having an adapter class for each of the (RESTFUL and terminal) layers would be a good choice.

Aucun commentaire:

Enregistrer un commentaire