Hi,
in the previous post I described the setting of Nancy and SqlTypeProvider in the project. Whole application was designed to periodically send information with incoming customer evaluations about products presented to them as well as evaluating their (evaluations) sentiment. At this time we have finished downloading data. In this post I would like to take care of adding the connection with an Azure Api to download semantic evaluation of the aforementioned comments. For this purpose we use sentiment function from the Text Analysis Api module from Azure Cognitive Services (more about the function and how it works here). In short, the function based on submitted text(s) returns information How undertone had given text. Whether it was positive or negative in 0-1 scale where the higher the value is, the more positive undertone it has. We begin by preparing format for Azure Api, So I created this types: For request (to Azure): For response (from Azure): As we remember, the data model from a database is a little more complicated We have to map somehow data from to db to fit into data accepted by Azure api. So I created following function to map data: Well we have a ready data conversion “to” Azure Api. So now we need to call the action “sentiment”. Call looks as follows: At this point we should have a result from Azure and the data from db. The only thing left for us at the moment is to map these data together, I realize this with this method: To check if everything works nicely, I provide a new action in the nancy module:
Everything looks great, the only thing which bother me are hardcoded strings in code, they are bad. It is difficult then to manage such code, which contains somewhere some magical values. Let us, therefore move these values to the configuration file (app.config) so we could read them later from it in code. To achieve that I add reference to FSharp.Configuration: Well, but we need to extract data from the configuration file? Are we use CofnigurationManager.AppSettings for that? Well, no, we don’t have to install FSharp.Configuration if we want to use ConfigurationManager :) Let’s use this library to get our configuration. So we define a Settings type as follows: So it could read configuration from an app.config file. How we can get values from an app settings? Simple as that:
As we can see all values from and app.config are available here and what is very cool it understand types in app.config. So AzureApiUrl has type Uri not a string. So function to get data from Azure looks right now like this:
So it’s all for this post. We saw ho to pair calling external API in our mikroservice and how to easily extract the data we need, from a configuration file by using FSharp.Configuration. What has to be done to finish the project is a cyclical sending of messages, what I would like to take care of in next post.
Thanks for reading!