private static async Task Run_Impl(
            bool checkCache,
            ILogger log,
            ExecutionContext context
            )
        {
            ConfigurationUtils.Initialize(context, log);

            var responseToken = await API.GetSireneAccessToken.RunAsync(
                new API.GetSireneAccessTokenRequest(checkCache),
                API.GetSireneAccessToken.GetLocalCacheKey,
                async (key) =>
            {
                string json = await StorageUtils.ReadContentAsync($"{StorageUtils.CacheContainerName}/{key}");

                return(json == null ? null : JsonConvert.DeserializeObject <API.GetSireneAccessTokenResponse>(json, JsonUtils.SerializerSettings));
            },
                async (key, token) => await StorageUtils.WriteContentAsync($"{StorageUtils.CacheContainerName}/{key}", token == null ? null : JsonConvert.SerializeObject(token))
                );

            if (!responseToken.IsValid)
            {
                log.LogError("Failed to get Sirene access token");
            }
        }
Exemplo n.º 2
0
        private static async Task <IActionResult> Run_Impl(
            API.QuerySireneRequest request,
            ILogger log,
            ExecutionContext context
            )
        {
            ConfigurationUtils.Initialize(context, log);

            if (!API.QuerySireneRequest.CheckValidity(request?.Siren, request?.Siret))
            {
                return(new BadRequestObjectResult($"Please pass a valid {nameof(API.QuerySireneRequest)} object"));
            }

            #region Get Sirene Access Token
            {
                // Last chance to get a valid Sirene access token.
                // Stored token should always be valid if `RefreshSireneAccessToken` timer trigger is properly executed.
                var responseToken = await API.GetSireneAccessToken.RunAsync(
                    new API.GetSireneAccessTokenRequest(),
                    API.GetSireneAccessToken.GetLocalCacheKey,
                    async (key) =>
                {
                    string json = await StorageUtils.ReadContentAsync($"{StorageUtils.CacheContainerName}/{key}");
                    return(json == null ? null : JsonConvert.DeserializeObject <API.GetSireneAccessTokenResponse>(json, JsonUtils.SerializerSettings));
                },
                    async (key, token) => await StorageUtils.WriteContentAsync($"{StorageUtils.CacheContainerName}/{key}", JsonConvert.SerializeObject(token))
                    );

                if (!responseToken.IsValid)
                {
                    log.LogError("Failed to get Sirene access token");
                    return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
                }

                request = new API.QuerySireneRequest(responseToken.Token, request.Siren, request.Siret);
            }
            #endregion

            try
            {
                var response = await API.QuerySirene.RunAsync(request);

                log.LogDebug(JsonConvert.SerializeObject(response));

                return((ActionResult) new OkObjectResult(response));
            }
            catch (Exception e)
            {
                log.LogError(e.ToString());

                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }