WEBVTT

00:00.160 --> 00:09.720
The third step is to create the retrofit provider, select new object, name it as Retrofit Provider.

00:09.720 --> 00:17.480
Inside this object, we need to create a function that provides us with the directions service instance

00:17.520 --> 00:25.920
function create direction service that returns a directions service instance.

00:25.920 --> 00:31.800
This function takes a base URL as a string parameter.

00:31.840 --> 00:36.280
Don't worry, we're going to clarify everything in this example.

00:36.320 --> 00:43.560
Now return retrofit builder dot base url alt+.

00:43.560 --> 00:47.600
Enter to import retrofit instance base url.

00:47.840 --> 00:54.840
Pass the base URL parameter passed by the create directions service and add converter.

00:54.840 --> 01:02.880
Factory JSON for converter factory dot create dot build function dot create direction service class.

01:03.640 --> 01:10.520
This is what we've done and we talked about in the previous sections and in previous applications when

01:10.520 --> 01:12.480
we worked with retrofit.

01:12.480 --> 01:15.750
But don't worry, we're going to make a quick recap about it.

01:15.790 --> 01:24.150
This object defines a utility object called retrofit Builder, and the retrofit provider that creates

01:24.310 --> 01:30.630
and provides instances of retrofit service for directions related API calls.

01:30.790 --> 01:35.310
The object declaration object retrofit provider.

01:35.590 --> 01:38.270
This creates a singleton object.

01:38.270 --> 01:41.230
Only one instance exists in the application.

01:41.430 --> 01:43.230
No need to instantiate it.

01:43.230 --> 01:50.710
You can call its functions directly via retrofit provider dot create directions service, for example,

01:50.750 --> 01:52.150
or any function name.

01:52.190 --> 01:58.950
Create directions service is a function taking a base URL string as a parameter.

01:59.070 --> 02:03.830
Returning an implementation of directions service.

02:04.030 --> 02:10.670
The retrofit builder starts building a retrofit instance.

02:10.830 --> 02:15.150
Base URL sets the base URL for all API calls.

02:15.350 --> 02:25.710
As I said to you, the base URL of the URL of the API is the constant part of the API call dot add converter

02:25.710 --> 02:27.350
factory adds.

02:28.270 --> 02:33.020
As the converter for JSON serialization and deserialization as we talked about.

02:33.060 --> 02:33.860
Retrofit.

02:33.900 --> 02:37.020
It needs a converter that uses.

02:37.340 --> 02:47.660
That uses serialization and deserialization for converting JSON and JSON response to Kotlin objects

02:47.660 --> 02:48.740
and vice versa.

02:48.780 --> 02:53.420
Build finalizes and creates the retrofit instance.

02:53.420 --> 02:55.220
Create a function.

02:55.260 --> 03:00.660
Generates an implementation of direction service interface.

03:00.660 --> 03:05.180
This is a clean way to centralize retrofit service creation.

03:05.420 --> 03:10.980
The directions service is an interface defining API endpoints.

03:11.020 --> 03:14.740
Decent is used for JSON conversion.

03:14.780 --> 03:23.900
You could swap this for emoji or other converters, but as I said to you, JSON is the more professional

03:23.900 --> 03:25.220
and it's fast.

03:25.260 --> 03:29.780
The base URL is parameterized for flexibility.

03:29.980 --> 03:32.140
Could point to different environments.

03:32.180 --> 03:40.580
Okay, so this pattern is commonly used in Android application to abstract away retrofit setup and provide

03:40.580 --> 03:43.500
easy access to API services.
