Beginner’s Work With APIs
Tags: Codingbootcamp, Beginner, Learning Tips, Coding

One of the most interesting aspects of coding is the language. It has its own grammar and structure like any cultural language but also an uncomparable rigidity. That’s why it should be much easier to learn than say English or Korean. But working with APIs on a simple CLI project, I’ve discovered that coding can be just as full of inconsistencies and nuances.

Finding the Right One

Plenty of web APIs exist, ranging from professional to fun. Companies like Spotify can choose to make their data partially available to outside developers through easy-to-obtain authentication tokens and handy developer guides. Other APIs, such as this cat facts one, are written by one or two hobbyists to provide fun, easy information. I needed one that provides accurate information about individual bones of skeletons and it was not easy to find.

If accuracy and comprehensive information is your goal, you’d better have a popular subject. Unfortunately bones are not. With my current skills in coding (beginner), I’m completely dependent on what’s out there. Encyclopedia Britannica has wonderful, detailed and structured information about human skeletons but no API. I also found a company that makes medically accurate 3D models but with no outside access. Let’s not bring up the pages and pages of unusable sites.

The lesson that took me days to learn: project goals should be guided by what’s actually available. At least for now.

How to Read

Coding is a language, right? Reading code as a beginner is definitely not easy, even if it’s just some html data.

I ended up on Wolfram Alpha’s API for simple lists of skeletal components and helpful images. Not only are there certain stylistic differences between similar pages (all needed), but the information can be structured in ways that require a lot of manual parsing.

Reader Beware:

...baculum | basihyoid bone | basioccipital bone | distal phalanx of fifth digit of left forelimb | distal phalanx of fifth digit of left hindlimb | distal phalanx of fifth digit of right forelimb | distal phalanx of fifth digit of right hindlimb | distal phalanx of first digit of left forelimb | distal phalanx of first digit of right forelimb | distal phalanx of fourth digit of left forelimb | distal phalanx of fourth digit of left hindlimb | distal phalanx of fourth digit of right forelimb | distal phalanx of fourth digit of right hindlimb | distal phalanx of second digit of left forelimb | distal phalanx of second digit of left hindlimb | distal phalanx of second digit of right forelimb | distal phalanx of second digit of right hindlimb | distal phalanx of third digit of left forelimb | distal phalanx of third digit of left hindlimb | distal phalanx of third digit of right forelimb | distal phalanx of third digit of right hindlimb | dorsal sesamoid bone of fifth digit of left forelimb | dorsal sesamoid bone of fifth digit of left hindlimb | dorsal sesamoid bone of fifth digit of right forelimb | dorsal sesamoid bone of fifth digit of right hindlimb | dorsal sesamoid bone of fourth digit of left forelimb...

A query for “dog skeleton” brings back a list of 319 parts, in alphabetical order and no indication of subgroups. Each distal joint of each digit of each paw listed one after another, completely separated from the medial joints physically next to them.

Solution: Figure out the skeletal subsystems yourself and make API requests for the smallest categories possible. You’re a thinking coder! Not a robot!

Account for Differences

Oftentimes, pages are written by different coders (between or within). They try their best to keep a consistent API but this is where the human factor definitely comes in. The data I wanted, when in JSON format, sometimes sat in a hash and sometimes in an array. But consistently it was somewhere in a hash in an array in a hash in a hash…

def search(input, path)

count = 0

while count <= path.length - 1

if input.class == Array

input[0] == path[count] ?

(input = input[1]) && (count += 1) : input = input[0]

elsif input.class == Hash

input = input[path[count]]

count += 1

end

end

input

end

Tada! These few lines of (probably not the best) code uses the similarities I noticed between the pages to navigate the differences.

The key: Human brains are notably good at seeing patterns. Find the patterns and the computer brain can do the rest.

The Beginning of APIs

Each of these simple lessons took days of headaches for me to learn but there seems to be a proportional relationship between headache and knowledge-gain. So here’s to more headaches as we go! There is nothing to fear.