from twisted.internet import reactor, protocol

from hubbot.moduleinterface import ModuleInterface


class Echo(protocol.Protocol):
    """This is just about the simplest possible protocol"""

    def dataReceived(self, data):
        """As soon as any data is received, write it back."""
        self.transport.write(data)


class Healthcheck(ModuleInterface):
    port = 9999

    def __init__(self, bot):
        self.healthcheck_server = protocol.ServerFactory()
        self.healthcheck_server.protocol = Echo

        super().__init__(bot)

    def on_load(self):
        reactor.listenTCP(self.port, self.healthcheck_server)

    def on_unload(self):
        reactor.stopListening(self.port)

    def help(self, message):
        return f"Hosts an HTTP healthcheck server on port {self.port}."

from twisted.protocols import basic
from twisted.internet import protocol, reactor

from hubbot.moduleinterface import ModuleInterface


class HealthcheckProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        response_body = "All is well. Ish."
        self.sendLine("HTTP/1.0 200 OK".encode("UTF-8"))
        self.sendLine("Content-Type: text/plain".encode("UTF-8"))
        self.sendLine(f"Content-Length: {len(response_body)}\n".encode("UTF-8"))
        self.transport.write(response_body)
        self.transport.loseConnection()


class Healthcheck(ModuleInterface):
    port = 9999

    def __init__(self, bot):
        self.healthcheck_server = protocol.ServerFactory()
        self.healthcheck_server.protocol = HealthcheckProtocol

        super().__init__(bot)

    def on_load(self):
        reactor.listenTCP(self.port, self.healthcheck_server)

    def on_unload(self):
        reactor.stopListening(self.port)

    def help(self, message):
        return f"Hosts an HTTP healthcheck server on port {self.port}."

Write a slightly less dumb protocol?
Write a slightly less dumb protocol?

+ from twisted.protocols import basic
- from twisted.internet import reactor, protocol
+ from twisted.internet import protocol, reactor
  
  from hubbot.moduleinterface import ModuleInterface
  
  
+ class HealthcheckProtocol(basic.LineReceiver):
- class Echo(protocol.Protocol):
-     """This is just about the simplest possible protocol"""
- 
-     def dataReceived(self, data):
+     def lineReceived(self, line):
-         """As soon as any data is received, write it back."""
+         response_body = "All is well. Ish."
+         self.sendLine("HTTP/1.0 200 OK".encode("UTF-8"))
+         self.sendLine("Content-Type: text/plain".encode("UTF-8"))
+         self.sendLine(f"Content-Length: {len(response_body)}\n".encode("UTF-8"))
-         self.transport.write(data)
+         self.transport.write(response_body)
+         self.transport.loseConnection()
  
  
  class Healthcheck(ModuleInterface):
      port = 9999
  
      def __init__(self, bot):
          self.healthcheck_server = protocol.ServerFactory()
-         self.healthcheck_server.protocol = Echo
+         self.healthcheck_server.protocol = HealthcheckProtocol
  
          super().__init__(bot)
  
      def on_load(self):
          reactor.listenTCP(self.port, self.healthcheck_server)
  
      def on_unload(self):
          reactor.stopListening(self.port)
  
      def help(self, message):
          return f"Hosts an HTTP healthcheck server on port {self.port}."
  
Write a slightly less dumb protocol?
## Code Before:
from twisted.internet import reactor, protocol

from hubbot.moduleinterface import ModuleInterface


class Echo(protocol.Protocol):
    """This is just about the simplest possible protocol"""

    def dataReceived(self, data):
        """As soon as any data is received, write it back."""
        self.transport.write(data)


class Healthcheck(ModuleInterface):
    port = 9999

    def __init__(self, bot):
        self.healthcheck_server = protocol.ServerFactory()
        self.healthcheck_server.protocol = Echo

        super().__init__(bot)

    def on_load(self):
        reactor.listenTCP(self.port, self.healthcheck_server)

    def on_unload(self):
        reactor.stopListening(self.port)

    def help(self, message):
        return f"Hosts an HTTP healthcheck server on port {self.port}."

## Instruction:
Write a slightly less dumb protocol?
## Code After:
from twisted.protocols import basic
from twisted.internet import protocol, reactor

from hubbot.moduleinterface import ModuleInterface


class HealthcheckProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        response_body = "All is well. Ish."
        self.sendLine("HTTP/1.0 200 OK".encode("UTF-8"))
        self.sendLine("Content-Type: text/plain".encode("UTF-8"))
        self.sendLine(f"Content-Length: {len(response_body)}\n".encode("UTF-8"))
        self.transport.write(response_body)
        self.transport.loseConnection()


class Healthcheck(ModuleInterface):
    port = 9999

    def __init__(self, bot):
        self.healthcheck_server = protocol.ServerFactory()
        self.healthcheck_server.protocol = HealthcheckProtocol

        super().__init__(bot)

    def on_load(self):
        reactor.listenTCP(self.port, self.healthcheck_server)

    def on_unload(self):
        reactor.stopListening(self.port)

    def help(self, message):
        return f"Hosts an HTTP healthcheck server on port {self.port}."
