示例#1
0
        public static float InflationInARecentYear() //possessionPrice = PossessionPrice* ( inflation* daysLeft/365)
        {
            if (inflation != 0)
            {
                return(inflation);
            }

            string link = "https://www.quandl.com/api/v3/datasets/RATEINF/CPI_USA.json?api_key=4SykxztoStAkUxdKf7Xd";

            if (link == null)
            {
                return(0);
            }
            ApiLink api = new ApiLink
            {
                Url = link
            };
            HttpRequest httpRequest      = new HttpRequest();
            string      result           = httpRequest.StartHttpRequest(api);
            JObject     jObject          = JObject.Parse(result);
            var         monthlyInflation = jObject["dataset"]["data"];
            var         lastMonth        = float.Parse(monthlyInflation[0][1].ToString());
            var         lastYear         = float.Parse(monthlyInflation[11][1].ToString());

            return((lastMonth - lastYear) / lastYear * 100);
        }
示例#2
0
        private ApiResponse(ApiError[] errors, ApiLink links, T data, dynamic[] included)
        {
            Data   = data;
            Links  = links;
            Errors = errors;

            if (typeof(T) == typeof(Match))
            {
                if (data != null)
                {
                    Match match = (Match)(object)data;

                    for (int i = 0; i < match.Relationships.Rosters.Length; i++)
                    {
                        Roster  orig = match.Relationships.Rosters[i];
                        dynamic incl = included.Where(d => d.type == orig.Type && d.id == orig.Id).FirstOrDefault();
                        if (incl != null)
                        {
                            string inclRosterJson = JsonConvert.SerializeObject(incl);
                            Roster inclRoster     = JsonConvert.DeserializeObject <Roster>(inclRosterJson);

                            for (int i2 = 0; i2 < inclRoster.Relationships.Participants.Length; i2++)
                            {
                                Participant origParticipant = inclRoster.Relationships.Participants[i2];
                                dynamic     inclParticipant = included.Where(d => d.type == origParticipant.Type && d.id == origParticipant.Id).FirstOrDefault();
                                if (incl != null)
                                {
                                    string      inclParticipantJson = JsonConvert.SerializeObject(inclParticipant);
                                    Participant inclParticipantObj  = JsonConvert.DeserializeObject <Participant>(inclParticipantJson);
                                    inclRoster.Relationships.Participants[i2] = inclParticipantObj;
                                }
                            }

                            match.Relationships.Rosters[i] = inclRoster;
                        }
                    }
                }
            }

            if (typeof(T) == typeof(Leaderboard))
            {
                if (data != null)
                {
                    Leaderboard leaderboard = (Leaderboard)(object)data;

                    for (int i = 0; i < leaderboard.Relationships.Players.Length; i++)
                    {
                        Player  orig = leaderboard.Relationships.Players[i];
                        dynamic incl = included.Where(d => d.type == orig.Type && d.id == orig.Id).FirstOrDefault();
                        if (incl != null)
                        {
                            string inclPlayerJson = JsonConvert.SerializeObject(incl);
                            Player inclPlayer     = JsonConvert.DeserializeObject <Player>(inclPlayerJson);
                            leaderboard.Relationships.Players[i] = inclPlayer;
                        }
                    }
                }
            }
        }
示例#3
0
 public void AddDownloadEntity(ApiLink apiLink, IApiCallback callback)
 {
     taskEntities.Add(new ApiTaskEntity
     {
         apiLink = apiLink,
         objectThatNeedApiCallback = callback
     });
 }
示例#4
0
        private static ApiLink ParseLinkObject(JObject outer, string rel, ApiDefinition definition)
        {
            var link = new ApiLink {
                Rel = rel
            };

            foreach (var inner in outer.Properties())
            {
                var value = inner.Value.ToString();

                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }

                switch (inner.Name.ToLowerInvariant())
                {
                case "href":
                    link.Href = TryCreateUri(value, UriKind.RelativeOrAbsolute);
                    break;

                case "templated":
                    link.Templated = value.Equals("true", StringComparison.OrdinalIgnoreCase);
                    break;

                case "type":
                    link.Type = value;
                    break;

                case "deprication":
                    link.Deprecation = TryCreateUri(value, UriKind.Absolute);
                    break;

                case "profile":
                    link.Profile = TryCreateUri(value, UriKind.Absolute);
                    break;

                case "title":
                    link.Title = value;
                    break;

                case "hreflang":
                    link.HrefLang = value;
                    break;
                }
            }

            return(link);
        }
        /// <summary>
        /// Fetches the full object from an ApiLink object.
        ///
        /// Many representations in the Jenkins API will be a reference to an object. This
        /// method will resolve that link and serialise into an appropriate form.
        ///
        /// Objects can be optionally retrieved from a cache. A newly acquired response
        /// will always update the cache.
        /// </summary>
        /// <param name="apiLink"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public async Task <JenkinsObject> FetchFullObject(ApiLink apiLink, bool useCache = true)
        {
            string json = null;
            var    uri  = new Uri($"{apiLink.Url}api/json");

            if (useCache)
            {
                try
                {
                    var cacheResponse = await lambdaRetry.Retry(async() => await dataStore.Load(uri.ToString()));

                    if (cacheResponse.IsSuccess)
                    {
                        json = cacheResponse.Content;
                    }
                }
                catch (HttpRequestException ex)
                {
                    logger.LogError(ex, "Could not contact CouchDB. Is it switched on with correct credentials?");
                }
            }

            if (json == null)
            {
                using var response = await lambdaRetry.Retry(async () =>
                {
                    using var request = CreateJenkinsRequest(uri.ToString());
                    return(await httpClient.SendAsync(request));
                });

                response.EnsureSuccessStatusCode();
                json = await response.Content.ReadAsStringAsync();

                var  jenkinsObject = jenkinsDeserialiser.Deserialise(json);
                bool shouldCache   = true;
                if (jenkinsObject is WorkflowRun wr)
                {
                    shouldCache = wr.Result != null;
                }
                if (shouldCache)
                {
                    await dataStore.Save(uri.ToString(), jenkinsObject);
                }
            }

            return(jenkinsDeserialiser.Deserialise(json));
        }
        public async Task <List <WorkflowRun> > FetchAllWorkflowRuns(ApiLink apiLink)
        {
            List <WorkflowRun> runs = new List <WorkflowRun>();

            switch (apiLink)
            {
            case ApiLink link:
            {
                var jenkinsObject = await FetchFullObject(link);

                var r = await FetchAllWorkflowRuns(jenkinsObject);

                runs.AddRange(r);
            }
            break;

            default:
                logger.LogWarning($"Unknown API link type: {apiLink.GetType().FullName}.");
                break;
            }
            return(runs);
        }
        public void LoadPossessionsBackupList()
        {
            using (var scope = scopeFactory.CreateScope())
            {
                var db = scope.ServiceProvider.GetRequiredService <DboContext>();
                PossessionBackups = mapperBackup.Map <List <PossessionBackup> >(db.PossessionsData);
                APIFetcher apiFetcher         = new APIFetcher();
                List <DboPossessionData> List = db.PossessionsData.ToList();

                for (var x = 0; x < PossessionBackups.Count; x++)
                {
                    ApiLink apiLink = new ApiLink
                    {
                        Headers = List.ElementAt(x).Headers,
                        Url     = List.ElementAt(x).UrlAPI,
                        Type    = List.ElementAt(x).Type
                    };

                    apiFetcher.AddDownloadEntity(apiLink, (IApiCallback)PossessionBackups.ElementAt(x));
                }

                apiFetcher.RunAllDownloadsAsync();
            }
        }