|
| 1 | +--- |
| 2 | +title: Best Buy API Playground - An old version of the Best Buy API - Practice API |
| 3 | +description: Bes tBuy API Playground is an old version of the Best Buy API. Notable for comprehensive query filter operations. |
| 4 | +showads: true |
| 5 | +--- |
| 6 | + |
| 7 | +In addition to our [API Challenges](/gui/challenges) you should practice on as many sites as possible. Try [BestBuy API Playground](https://github.com/BestBuy/api-playground). |
| 8 | + |
| 9 | +# Best Buy API Playground - A Practice GET API |
| 10 | + |
| 11 | +The Best Buy API Playground is a node application which implements an older version of the Best Buy API. It comes pre-populated with production-like data. |
| 12 | + |
| 13 | +Since this is a practice version of the API, update and delete operations are enabled in addition to the expected `GET` verbs. These would not be available in the production version of the API. |
| 14 | + |
| 15 | +Summary: |
| 16 | + |
| 17 | +- Best Buy API Playground is a full API that can be run locally using Node or Docker |
| 18 | +- Full range of CRUD operations is supported using `GET`, `POST`, `PATCH`, `DELETE` |
| 19 | +- Large set of data is provided |
| 20 | +- Swagger UI is available |
| 21 | +- Open API Definition is downloadable |
| 22 | + |
| 23 | +## Running The Application Using Node |
| 24 | + |
| 25 | +If you have node installed then the API can best started by cloning the repository and using `npm`. |
| 26 | + |
| 27 | +Full instructions are on the [GitHub page](https://github.com/BestBuy/api-playground), copied here for ease of reference: |
| 28 | + |
| 29 | +``` |
| 30 | +git clone https://github.com/bestbuy/api-playground/ |
| 31 | +cd api-playground |
| 32 | +npm install |
| 33 | +npm start |
| 34 | +``` |
| 35 | + |
| 36 | +The Best Buy API Playground will now be accessible on `http://localhost:3030` |
| 37 | + |
| 38 | +I personally prefer to run the application using Docker as described below. |
| 39 | + |
| 40 | +## Running The Application Using Docker |
| 41 | + |
| 42 | +Create the Docker image locally: |
| 43 | + |
| 44 | +``` |
| 45 | +git clone https://github.com/BestBuy/api-playground |
| 46 | +cd api-playground |
| 47 | +docker build --tag bestbuy . |
| 48 | +``` |
| 49 | + |
| 50 | +Then run the Docker image, making sure you map the exposed port `3030`: |
| 51 | + |
| 52 | +``` |
| 53 | +docker run -d --name=bestbuy-api-playground -p 3030:3030 "bestbuy" |
| 54 | +``` |
| 55 | + |
| 56 | +I map the port to `3030` on localhost to make the Swagger documentation URL work with the default values. |
| 57 | + |
| 58 | +## Documentation End Points |
| 59 | + |
| 60 | +You can then access: |
| 61 | + |
| 62 | +- the basic api documentation at |
| 63 | + - `http://localhost:3030` |
| 64 | +- A Swagger UI Front End at |
| 65 | + - `http://localhost:3030/docs/` |
| 66 | +- A Swagger API Definition file at |
| 67 | + - `http://localhost:3030/swagger.json` |
| 68 | +- A PostMan collection has been bundled with the app at |
| 69 | + - `http://localhost:3030/postman/API.postman_collection.json` |
| 70 | +- The Queries documentation shows some examples of queries to use on the API. This is useful because the GET API supports a complex set of query filters using SQL-like operators. |
| 71 | + - `http://localhost:3030/queries` |
| 72 | + |
| 73 | +## API Endpoints |
| 74 | + |
| 75 | +The application is bundled with sample data so you will be able to query the data on the back end immediately. |
| 76 | + |
| 77 | +The main endpoints are: |
| 78 | + |
| 79 | +- `/products` - products available in the database. |
| 80 | +- `/categories` - product categories and their subcategories/path. |
| 81 | +- `/stores` - returns a list of Best Buy store locations. |
| 82 | +- `/services` - returns a list of services available at Best Buy stores. |
| 83 | +- `/version` - returns the current version of the application. |
| 84 | +- `/healthcheck` - returns information about the application's health. |
| 85 | + |
| 86 | + |
| 87 | +## Exercises |
| 88 | + |
| 89 | +### Exercise - `GET` all the main endpoints |
| 90 | + |
| 91 | +- Issue a GET request on each of the top level endpoints listed above |
| 92 | +- Initially do this using the Swagger UI `http://localhost:3030/docs/` |
| 93 | +- Repeat the exercise using a REST API Client to gain more control. You can download the Swagger file to pre-populate the requests in your REST Client. |
| 94 | + |
| 95 | +### Exercise - Explore the Example Query Formats |
| 96 | + |
| 97 | +- Use the list of Query formats on the Query Example page |
| 98 | + - `http://localhost:3030/queries` |
| 99 | +- Initially do this by clicking on the links |
| 100 | +- Copy the URLs into your REST client and repeat the exercise |
| 101 | +- Amend Query parameters to explore the results |
| 102 | + |
| 103 | +### Exercise - Create and Update the Data |
| 104 | + |
| 105 | +- Explore the endpoints to create and amend the data in the backend |
| 106 | +- Use `OPTIONS` do check if the endpoints honour the verbs listed |
| 107 | + |
| 108 | +### Exercise - Does the API only support JSON? |
| 109 | + |
| 110 | +- The response format can sometimes be influenced using the `accept` header to tell the server that the client will accept a different format for the response. |
| 111 | +- By default the server responds with `JSON`. |
| 112 | +- What happens if you ask the server for `application/xml`? |
| 113 | +- What other formats could you ask for? |
| 114 | + |
| 115 | +## Some Observations |
| 116 | + |
| 117 | +### Create and Updating Data |
| 118 | + |
| 119 | +- When creating data I noticed that the system was fussy around headers, but in a misleading way. Sending through a valid payload in 'text/plain' format I received the information that it `should have required property 'name'` rather than content-type not accepted. |
| 120 | +- The server checks payloads for extra and unexpected fields. |
| 121 | +- Server responds with all validations that the request fails, not just the first one, which is useful for debugging requests. |
| 122 | +- Duplicate fields are allowed with the last one being accepted |
| 123 | +- JSON validation fails with a `500` error rather than a `400` error suggesting that a general exception is thrown but not trapped to return a `400` (client) error. NOTE: the swagger documentation suggests that a `400` will be returned |
| 124 | +- The swagger documentation did not show the field validation rules so I experimented to find validation lengths. And then used an online counterstring tool to check the length calculations [counterstring generator](https://eviltester.github.io/TestingApp/apps/counterstrings/counterstrings.html) |
| 125 | +- Service names do not need to be unique |
| 126 | +- Trying to amend an existing item using `POST` returned a `404` rather than a `405 Method Not Allowed`, which also contradicts the methods listed when I asked for `OPTIONS` |
| 127 | + |
| 128 | +### `accept` Header |
| 129 | + |
| 130 | +- The API examples are all `JSON` so I tried asking the API for `application/xml` I received a `500` error. I rather expected a `400` error because the request was asking for something the API did not support, rather than the server throwing a `500` General Error. |
| 131 | +- Since the `xml` request failed I wondered what would happen with a `text/html` response and I found it interesting that the server did actually respond with an error in HTML format. |
0 commit comments