1. 程式人生 > >Teaching APIs with Girls Who Code

Teaching APIs with Girls Who Code

Teaching APIs with Girls Who Code

Chris Guthrie, Vice President, is a developer in BlackRock’s Aladdin Wealth team. Chris focuses on developing tools that empower decision makers and portfolio managers at wealth management firms to make the best decisions possible for their clients.

One of the reasons I joined BlackRock was the support and encouragement to volunteer my time and expertise to make a positive difference in our community. Stepping away from my usual work — building FinTech tools in Aladdin — to help others is also a great opportunity to meet colleagues outside my usual circle and gain new experiences.

Girls Who Code

Recently, I participated in the Girls Who Code Summer Immersion Program which BlackRock, as a corporate sponsor, hosts in our New York office. The program, now in its 3rd year at BlackRock, is a seven-week course for 20 rising high school junior women that teaches them the fundamentals of computer science and software development.

As part of the program, they get to hear from a variety of BlackRock people about technology topics and I was lucky enough to be invited to give a talk about APIs. This is the second year in a row that I’ve presented with Girls Who Code and it’s been a rewarding and fun process each time. I’ve had a passion for teaching and volunteering for some time, so this opportunity has really been a great fit for me. Their mission of closing the gender gap in tech is one that I’m happy and honored to be able to make an impact on.

Teaching APIs with Blackjack

This year, my colleague Warren Jackter, also a Girls Who Code volunteer, shared with me a simple to use web API called the Deck of Cards API. This is a free public API where one can create virtual deck(s) of cards, shuffle them, and draw cards from them — everything a budding programmer needs to create a card game without having to write a ton of code from scratch just to simulate a deck of cards. I thought this would be a perfect API to use in my talk as it’s a great example of an easy-to-use, useful API.

The task I came up with for the students was to build their very own Blackjack game. But, before we could get started, I had to teach the girls how to actually play Blackjack first, which was a blast.

Teaching the students how to play Blackjack

Once they got the hang of how to play, I wanted them to start thinking of how this could work as a computer program. To start, we did an exercise where I acted as the API and they had to give me commands to deal out a hand. After they understood that this was essentially how APIs work, we jumped into the Deck of Cards documentation. Here is how it works:

API as your Dealer

You can create and shuffle a single deck by simply using your browser to perform a GET request by visiting:

This will return some properties about your freshly minted new deck, including a deck_id property, which we’ll need later to perform further actions on the deck:

If you want to draw cards from this deck, you can simply pass that deck_id as a route parameter and the number of cards you want to draw as a query parameter into the draw API:

The API will return you the number of cards you requested from your deck (2 in this case):

By using this API along with some Blackjack “business” logic (I promise I’m not running a casino over here), the girls would be able to build the game themselves. The API provides a card rank/suit nomenclature in the response which we can use to display information about the card, or even display pictures of the cards using the image links provided. In an attempt to reduce some complexity of this exercise, I went with a command line program and thusly did not display the images. Since the girls were knee-deep in learning Python, I decided to join the fray and take a crack at building the solution in Python, which would be the first thing I’ve built in that language (I tend to live in Javascript and Java these days)

The result was something like this:

And then after you make a decision …

It’s just as difficult to win in this application as it is in real life!

The Assigment

After I built the working program, I removed the API calls and the result is what the girls cloned as their starting point. To get the program working again, they had to make their own HTTP requests using the highly popular Python ‘requests’ library. The ones I created for my example were pretty simple.

There was one for shuffling a new deck and returning a deck_id:

and one for drawing cards by deck_id and returning the cards drawn:

Conclusion

This was the students’ first exposure to HTTP status codes, HTTP verbs, and web APIs in general. It turned out to be a formidable challenge for them, but through the struggle (we’ve all been there) they learned a lot.

Understanding how APIs work is an integral part of software development. When I was first learning how to code, I vividly remember the possibilities of things I could build once I understood how APIs worked. My hope is that this simple introduction will end up inspiring some of the students to build their own cool apps and continue their journey into programming.

Originally published at rockthecode.io on October 15, 2018.