Professional Documents
Culture Documents
talkwithangel.com
17-21 minutes
It’s Friday! that means we will continue with the third part of this
series of blogs about migrating and redesigning an existing and
popular app: CabañasRD.
3. Moving the data to the cloud: Let’s make our backend with Azure
Functions and CosmosDB and consume it from the app (this post).
I decided to make another post for the backend, if your are focused
just in the app development you can continue reading this post
without worry about the API, we will be using the one that is already
deployed.
GET https://cabanasrd-api.azurewebsites.net
/api/cabanas?code=YourKey
1 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
You can see how to get the ApiKey in this post about “Creating a
simple serverless microservice using Azure Functions and
CosmosDB”.
[Get("/api/cabanas?code={key}")]
2 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
[JsonProperty("id")]
[JsonProperty("name")]
[JsonProperty("longitude")]
[JsonProperty("latitude")]
[JsonProperty("motelServices")]
[JsonProperty("phones")]
[JsonProperty("takeCredictCards")]
[JsonProperty("ranking")]
[JsonProperty("images")]
[JsonProperty("address")]
[JsonProperty("state")]
[JsonProperty("descripcion")]
3 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
[JsonProperty("creditCards")]
[JsonProperty("type")]
[JsonProperty("isManagedByTheOwner")]
[JsonProperty("rankingValue")]
[JsonProperty("service")]
[JsonProperty("price")]
[JsonProperty("type")]
[JsonProperty("currencyType")]
[JsonProperty("descriptionDetail")]
4 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
[JsonProperty("id")]
[JsonProperty("name")]
This model was generated using the JSON response from the
service, with the help of https://quicktype.io/ we got the classes with
the fields mapped.
....
...
//Refit APIs
containerRegistry.RegisterInstance(RestService.For<ICabanasAPI>
(Configs.AppSettingsConstants.ApiUrl));
...
5 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
object to another.
....
...
//AutoMapper
containerRegistry.RegisterInstance(GetMapperConfiguration().CreateMapper
cfg.AllowNullCollections = true;
//MotelServices
cfg.CreateMap<Framework.APIs.Models.MotelResponseService,
Domain.Motels.MotelService>()
//Motel
6 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
cfg.CreateMap<Framework.APIs.Models.MotelResponse,
Domain.Motels.Motel>()
Url = $"{i}.{imagesExtension}"
})))
Number = p
})));
});
config.AssertConfigurationIsValid();
return config;
Now we are able to convert easily from the service response to our
domain model.
7 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
_cabanasAPI = cabanasAPI;
_mapper = mapper;
try
return _mapper.Map<List<APIs.Models.MotelResponse>,
List<Domain.Motels.Motel>>(cabanasResponse);
switch (ex.StatusCode)
8 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
case HttpStatusCode.InternalServerError:
case HttpStatusCode.Unauthorized:
default:
break;
throw ex;
...
...
...
//containerRegistry.Register<IMotelsSource,
InMemoryMotelsSource>();
containerRegistry.Register<IMotelsSource, MotelsSource>();
9 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
...
That’s all, run the app, wait a few seconds in the map and voilà!
3. If any error occurred during the request the app will crash, there is
a need for handling the exceptions.
10 of 11 11/28/2020, 4:06 PM
CabañasRD, from native to cross-platform with Xamarin Forms [3/4]. about:reader?url=http://talkwithangel.com/cabanasrd-from-native-to-cr...
Let’s make a deal , I will resolve the first issue and you are going
to help me by resolving the rest of problems on GitHub by creating
a PR with the changes.
For manage the ApiKey as a secret we will use this wonderful tool
made by Dan Siegel called Mobile.BuildTools.
src/App/CabanasRD/Helpers/Secrets.cs
src/App/CabanasRD/secrets.json
PD: If the build does not generate the Secrets.cs file, add it and the
Helpers folder manually and build again.
11 of 11 11/28/2020, 4:06 PM