Solution for Chapter 2: Understanding HTTP, REST, and APIs

Design and Build Great Web APIs — by Mike Amundsen (113 / 127)

The Pragmatic Programmers
The Pragmatic Programmers
5 min readApr 28, 2021

--

👈 1: tarted with API First | TOC | 3: APIs 👉

The task in this exercise is to move our services folder (and all the API calls stored there) into the project repository we created in chapter 2 (see Chapter Exercise).

You can use the before folder for this chapter as a clean start for this exercise. It picks up where our last exercise left off.

Depending on whether you followed along in chapter 2 with the process of creating your onboarding project folder and subsequent changes, you may have some of this exercise already completed. To keep things clear, we’ll start from the beginning by creating a new onboarding folder, making it a git repository, and then copying the services folder into the repository and committing the changes.

Here’s a step-by-step solution.

Create Your Local Git Repo

First, create your onboarding project folder and change directories into that new folder:

​ ​mca@mamund-ws:~/$ ​​mkdir​​ ​​onboarding​
​ ​mca@mamund-ws:~/$ ​​cd​​ ​​onboarding​
​ ​mca@mamund-ws:~/onboarding$​

Next, make this new empty folder a git repository:

​ ​mca@mamund-ws:~/onboarding$ ​​git​​ ​​init​
​ Initialized empty Git repository in /home/mca/onboarding/.git/
​ ​mca@mamund-ws:~/onboarding$​

Next, let’s add a README.md file to the repo. It’s always a good idea to use README files to help remember what’s in the repo and why it was created. For this example, we can create a simple text file that contains the following text and save it to disk as a file named “README.md”:

​ ​# Onboarding API Project​
​ This project contains files related the BigCo Onboarding API.

Now do a git status to check the status of the repo:

​ ​mca@mamund-ws:~/onboarding$ ​​git​​ ​​status​
​ On branch master

​ Initial commit

​ Untracked files:
​ (use "git add <file>..." to include in what will be committed)

​ README.md

​ nothing added to commit but untracked files present (use "git add" to track)
​ ​mca@mamund-ws:~/onboarding$​

You’re ready to make your first commit to the repo:

​ ​mca@mamund-ws:~/onboarding$ ​​git​​ ​​add​​ ​​README.md​
​ ​mca@mamund-ws:~/onboarding$ ​​git​​ ​​commit​​ ​​-m​​"added README"​
​ [master (root-commit) ce69677] added README
​ 1 file changed, 3 insertions(+)
​ create mode 100644 README.md
​ ​mca@mamund-ws:~/onboarding$​

Move the Services Folder into the Repo

Now that you have an up-to-date repository, you’re ready to move the services folder (and all its contents) into that repo and check the repo’s status:

​ ​mca@mamund-ws:~/onboarding$ ​​mv​​ ​​../services​​ ​​.​
​ ​mca@mamund-ws:~/onboarding$ ​​ls​
​ README.md services
​ ​mca@mamund-ws:~/onboarding$​
​ ​mca@mamund-ws:~/onboarding$​​ ​​git​​ ​​status​
​ On branch master
​ Untracked files:
​ (use "git add <file>..." to include in what will be committed)

​ services/

​ nothing added to commit but untracked files present (use "git add" to track)
​ ​mca@mamund-ws:~/onboarding$​

Finally, you can commit the changes (the new folder and its contents) into the Git repository:

​ ​mca@mamund-ws:~//onboarding$ ​​git​​ ​​add​​ ​​services/​
​ ​mca@mamund-ws:~//onboarding$ ​​git​​ ​​status​
​ On branch master
​ Changes to be committed:
​ (use "git reset HEAD <file>..." to unstage)

​ new file: services/account-list.json
​ new file: services/account-record.json
​ new file: services/activity-list.json
​ new file: services/activity-record.json
​ new file: services/company-home.json
​ new file: services/company-list.json
​ new file: services/company-record.json

​ ​mca@mamund-ws:~//onboarding$ ​​git​​ ​​commit​​ ​​-m​​"add services folder"​
​ [master df445e6] add services folder
​ 7 files changed, 536 insertions(+)
​ create mode 100644 services/account-list.json
​ create mode 100644 services/account-record.json
​ create mode 100644 services/activity-list.json
​ create mode 100644 services/activity-record.json
​ create mode 100644 services/company-home.json
​ create mode 100644 services/company-list.json
​ create mode 100644 services/company-record.json
​ ​mca@mamund-ws:~/onboarding$​

Push Your Local Repo to GitHub

Our last task is to push this local repo up to https://github.com to make it publicly available for others. To do that we need to log into your GitHub account, create a repository there, copy the repository address onto our clipboard, and then add that public address to our local repo. Then we can use the git push command to copy our local repo contents to GitHub.

First, log into your GitHub account and click New to start a new repository. Or you can just navigate to https://github.com/new.

Next, fill in the repository name (onboarding) and add a short description (public copy of local onboarding repo). Leave all the other entries as is and click “Create repository,” as shown in the following screenshot:

images/http/exercise-github-new.png

Once that’s created, you’ll see a response screen that contains a lot of text on how to start populating your public repository. For this solution, copy the two lines that appear under the …or push an existing repository from the command line option, as shown in the screenshot. (You should see a small clipboard icon near that text to make it easy to copy the commands.)

images/http/exercise-github-push.png

Paste the commands from your clipboard into your command window and press the Enter key to commit the git push from your local repository to the public one. Your command line should look something like this:

​ mca@mamund-ws:~/onboarding$ git remote add origin \
​ git@github.com:mamund/onboarding.git
​ mca@mamund-ws:~/onboarding$ git push -u origin master
​ Counting objects: 13, done.
​ Delta compression using up to 4 threads.
​ Compressing objects: 100% (12/12), done.
​ Writing objects: 100% (13/13), 2.06 KiB | 0 bytes/s, done.
​ Total 13 (delta 4), reused 0 (delta 0)
​ remote: Resolving deltas: 100% (4/4), done.
​ To git@github.com:mamund/onboarding.git
​ * [new branch] master -> master
​ Branch master set up to track remote branch master from origin.
​ mca@mamund-ws:~/onboarding$

You can check your work by refreshing your project page (from the previous step). You should then see something that looks like this:

images/http/exercise-github-onboarding.png

👈 1: tarted with API First | TOC | 3: APIs 👉

Design and Build Great Web APIs by Mike Amundsen can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Unlisted

--

--

The Pragmatic Programmers
The Pragmatic Programmers

Published in The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.

The Pragmatic Programmers
The Pragmatic Programmers

Written by The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.

No responses yet