Skip to content

Commit

Permalink
Merge pull request #345 from turingschool/zf-calling-3rd-party-APIs
Browse files Browse the repository at this point in the history
Calling 3rd party APIs
  • Loading branch information
zoefarrell authored Oct 12, 2023
2 parents 9a66e09 + ccd4387 commit c5a11bd
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
Binary file added assets/images/module5/week4/EnablingIssues.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions module5/labs/Week4/CallingThirdPartyAPIs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
layout: page
title: Calling Third Party APIs Lab
---

### Overview
For today's lab you will be creating a form that when submitted will create an issue on one of your Github Repos. This will require you to make a POST request and use the Github Issues API. [Here](https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue) is the API documentation you will need.

Fork and Clone [this](https://github.com/turingschool-examples/IssueSubmitter) starter code. You will see a form has already been created that calls a controller action. You're responsible for filling in the TODOs in the GitHubService and GitHubController to get the form working.

<aside class="instructor-notes" markdown="1">
<p><strong>Instructor Note</strong><br>
There is a branch called zf-fully-working that has all the necessary code to get this working minus the personal access token.</p>
</aside>
You will be working in traditional driver/navigator pair programming for this lab.

### Set-Up

#### Access Token
The Github API requires an API Personal Access Token to create issues.

To generate a token, while logged in to GitHub visit [https://github.com/settings/tokens](https://github.com/settings/tokens).

Generate a new token and make sure to give it `repo` access.

![Personal Access Token](/assets/images/module5/week4/PersonalAccessToken.png)

Copy your generated token, you will need it to make your POST requests in postman or from an application.

**DO NOT PUSH TO GITHUB IF YOUR ACCESS TOKEN IS HARDCODED**
If you accidentally do, reach out to an instructor who will help you delete that token so it doesn't work anymore.

#### Enabling Issues

You will use the token and repo from one person in the pair. That person can pick any repo that they own to use, but they need to enable issues.

To enable Issues for a repo, go to `Setting` and then check the box next to `Issues`. You should now see an `Issues` tab next to `Code`.

![Enabling Issues](/assets/images/module5/week4/EnablingIssues.png)

### Tips

* I recommend that you start by trying to make the POST request in Postman
* You will probably need to add the `UserAgent` header in addition to the Bearer authorization token.

### Hungry For More

If you get your form working, continue on this extra challenge, or one of your own.

* Hardcoding your Personal Access Token is very bad practice, can you figure out how to make this more private so it won't be committed to github?
99 changes: 99 additions & 0 deletions module5/lessons/Week4/CallingThirdPartyAPIs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
layout: page
title: Calling Third Party APIs
---

## Learning Goals
* Build familiarity with asynchronous code and Async/Await
* Understand how to make a GET request to a Third Party API
* Learn how to make a POST request

### Overview

In Mod 4 you were introduced to Third Party APIs and got a ton of practice calling various APIs from Postman and reading documentation. Maybe you remember working with the Github API, The Pokemon API, and the Cat API.

Today we're going to take the exciting leap of calling APIs, such as those listed above, from your applications! You will learn how to retrieve data from the API and then display it to the user.


### Preparation Review

In your preparation you deepened your understanding of asynchronous code.

<section class="call-to-action" markdown="1">
With your partner: Discuss your current understanding of asynchronous code and the keywords `Async` and `Await`. When we come back together you instructor will call on folks to share what they talked out.
</section>

<section class="call-to-action" markdown="1">
With your partner: Discuss why we might want to use asynchronous code when calling a third party API from our applications? When we come back together you instructor will call on folks to share what they talked out.
</section>

<section class="answer" markdown="1">
### Asynchronous Code Definitions

**Synchronous Code**: Synchronous code executes tasks sequentially, waiting for each to finish before proceeding, potentially causing resource inefficiencies during idle periods.

**Asynchronous Code**: Asynchronous code enables tasks to execute independently, improving resource utilization by allowing operations to progress without waiting for others to complete.

**Async Keyword**: In C#, the async keyword is used to mark a method as asynchronous. It indicates that the method contains asynchronous operations and may include one or more await expressions. Asynchronous methods can execute tasks concurrently without causing blocking.

**Await Keyword**: The await keyword is used within an asynchronous method to pause execution and asynchronously wait for the completion of an asynchronous operation. When await is used, it allows the method to temporarily yield control until the awaited task is finished executing. This non-blocking behavior enables efficient resource usage and helps maintain the application's responsiveness.

</section>


### Learning From a Tutorial
Follow [this](https://www.ezzylearning.net/tutorial/how-to-consume-third-party-web-apis-in-asp-net-core) tutorial individually up until the section **Managing HttpClient objects with IHttpClientFactory**. You will have roughly 1.5 hours, which should be enough time to thoughtfully work through it.

As you work through the tutorial, add comments to specify what each part of your code is doing. I also highly recommend that you type the code instead of copy/pasting for topics that feel new.

As with all tutorials, you have to expect it might not work perfectly and you might need to do some debugging to get your code to work! If you're stuck on something for more than 5 or 10 minutes reach out to your peers in code-help!

Deliverable✅: Once you have finished the tutorial, send the following two things in a message to your instructor:

* A screenshot of using your app to find holidays for a country other than the US.

* Your answer to the question: When is learning from a tutorial helpful and when is it not helpful? (This can be both about a particular tutorial and about where you are with your coding experience!)

It might be helpful to think about why we had you learn from a tutorial this week instead of last week for the auth lessons.


<aside class="instructor-notes" markdown="1">
<p><strong>Instructor Note</strong><br>
When working through the tutorial myself, I noticed a couple of things that might trip students up. <br>
1. They don't say where to put the interface and the service and they don't give the necessary imports. <br>
2. They have you add to a Startup.cs but our application only has a Program.cs and has a different way of adding services. This is how you need to do it for our application: builder.Services.AddSingleton<IHolidaysApiService, HolidaysApiService>(); <br>
3. The Index action in the HomeController is already injecting a logger, so you will now inject two things, or not the logger because we are not using that. <br>
4. Even following the tutorial perfectly it won't work, The first time the user visits the index it will not have a date or a state and the API request will throw an error. Not sure how this worked for the creator of the tutorial. One way to fix is the following where you add default parameters: Index(string countryCode = "US", int year = 2023)
</p>
</aside>


#### Additional Research
Then move on to thinking about and researching the following advanced topics:

* Why do you think this tutorial had you create an interface in addition to a service class?
* In your own words, why do we want to create one HTTPClient instead of one for every call?
* What best practices for error handling do you see in this tutorial?
* Were there any pieces of code in this tutorial that were new to you and you did research to figure out how they work?

Your instructor will ask for volunteers to share out answers to these questions after we work through part of the tutorial as a class.

<aside class="instructor-notes" markdown="1">
<p><strong>Instructor Note</strong><br>
Make sure to talk about not getting stuck in tutorials and never moving to your own problem solving.</p>
</aside>

### Building It Together

We're going to build through the key API request portion of this tutorial as a class.

<aside class="instructor-notes" markdown="1">
<p><strong>Instructor Note</strong><br>
Here is the starter code that includes the Model, Interface, and View. That way you can add the service and controller code as a class.</p>
</aside>

### Stretch Questions

* Regardless of what language and framework you are using, there are some parts of calling a third party API from an application that are always the same. What can you think of that might always be the same?
* Imagine that you're working in an API instead of an MVC application, why might it make sense to call a Third Party API from your application?
* What would we need to change about our API request if we were going to make a POST request instead of GET?
17 changes: 17 additions & 0 deletions module5/preparation/Week4/CallingThirdPartyAPIs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: page
title: Calling Third Party APIs Prep
---

### Review
Review what we did when we [built our own APIs](https://launch.turing.edu/module4/lessons/Week2/BuildAnAPI), specifically focus on GET and POST requests.

### Research
At the start of tomorrow's lesson we are also going to talk briefly about asynchronous vs synchronous code and the keywords `Async` and `Await`.

To prepare for that discussion, research the following questions and be prepared to share your answers:

* What is the difference between synchronous and asynchronous code?
* What do the keywords `Async` and `Await` do in C#?

✅ Deliverable: Share one of the resources you found helpful in learning about these topics on the Slack thread your instructor will create.

0 comments on commit c5a11bd

Please sign in to comment.