Chapter 37: a status decided late, built

When is an HTTP status decided? Usually before the body - but a body can discover a problem. Chapter 37's LateStatus builds a response class whose status is read after its content is made, and a route that returns it. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-late-status.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.notesservice.latestatus

  defines constant
    LATE_PORT <- 8084

    STATUS_OK <- 200

    STATUS_NOT_FOUND <- 404

  defines class

    LateResponse with trait of HTTPResponse
      code as Integer: STATUS_OK

      override status() as pure
        <- rtn as Integer: code

      override content()
        <- rtn as Bytes: Bytes()

        code := STATUS_NOT_FOUND
        rtn: Bytes().withUtf8("there is no such note")

      override contentType() as pure
        <- rtn as String: "text/plain"

      default operator ?

  defines service

    Late for :/late open

      check() as GET for :/
        <- response as HTTPResponse: LateResponse()

  defines application

    LateApp
      register Late()

  defines program

    ServeLate() with application of LateApp
      try
        -> server <- HTTP(NetworkProperties(port: LATE_PORT, localOnly: true))
        server.serve()

//EOF

Step 1: Constants

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

LATE_PORT is the constant's name and 8084 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: LATE_PORT is the constant's name and 8084 its value - a literal in the Value field, from which the type is inferred

The preview shows LATE_PORT about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows LATE_PORT about to be written

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

STATUS_OK is the constant's name and 200 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: STATUS_OK is the constant's name and 200 its value - a literal in the Value field, from which the type is inferred

The preview shows STATUS_OK about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows STATUS_OK about to be written

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

STATUS_NOT_FOUND is the constant's name and 404 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: STATUS_NOT_FOUND is the constant's name and 404 its value - a literal in the Value field, from which the type is inferred

The preview shows STATUS_NOT_FOUND about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows STATUS_NOT_FOUND about to be written

Step 2: LateResponse — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

LateResponse is the class's name. It is a response that can change its mind: its status is read after its content is made. Choose Preview.

The EK9 IDE just before choosing Preview: LateResponse is the class's name

The preview shows LateResponse about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows LateResponse about to be written

Select the LateResponse line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the LateResponse line and, above it, type the blank line the book opens the block with

Right-click the LateResponse line and choose Add Field…

The EK9 IDE once this is done: Right-click the LateResponse line and choose Add Field…

Type code as Integer: STATUS_OK. The status it will report, until changed. Choose OK.

The EK9 IDE just before choosing OK: Type code as Integer

The preview shows code added - and default operator ? with it, LateResponse's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows code added - and default operator ? with it, LateResponse's first field

Right-click the LateResponse line and choose Adopt Trait…

The EK9 IDE once this is done: Right-click the LateResponse line and choose Adopt Trait…

Type HTTPResponse as the trait - Implement method stubs is already chosen - and choose OK.

The EK9 IDE just before choosing OK: Type HTTPResponse as the trait - Implement method stubs is already chosen - and choose OK

Adopt Trait changes the open file in the editor, not yet saved. Choose File > Save.

The EK9 IDE once this is done: Adopt Trait changes the open file in the editor, not yet saved

Right-click the LateResponse line and choose Add Method…

The EK9 IDE once this is done: Right-click the LateResponse line and choose Add Method…

status is the method's name. It is HTTPResponse's own method. Set Return type to Integer, tick pure and override. Choose Apply.

The EK9 IDE just before choosing Apply: status is the method's name

Select the stub and type the answer: the code. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the answer

Right-click the LateResponse line and choose Add Method…

The EK9 IDE once this is done: Right-click the LateResponse line and choose Add Method…

content is the method's name. It is the body. Set Return type to Bytes and tick override. Choose Apply.

The EK9 IDE just before choosing Apply: content is the method's name

Select the stub and type the body - and, while making it, the late decision: the status becomes not found. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the body - and, while making it, the late decision

Right-click the LateResponse line and choose Add Method…

The EK9 IDE once this is done: Right-click the LateResponse line and choose Add Method…

contentType is the method's name. Set Return type to String, tick pure and override. Choose Apply.

The EK9 IDE just before choosing Apply: contentType is the method's name

Select the stub and type the answer. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the answer

Step 3: Late — a service

Choose Code > New Construct > Service…

The EK9 IDE once this is done: Choose Code > New Construct > Service…

Late is the service's name and /late the base URI every route sits under. One route, with no path variable. The Routes table holds check, GET /. Choose Preview.

The EK9 IDE just before choosing Preview: Late is the service's name and /late the base URI every route sits under

The preview shows Late about to be written, with its route, answering with an HTTPResponse the tool wrote. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Late about to be written, with its route, answering with an HTTPResponse the tool wrote

Select the Late line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the Late line and, above it, type the blank line the book opens the block with

The tool wrote a response of its own; the book returns the LateResponse. Select it, from its first line down to the default operator, and type the one line. Saves when typed.

The EK9 IDE once this is done: The tool wrote a response of its own; the book returns the LateResponse

Step 4: LateApp — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

LateApp is the application's name. It registers the service. The Registrations table holds Late. Choose Preview.

The EK9 IDE just before choosing Preview: LateApp is the application's name

The preview shows LateApp about to be written, with its register line. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows LateApp about to be written, with its register line

Select the LateApp line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the LateApp line and, above it, type the blank line the book opens the block with

Step 5: ServeLate — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

ServeLate is the program's name. Leave Application empty - a later step says why. Choose Preview.

The EK9 IDE just before choosing Preview: ServeLate is the program's name

The preview shows ServeLate about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ServeLate about to be written

Select the ServeLate line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the ServeLate line and, above it, type the blank line the book opens the block with

The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound. Select the stub, from the program's header down to its //AI line, and type the header with its application and the body that serves, on this machine only. Saves when typed.

The EK9 IDE once this is done: The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound

The result

LateStatus.ek9 now reads exactly as chapter 37 of EK9 in Depth shows it. The response answers 200 until its content is made, and the content makes it 404 - the status is read afterwards. Built, not run: the program serves until sent TERM.

Where this lesson comes from