Skip to main content

6. Write the API consumer

The Consumer

Time to create our consumer code base. For the sake of simplicity, we're going to focus only on the bits of our application that communicate to the provider. In our project, they are:

  • The model (the Product class), representing the data returned from the Product API: example-bi-directional-consumer-mountebank/src/api.js
  • The API client (the ProductApiClient) which will be responsible for making the HTTP calls to the Product API and returning an internal representation of an Product: example-bi-directional-consumer-mountebank/src/product.js

Install the dependencies for the project: npm i

Product Model

src/product.js
loading...

Product API Client

Here is a version of our API client code. This code is responsible for fetching products from the API, and returning a Product:

src/api.js
loading...

This class, and specifically the getProduct() and getAllProducts methods, will be the target of our contract tests.

info

You can pick your consumer now

Fork and clone the consumer

  1. Fork the example-bi-directional-consumer-mountebank project in to your own Github account (click the 'Fork' button in the top right).

  2. Clone the repositories on to your local machine.

    git clone git@github.com:<YOUR_GITHUB_USERNAME>/example-bi-directional-consumer-mountebank.git
  3. Install the dependencies.

    npm install

Check

Before moving to the next step, check the following:

  1. You are in the correct directory cd /root/example-bi-directional-consumer-mountebank
  2. You have run npm i and the dependencies have been installed
  3. You have studied and understood the Product class: example-bi-directional-consumer-mountebank/src/product.js
  4. You have studied and understood the API client: example-bi-directional-consumer-mountebank/src/api.js