Chapter 4. Talking to servers

 

This chapter covers

  • Using decoders to validate and translate JSON
  • Handling descriptive errors with results
  • Communicating over HTTP with commands

We’ve made great progress! Users of our Photo Groove application can now do quite a bit:

  • Select a thumbnail to view a larger version
  • Choose Small, Medium, or Large thumbnail sizes
  • Click the Surprise Me! button to select a random thumbnail

Our manager is impressed with our progress but has even more in mind: “It’s time to take Photo Groove to the next level. The highest level. The cloud level. That’s right, we’re going to have Photo Groove start getting its photo information from our servers!”

As with last time, our manager has one more minor feature request: “There’s also going to be some metadata associated with each photo—specifically, download size and an optional caption. We can have it show those on top of the big photo, right?”

Sure, since you asked so nicely.

In this chapter, we’ll teach our application how to talk to servers. We’ll validate and translate JSON data, communicate over HTTP using The Elm Architecture, and reliably handle errors in client-server communication. Let’s get to it!

4.1. Preparing for server-loaded data

Now that we’re going to be loading our initial list of photos from the server, our data-modeling needs will change.

4.1.1. Modeling incremental initialization

Right now, our initialModel looks like this:

4.2. Fetching data from a server

4.3. Decoding JSON

Summary