

from twisted.web import resource
from twisted.web.server import NOT_DONE_YET


class LookupSimulatorResource (resource.Resource):
    def __init__(self, db):
        # FIXME - db is some simple memory structure holding info;
        # the details will solidfy soon.  This resource reads from
        # this structure.

        resource.Resource.__init__(self)
        self._db = db

    def render_GET(self, request):
        # FIXME: This is not implemented yet.
        request.setResponseCode(500, 'NOT IMPLEMENTED')
        request.finish()
        return NOT_DONE_YET


import json

from twisted.web import resource
from twisted.web.server import NOT_DONE_YET


class LookupSimulatorResource (resource.Resource):
    def __init__(self, db):
        """db is a dict mapping { fqdn -> other_stuff }; inserts come from mlabsim.update."""
        resource.Resource.__init__(self)
        self._db = db

    def render_GET(self, request):
        if request.args['match'] == ['all'] and request.args.get('format', ['json']) == ['json']:
            request.setResponseCode(200, 'ok')
            request.write(json.dumps(self._db.values(), indent=2, sort_keys=True))
            request.finish()
        else:
            request.setResponseCode(400, 'invalid')
            request.finish()
        return NOT_DONE_YET

Implement the current ``GET /ooni`` api.
Implement the current ``GET /ooni`` api.

  
+ import json
  
  from twisted.web import resource
  from twisted.web.server import NOT_DONE_YET
  
  
  class LookupSimulatorResource (resource.Resource):
      def __init__(self, db):
+         """db is a dict mapping { fqdn -> other_stuff }; inserts come from mlabsim.update."""
-         # FIXME - db is some simple memory structure holding info;
-         # the details will solidfy soon.  This resource reads from
-         # this structure.
- 
          resource.Resource.__init__(self)
          self._db = db
  
      def render_GET(self, request):
-         # FIXME: This is not implemented yet.
+         if request.args['match'] == ['all'] and request.args.get('format', ['json']) == ['json']:
-         request.setResponseCode(500, 'NOT IMPLEMENTED')
+             request.setResponseCode(200, 'ok')
+             request.write(json.dumps(self._db.values(), indent=2, sort_keys=True))
-         request.finish()
+             request.finish()
+         else:
+             request.setResponseCode(400, 'invalid')
+             request.finish()
          return NOT_DONE_YET
  
Implement the current ``GET /ooni`` api.
## Code Before:


from twisted.web import resource
from twisted.web.server import NOT_DONE_YET


class LookupSimulatorResource (resource.Resource):
    def __init__(self, db):
        # FIXME - db is some simple memory structure holding info;
        # the details will solidfy soon.  This resource reads from
        # this structure.

        resource.Resource.__init__(self)
        self._db = db

    def render_GET(self, request):
        # FIXME: This is not implemented yet.
        request.setResponseCode(500, 'NOT IMPLEMENTED')
        request.finish()
        return NOT_DONE_YET

## Instruction:
Implement the current ``GET /ooni`` api.
## Code After:

import json

from twisted.web import resource
from twisted.web.server import NOT_DONE_YET


class LookupSimulatorResource (resource.Resource):
    def __init__(self, db):
        """db is a dict mapping { fqdn -> other_stuff }; inserts come from mlabsim.update."""
        resource.Resource.__init__(self)
        self._db = db

    def render_GET(self, request):
        if request.args['match'] == ['all'] and request.args.get('format', ['json']) == ['json']:
            request.setResponseCode(200, 'ok')
            request.write(json.dumps(self._db.values(), indent=2, sort_keys=True))
            request.finish()
        else:
            request.setResponseCode(400, 'invalid')
            request.finish()
        return NOT_DONE_YET
