What's up, everyone?
So in this video, I want to show you how you can use the currency converter that we have built in the
last two videos with API.
So you don't need to use the code that you have seen in the database.
Example, you actually don't even have to have seen the database example because we are going to use
the code that we created in the static example.
So in the static WP F currency converter for example, which we use static data and we're going to extend
that in order for it to use data from an API, which means we get data from the internet and we use
that data in order to have currency conversion at its finest with the latest currency values, the latest
conversion rates.
And that's basically the idea behind this.
You can, of course, check out the article.
We created the complete article for this.
And yeah, you can basically just check it out.
You can find all of the code in there, a bunch of explanations and all of that.
The.
The API that we're going to use is from open exchange rates and this is pretty cool.
This website offers exchange rates and we can use up to 1000 calls per month for free.
So this is great for learning purposes, but as soon as you want to actually use it for a service that
you want to sell, for example, you would have to pay.
But as I said, it's for free for testing and that's exactly what we need.
So you can go to pricing every time if you are such a paid page and just see what they have here.
And you can see that there is a developer plan, enterprise plan, unlimited plan, but that's not what
is relevant for us.
What we are interested in is this free plan, so provides hourly updates with base currency USD and
up to 1000 requests per month, which is great.
So you go to free plan here and then you have to register for you to be able to use this.
Okay.
So if you want to follow along, you can just register here and once you have registered.
You will need to have your app ID so you will need this ID here.
And well, this ID is what we are going to require to make API calls because now we can make API calls
and basically get data back.
And how do we get the data back?
Well, we get it in this format, so we get it in the JSON format.
This is the JSON format.
And JSON is a very useful format to display data and a readable format with just text.
So basically it breaks it down.
So this is not a data table as you would usually see data in many cases, but this is really in a text
format which you still can read quite well.
Okay.
So basically the data that we're going to get is a disclaimer license timestamp base and the rates and
the rates itself are a bunch of different rates and the base will be US dollars.
So for a US dollar you will get a 3.6 whatever a EAD stands for, I don't know, then Australian dollars
you will get 139 for example.
Okay, so these are going to be the values that we will get.
And what we can do is we can use that in order to basically adjust our program so that it allows us
to get the latest currencies or currency ratios ratios so that we don't have to manually enter them
in our code and we don't have to have an extra database tab as we did in the last video where we need
to enter the manually in the program.
All right.
So let's go to our project.
And this is the static project.
And as I said, if you haven't watched the video, definitely check out the video where I show you how
to build this, the WPF part and all of that, because I'm going to cover the WPF part in this video
at all.
I'm only going to focus on the API side of things.
And for this to work, what we will need is we'll need extra classes because we.
Get the information in a certain format, right.
And the format that we get it with, well, as I said, is this format here.
So you can always go to the documentation and you will find it a little more readable.
Here you can see this is the HTTP request that you need to do to get this kind of format or this kind
of data.
You see, we will need to make a request to this web page with the app ID and then it's basically the
app ID that you have here in your back end.
So you need to use your own app ID for this to work because mine will be limited to 1000 and if anyone
is going to use it, it will.
Well, if some of you are going to use it, then it will be misused so often that it will not work.
So we will you will need your own one.
But then once you have done that, what you can get is this kind of information and this kind of information.
Well, I'm going to call this one root.
So I'm going to create a class which is going to be route, which is basically going to be my type,
which allows me to get all the data.
And then inside of route I'll have properties which can also be classes itself.
So for example, rates will be another class.
So I'm going to create a class called rates, but then I can just create a long for timestamp because
this is a long value.
I could just create a string for US dollars and so forth.
So let's just set it up step by step.
Let's just start with the route class.
So I'm just going to create a new class inside of my main window class.
So here I'm going to go ahead and create this public class.
Root.
Okay, and in there I have a rate called rates and I have a long called time stamp.
And then I could get, for example, the license.
So let's get the license as well.
Let's go in here.
And I'm just going to focus on the easy ones license.
So public string license.
And then now we need to get the rates.
But as I said, rates are a little more complex because there are a bunch of different rates.
Well, I'm going to create an extra class, which I'm going to call rate.
And what's important is that I have the names here, which are exactly the same names as we have them
here.
So as well, the names that will be made available to us, these are not the only ones.
There are more.
You can get more details.
With supported currencies.
So you can go here up to the documentation and go to supported currencies and here you will see all
of the currencies that are supported.
And I'm not going to use all of them.
I'm just going to use some of them.
But what's important is that the names that you are using in your project, they should be available
there as well.
So Indian rupee, Japanese yen, US dollars, I don't know what that is.
Euro Canadian dollars, I guess, and so forth.
Okay.
Danish crown, Czech crown.
PHP Well, that's just a cool language, I guess a programming language.
But in this case it's more than that.
It's actually, uh, also a rate.
Okay, so I have all of these properties with getters and setters, okay?
So I have this public class rate which has all of these setters and getters, and you will already see
that each time that you, for example, want to get data which is in this format.
So this is the root class which has a bunch of properties, but then this is a property that can have
multiple properties itself, which is a class.
So we create this class rate which will be called rates, which is exactly the same as it is here.
That's important.
So you need to make sure that you use the same names in your program with this one here.
So I'm just going to get rates, timestamp and license now and rate will just get these properties here,
which means just these currencies or the values that are available online for these currencies.
And now that we have that out of the way, we can start getting values, we can get the data from the
Web.
And I'm just going to create an extra method that will get that data for us.
And let me quickly introduce this method to you.
I'm going to paste it in here and actually let's do it after main window and let's look over it.
So what I'm using here is an async task.
So let me fix this by using sys threading tasks.
Okay.
So what this allows us to do is to do things in parallel.
There are multiple threads that we can activate.
So using tasks what what is this allows us to do is to run asynchronous operations.
And whenever you get data from the Internet, you should do that asynchronously because otherwise,
while you wait for the response, you would basically freeze your program.
So it would freeze the whole main thread and the application would freeze and it wouldn't work correctly.
That's why you use async tasks.
You use this keyword async and you use this task class and we want to get a result which is off type
route.
So if you want to know more about task, you can always check out the documentation.
You will see.
Task gives us a generic bag of type t result.
Well, t result.
What is T result.
Well, the type of the result produced by this task.
And what is the result that I want to be produced by this task.
When I get the data well I want to get root so I want to get a result which is of this type of this
class that we created, which is root, and we can then use that result, which means we can use the
data that we get as a result and we can do whatever we want with it.
For example, we can use it to assign it to our program that then uses the currency rates or the exchange
rates.
Okay.
So now what I do is I create a new root object because basically I want to be able to return a root
object here with my root.
And because whenever you do something with the Internet, things can go wrong.
You need to use, try and catch.
So we're using try and catch for this not to break our application.
You could have an exception call here as well, but I'm just going to return my root.
If this didn't work, then we are using the using keyword here with the var client, new HTTP client.
So we create a new client and we use this system dot net http, which is the HTTP client.
So now this is made available to us.
So this class provides a base class for sending receiving HTTP requests and responses from a URL.
Okay.
And for time span we need to add system to our namespace is up here.
So we have threading tasks now we have data, we have http.
You can see all of the good stuff.
Okay.
And now the client needs a timeout.
So I'm going to give it a time span of it to wait for up to one minute.
So it has plenty of time to get the data.
You could, of course, use seconds here as well, but I'm just going to say give it a whole minute
and then the response message that I'm going to get, which is going to be client get async URL.
So let's look at get async.
Get async is this method which needs a request.
URI.
So basically we send it a link or we give it a link.
So a URL which then gives us.
Something big.
So it gets us to data in an asynchronous way.
So it's just doing it in parallel, so to speak, to our main threads so that our application doesn't
crash.
And because this is something that is available.
So you can see here the keyword available, this task does something asynchronously.
We will need to add this a weight keyword here because we are awaiting for the result and once it's
there we are going to store it in response.
So basically this is going to be the response that we get.
And the response will be in a format that we cannot read yet.
So it will be this kind of response, but in a format that we cannot read yet.
So we need to convert it into a format that our program can read, which means that we need to, first
of all, make a string out of it, which we do here.
So this is some data which is in a byte format, so to speak.
Now we convert it into an.
String so you can see serialize to http content to a string as asynchronous operation.
So we have this data and we serialize it.
So we bring it into a string format.
So we use our response, which is the HTTP response message.
We get its content, so gets or sets the content of an HTTP response message.
So basically we get the data from it and then we read it as a string and we do that asynchronously away
again and this is available again.
So we need to add a weight here.
This gives us the response string.
Now the thing is we need it in the JSON format.
So that's why we used a JSON converter here.
So let me use JSON convert here and actually.
It doesn't even offer it here, so I'll need to do it manually.
Which means I'm going to go up here and say using a new turn soft dot.
Jason.
And now for this to work, we need to make sure that we have this package installed.
So manage new get package.
And here we need to install this Newton soft Jason.
So let's install it.
You can just also browse and search for it.
In my case, it was directly available because well, I already entered the name there.
And once it is loaded and installed, we can now use this package so you can see Newton soft.
Jason Now the error disappeared and that works fine.
Okay, now what you can do is you can go down here and you can see that Jason convert works and Jason
Convert comes from this Newton soft Jason.
So what it basically does is it DC realizes the object root that we have.
With the response string.
So we need to give it the response string, which in turn was already converting the data that we got,
which was an HTTP response into a string, and now we converted that string into a JSON object.
Now, the thing is, with this response object, which is adjacent object, we can now work with it
like with a C sharp object.
And that's really the power of it because now we can just very easily access its properties.
For example, I can just go ahead and say timestamp response, object, dot timestamp so I can now just
show it in a message box because I think this is the easiest way to display it.
And with this way, I can now display that we actually get data from the Internet.
Let's just test it in a second.
But before we can do that, let's finish this method here.
So now I will return that response object.
So each time that we call get data now this method, it will return a task root response object.
Well, it will basically allow us to then access all of the root elements such as time stamp and, well,
the different currency rates that we wanted to have.
And in case that didn't work.
So in case the response code, this status code, was not okay.
And now let's look at the status codes that there are.
There is the status code, which is the 200 status code and probably the most known one is 404.
It's not found status code.
So this is a memorable status code.
You probably know it 0404 Girlfriend not found or website not found something like that.
Okay.
So these are the different response codes that there are available.
And each time that you basically try to access a HTTP page or a web page or any kind of HTTP request,
you send one out, you get a status code first.
And based on the status code, you can then decide whether you do something with the data or not.
Because, for example, if the status code was not okay, so for example, we didn't get the data,
it was not found.
Well then we cannot work with the data.
So then we could say, okay, well let's just skip it then or try something else.
Okay, but if it's 200, so if it's OC, which basically internally means it's a 200 code, then we
know that we can work with it and to only then we want to convert the response into a JSON object which
we then can use in our project.
Otherwise I'm just going to return an empty root object, so I'm just going to return my root.
And in case the program crashes or this task get data crashes, I will also just return my root here.
Okay.
Now we have this bind currency and let's see where we are using bind currency.
Well, we're using it in main window.
Now let's create a new method which will be get value or actually we can either create an extra method
for this.
Well, I think that makes sense because it's an async method.
So we have this private async void get value and what we want is we only want to bind the currencies
once we get the values.
So I need values.
A variable for vol here vol will be of type root.
So let me add that here.
Actually I can.
Yeah.
And let's add it at the very top.
So I'm going to create a new route object, which I'm going to call Val, which is going to be the values
which will be an empty root object at that point.
So it will be an object of this class.
And then I can go ahead and say.
Set well, so this route value based on what you get from the data.
Now this a way to get data will basically call this asynchronous method, which we just created and
that one will return if everything went fine.
A response object which is of type JSON, which is basically going to be a readable object which has
this format.
This is a JSON object that we get and that's what we want because that's well, with that we can work
very easily because we can then access its properties very easily by just saying response, object,
dot timestamp, for example.
Now we get that data by saying a wait, get data and here we need to pass a URL, so we need to pass
it the string URL, which in turn is basically just this request that we get from the website.
So this link here, so basically it's this link, but you take this app ID that you have here and replace
this part here.
So your app ID, you replace it and then you end up with what you see here.
So I'm just going to replace it with my own ID.
So if you download the code, you can just replace this part here.
Everything after app ID equals sign.
You can replace it with your own ID and then you will now create your own request.
And by the way, you can even test it in the browser.
So let me do that real quick here.
I'm just going to test it in a browser and this is what I'm getting.
So this is the result that I'm getting with all of the different currencies and the currency rates.
So you see, we get the disclaimer, we get the license, the time stamp base and the rates and the
rates are all of those different rates, which then in turn have this float value or actually could
be a double value.
I'm just going to use it as a double value and yeah, that's it.
So that's what we are going to receive now when we use the code that you just saw.
So basically the code that we have here, this one.
Is going to give us exactly what you just saw.
This Val will now be a root object, which is the root which contains the rates and in our case, the
time stamp and the license.
At least that's what I'm doing with it.
These are the properties that I will use and you can use other ones as well.
Well, all of them that are available here in this case, license and base are missing or disclaimer
and based on missing.
But with base, you have to be careful because well, base is a keyword in C-sharp.
So.
At this point, we get the value or we can get values.
Now let's call get value here in our main window method and basically in our main window constructor.
And now we will get the values and we will bind the currency.
Now let's look at bind currency because this was the method that we created earlier on.
Now, of course, we need to adjust, bind currency because we can't do it as we did before where we
got the data and well, we, we put the data in manually.
That's what we did here.
So what we will do is we'll get the data this time from the web.
So instead of using the different values manually, what I'm going to do is I'm going to.
Just paste it in here.
I'm going to keep it simple.
And by the way, I recall or rename this to DT for it to be a little easier to write.
Okay.
So now to this data table is called T and we have data table rows.
We add select at the first position.
But then at the second position we have, for example, Indian rupees which will just give us the value
rates, Indian rupees.
So what is value once again?
Well, value was this brute object that we created, which is of type root, of course, which contains
rates.
Which is of type rate, which is all which allows all of these rates that we have here.
So now.
We need to make sure that the names that we use here are also the names that we use here, because otherwise
we can't access them.
So I'm getting all of the different rates and I'm assigning them to the names of those rates or of those
currencies, to be precise.
Now the cool thing is the rest can pretty much stay the same because all of those combo boxes from currency
items, sources and so forth, they stay the same.
We don't need to make any changes to those because the only thing that we changed is the data now doesn't
come from the manually added elements, but it just comes from the web.
But this is basically just a number, right?
So it's really not that much of a difference for our program.
It doesn't care what the data comes from because we didn't change the data format.
It's still the same data format, only that now this here comes from the web, which is very cool.
Now, if we run this application, we can see that this is the time stamp that we get.
So this is the current timestamp at the moment of me running this.
And why did this window come up?
Well, because I have added this message box here.
Let me change that up a little bit.
So instead of displaying the timestamp, what else did we have?
We had license, so let me get the license instead.
So here I'm just going to call this one license.
Let's run this once again.
And now see.
You see license is and that is the license of open exchange rates dot org.
Now let's get the rates.
So all of the rates that we have.
Rates.
And this one.
Let's try it.
Even though rates is a different format, let's see if it crashes.
Well, it's just currency static window rate, so it doesn't show us the data in this particular format.
Let's use this response string instead.
So we should get this whole thing as one string.
You see, there we are.
So this is basically the string format of all of the data that we have gotten.
So the rates disclaimer, license timestamp.
So you can see we get the data from the Internet for the current.
Exchange rates.
Now, the cool thing is we can select those exchange rates that we have defined there.
And let's say I want to convert €1,000 into.
Well into US dollars and.
As it seems, our calculation is not correct because it should be the other way around.
So it should be for 1,000 USD, you get €843.
And now since we are using different values or we're getting different values when basically our program
is based or the data that we get is based on US dollars as the base as you can see on their page.
So they say that US dollars are the base here.
We need to make sure that we make one change to our software again and that is here at the main window
XML file inside of our convert click function.
We need to change the formula up a little bit because now the formula shouldn't go from currency, so
here it should say C and B to currency selected value and then further down or further on the right
hand side.
This one now should say from because we basically need to swap the direction of the two currencies because
otherwise we get the wrong values.
And now if I run that again.
And actually we should get rid of this box because it's really annoying.
We can just comment it out here.
This one.
Now.
Let's run it again.
And that we are so let's say 1,000 USD and let's convert that into euros.
So how many euros do we get for 1,000 USD at the point of the recording?
It's 843.
Okay.
So yeah, that's pretty much it.
So now you have seen how you can basically get data from the Internet, how to use an API, how to convert
that API information into a JSON object and then use it, because then you can just use it like a sharp
object, which is really the power of JSON objects and the conversion to something that we can read.
So the important part was, first of all, that we're using async tasks because we are getting data
asynchronously and this can take a while, this could go wrong and everything.
That's why we use try here then that we are using our HTTP client here where we have a timeout, then
we have a response which is of a certain type.
It's just of the type HTTP response message and we need then to.
First of all, get the status code of that response, and then we can get the content and we can read
it as, for example, a string which we then store in a string and then we take that string and make
a JSON object out of it.
So we use JSON convert the serialize object.
So we're serializing it into the format of root, which is again the class that we created ourself,
which basically has the different properties which we just took from the JSON format.
So now you can go to any API that is out there and you can basically apply the same principles that
you have learned in this video and get the data from there and then work with it in your program.
So I hope you enjoyed this video.
This was just a little addition to the course where I decided to add it, because I just think that
it's nice to know about this stuff and it's really helpful and useful as a developer in general.