WEBVTT 1 00:00:00.730 --> 00:00:02.330 Hi guys, and welcome back. 2 00:00:02.330 --> 00:00:04.820 In this video, we're going to be simplifying our code a bit 3 00:00:04.820 --> 00:00:06.840 and making it a bit more reusable 4 00:00:06.840 --> 00:00:10.540 by creating a client for our API. 5 00:00:10.540 --> 00:00:14.310 Creating API clients is a very common thing to do. 6 00:00:14.310 --> 00:00:18.400 Right now, our application has a lot of knowledge 7 00:00:18.400 --> 00:00:20.450 of how the API works. 8 00:00:20.450 --> 00:00:22.730 For example, it knows that it has to make a request 9 00:00:22.730 --> 00:00:24.250 to this particular ENDPOINT. 10 00:00:24.250 --> 00:00:27.020 It knows that it needs this particular APP_ID. 11 00:00:27.020 --> 00:00:29.260 It knows that it has to access the rates key 12 00:00:29.260 --> 00:00:31.950 in the response and so on. 13 00:00:31.950 --> 00:00:33.120 Now this is all well and good, 14 00:00:33.120 --> 00:00:35.170 and it has to be somewhere in our application 15 00:00:35.170 --> 00:00:36.840 in order for it to work, 16 00:00:36.840 --> 00:00:40.850 but maybe it doesn't belong in our main application. 17 00:00:40.850 --> 00:00:43.760 We can encapsulate it in another file 18 00:00:43.760 --> 00:00:45.730 so that it's easier to use. 19 00:00:45.730 --> 00:00:49.160 So what we're gonna do is we're gonna create a new file 20 00:00:49.160 --> 00:00:51.520 that will be a client to this API, 21 00:00:51.520 --> 00:00:52.410 and what that means is 22 00:00:52.410 --> 00:00:55.110 it's going to contain a set of functions 23 00:00:55.110 --> 00:00:58.560 that allows us to very easily interact with this API 24 00:00:58.560 --> 00:01:00.100 without having to know exactly 25 00:01:00.100 --> 00:01:02.780 what endpoint we wanna use and so on. 26 00:01:02.780 --> 00:01:06.040 So what we'll do is we are going to essentially 27 00:01:06.040 --> 00:01:09.080 delete this stuff here, 28 00:01:09.080 --> 00:01:10.680 and we will say that the client 29 00:01:10.680 --> 00:01:13.906 is an OpenExchangeClient, 30 00:01:13.906 --> 00:01:15.603 and we'll pass in the APP_ID. 31 00:01:17.140 --> 00:01:20.430 Then, the gbp_amount will no longer be 32 00:01:20.430 --> 00:01:22.310 the usd_amount multiplied by the exchange_rates. 33 00:01:22.310 --> 00:01:23.750 We no longer have those. 34 00:01:23.750 --> 00:01:26.457 We will say client.convert usd_amount 35 00:01:27.680 --> 00:01:31.040 from USD to, oh sorry, 36 00:01:31.040 --> 00:01:34.593 let's stick two double quotation marks, to GBP. 37 00:01:36.460 --> 00:01:39.920 So what this'll do is we will create this object 38 00:01:39.920 --> 00:01:41.620 of this class, OpenExchangeClient, 39 00:01:43.070 --> 00:01:46.380 and then, it will have a method called convert, 40 00:01:46.380 --> 00:01:48.533 and this method will take in a number, 41 00:01:49.390 --> 00:01:51.970 a currency that it should start from, 42 00:01:51.970 --> 00:01:54.020 and a currency that it should convert to. 43 00:01:55.170 --> 00:01:59.067 Let's go and create a new Python package 44 00:01:59.067 --> 00:02:00.931 that I'm gonna call libs 45 00:02:00.931 --> 00:02:04.335 because this client is a library, 46 00:02:04.335 --> 00:02:07.418 and I'm gonna call this openexchange. 47 00:02:08.810 --> 00:02:11.460 The reason why I'm calling this package libs, by the way, 48 00:02:11.460 --> 00:02:14.410 is because the files inside this package 49 00:02:14.410 --> 00:02:17.150 are files for interacting with other things 50 00:02:17.150 --> 00:02:19.080 that are not part of our application, 51 00:02:19.080 --> 00:02:21.190 and whenever you're interacting with other things 52 00:02:21.190 --> 00:02:22.330 that are not part of your application, 53 00:02:22.330 --> 00:02:25.520 that's usually called a library for something else. 54 00:02:25.520 --> 00:02:27.090 So here, we're defining a library 55 00:02:27.090 --> 00:02:29.163 for the OpenExchangeRates API. 56 00:02:30.150 --> 00:02:32.420 So we're gonna say import requests, 57 00:02:32.420 --> 00:02:34.800 and we will define our 58 00:02:34.800 --> 00:02:35.750 OpenExchangeClient. 59 00:02:40.250 --> 00:02:43.670 The BASE_URL that we're gonna define here is 60 00:02:43.670 --> 00:02:48.670 https://openexchangerates.org/api. 61 00:02:49.430 --> 00:02:51.990 Then, we will define an init method 62 00:02:51.990 --> 00:02:53.400 that takes in an app_id, 63 00:02:53.400 --> 00:02:55.923 and we say self.app_id is app_id, 64 00:02:57.010 --> 00:03:00.380 and then, we will say that 65 00:03:00.380 --> 00:03:03.070 the latest exchanges rates 66 00:03:03.070 --> 00:03:05.250 are the ones that we already requested earlier, 67 00:03:05.250 --> 00:03:07.730 so return requests.get 68 00:03:07.730 --> 00:03:09.000 self.BASE_URL 69 00:03:11.260 --> 00:03:16.260 /latest.json?app_id= 70 00:03:16.410 --> 00:03:17.463 self.app_id 71 00:03:19.253 --> 00:03:20.086 .json. 72 00:03:20.940 --> 00:03:23.530 So we define the latest as being the result 73 00:03:23.530 --> 00:03:24.940 of getting the latest.json, 74 00:03:24.940 --> 00:03:26.420 again, passing in the app_id, 75 00:03:26.420 --> 00:03:27.810 and we'll only get the JSON back, 76 00:03:27.810 --> 00:03:30.600 so we no longer need the response for anything, 77 00:03:30.600 --> 00:03:32.770 so we're just gonna extract the JSON from that 78 00:03:32.770 --> 00:03:34.710 and return it. 79 00:03:34.710 --> 00:03:36.980 Then, we'll define our convert method 80 00:03:36.980 --> 00:03:38.923 that takes in a from_amount, 81 00:03:40.728 --> 00:03:43.645 a from_currency, and a to_currency. 82 00:03:45.320 --> 00:03:49.330 What this says is that the rates are self.latest, 83 00:03:49.330 --> 00:03:51.790 rates, like that, 84 00:03:51.790 --> 00:03:54.859 and the to_rate, so the rate that we're interested in, 85 00:03:54.859 --> 00:03:57.109 is the rate of to_currency. 86 00:03:58.540 --> 00:04:01.853 Then, we'll say if the from_currency is USD, 87 00:04:02.730 --> 00:04:04.790 which it's almost always gonna be, 88 00:04:04.790 --> 00:04:06.310 the reason we're extracting this 89 00:04:06.310 --> 00:04:07.620 into a separate if statement 90 00:04:07.620 --> 00:04:11.920 is because the base rate of the latest is always USD, 91 00:04:11.920 --> 00:04:15.830 so we'll just say return from_amount times to_rate. 92 00:04:17.150 --> 00:04:19.850 We'll leave it as this just for a moment 93 00:04:19.850 --> 00:04:23.090 while we continue with our app, 94 00:04:23.090 --> 00:04:26.010 and here, we're gonna make this a property. 95 00:04:26.010 --> 00:04:29.870 The reason for it is because this doesn't take any arguments 96 00:04:29.870 --> 00:04:31.490 and it just makes a return. 97 00:04:31.490 --> 00:04:34.940 It doesn't actually modify the object at all. 98 00:04:34.940 --> 00:04:36.670 So by making it a property, 99 00:04:36.670 --> 00:04:40.610 it's a little bit easier to access and to reason about 100 00:04:40.610 --> 00:04:42.290 knowing that when you use it, 101 00:04:42.290 --> 00:04:44.440 that you cannot pass in any arguments 102 00:04:44.440 --> 00:04:45.720 or do anything with it. 103 00:04:45.720 --> 00:04:48.200 So just by doing a self.latest 104 00:04:48.200 --> 00:04:49.590 and getting the rates of that, 105 00:04:49.590 --> 00:04:50.690 it almost looks like 106 00:04:50.690 --> 00:04:53.410 this is a value stored within the class, 107 00:04:53.410 --> 00:04:55.640 but it is a dynamic value. 108 00:04:55.640 --> 00:04:57.870 Okay, so now that we have this, 109 00:04:57.870 --> 00:05:00.670 as long as we are converting from USD to GBP, 110 00:05:00.670 --> 00:05:01.900 this is gonna work. 111 00:05:01.900 --> 00:05:04.700 So all we have to do is import, 112 00:05:04.700 --> 00:05:09.700 so from_libs.openexchange import_OpenExchangeClient. 113 00:05:09.860 --> 00:05:11.173 Let's run our app, 114 00:05:12.240 --> 00:05:14.570 and you'll see that the value is the same 115 00:05:14.570 --> 00:05:15.403 which is good. 116 00:05:15.403 --> 00:05:16.793 That is exactly what we wanted. 117 00:05:17.650 --> 00:05:20.530 Now if the input currency is not USD, 118 00:05:20.530 --> 00:05:23.330 we'll need to convert into USD first 119 00:05:23.330 --> 00:05:26.140 before converting it into the final currency. 120 00:05:26.140 --> 00:05:29.650 So we'll say from_in_usd is from_amount 121 00:05:30.680 --> 00:05:35.360 but divided by the rate in from_currency. 122 00:05:35.360 --> 00:05:37.900 Normally, when we are performing a currency exchange, 123 00:05:37.900 --> 00:05:41.410 we multiply by the rate in order to give us the base 124 00:05:41.410 --> 00:05:42.860 in the new currency, 125 00:05:42.860 --> 00:05:44.210 but if we wanna go the other way around 126 00:05:44.210 --> 00:05:46.740 where we wanna turn a currency into the base, 127 00:05:46.740 --> 00:05:48.840 we have to divide, 128 00:05:48.840 --> 00:05:51.700 and then, we'll say return from_in_usd 129 00:05:51.700 --> 00:05:54.343 multiplied by rate, just as above. 130 00:05:55.720 --> 00:05:58.020 All right, so now, we can convert from any currency 131 00:05:58.020 --> 00:06:00.640 into any other currency using this method, 132 00:06:00.640 --> 00:06:03.620 which is much simpler because now that we've got that, 133 00:06:03.620 --> 00:06:06.300 you could potentially write unit tests for it. 134 00:06:06.300 --> 00:06:08.960 You could, of course, use it in multiple places 135 00:06:08.960 --> 00:06:09.830 in your application. 136 00:06:09.830 --> 00:06:11.450 You can write documentation for it 137 00:06:11.450 --> 00:06:13.620 so it's a little bit clearer what it does, 138 00:06:13.620 --> 00:06:15.570 and you don't have to have all that code 139 00:06:15.570 --> 00:06:17.880 in just one place in your application. 140 00:06:17.880 --> 00:06:21.550 So if you wanted to convert a thousand Euros to GBP, 141 00:06:21.550 --> 00:06:24.800 do make sure to rename this variable if you do that. 142 00:06:24.800 --> 00:06:27.460 Then you'll see that 1,000 Euros 143 00:06:27.460 --> 00:06:30.580 is GBP 885, basically 886, 144 00:06:30.580 --> 00:06:34.600 and this is here where we encounter the long numbers. 145 00:06:34.600 --> 00:06:38.480 If you want to format a number as two decimal places, 146 00:06:38.480 --> 00:06:41.190 you just have gotta add after the variable name 147 00:06:41.190 --> 00:06:45.800 but within the curly braces :.2f, 148 00:06:45.800 --> 00:06:49.030 and that is gonna limit this to two decimal places, 149 00:06:49.030 --> 00:06:51.710 and you can see that it rounds appropriately, 150 00:06:51.710 --> 00:06:54.143 and then, you get 886.00, 151 00:06:55.030 --> 00:06:58.360 but we're gonna go back into USD just so it's a bit clearer, 152 00:06:58.360 --> 00:07:00.393 and also, all our strings are using USD. 153 00:07:01.410 --> 00:07:02.830 That's it for this video. 154 00:07:02.830 --> 00:07:05.190 We have created a simple client 155 00:07:05.190 --> 00:07:08.500 that gets the data from the API 156 00:07:08.500 --> 00:07:11.470 and then uses it, all within one class, 157 00:07:11.470 --> 00:07:13.810 so that it's much easier to think about, 158 00:07:13.810 --> 00:07:17.423 easier to reuse, and easier to test later on as well. 159 00:07:18.590 --> 00:07:20.200 Thanks for joining me in this video. 160 00:07:20.200 --> 00:07:21.650 I'll see you in the next one.