Exemple #1
0
        private async Task <ApiResponse> GetPagedApiResponse(string apiEndpoint, string fields, int offset, Dictionary <string, string> filters = null)
        {
            var context    = new ApplicationDbContext();
            var apiBaseUrl = context.ApplicationSettings.FirstOrDefault(x => x.SettingName == ApplicationSettingsTypes.ApiBaseUrl)?.SettingValue;

            var maxRecordLimit = 100;
            var fullUrl        = GetApiPrefix() + apiEndpoint + "?limit=" + maxRecordLimit;

            if (!string.IsNullOrEmpty(fields))
            {
                fullUrl += "&fields=" + fields;
            }
            if (filters != null)
            {
                foreach (var item in filters)
                {
                    fullUrl += string.Format("&{0}={1}", item.Key, item.Value);
                }
            }
            var tokenModel = GetToken();

            if (!tokenModel.IsSuccessful)
            {
                var ex = new EF2ORCustomException("There was a problem connecting to the API.  Make sure the connection can test properly in the Settings page.");
                throw ex;
            }

            var token = tokenModel.Token;

            return(await GetPagedJArray(token, fullUrl, apiBaseUrl, offset));
        }
Exemple #2
0
        public async Task <EdFiOdsApiNS.IEdFiOdsData> GetApiResponse <T>(string apiEndpoint, string fields) where T : class, EdFiOdsApiNS.IEdFiOdsData, new()
        {
            var context        = new ApplicationDbContext();
            var apiBaseUrl     = context.ApplicationSettings.FirstOrDefault(x => x.SettingName == ApplicationSettingsTypes.ApiBaseUrl)?.SettingValue;
            var maxRecordLimit = 100;
            //var stopFetchingRecordsAt = 500;

            var fullUrl = GetApiPrefix() + apiEndpoint + "?limit=" + maxRecordLimit;

            if (!string.IsNullOrEmpty(fields))
            {
                fullUrl += "&fields=" + fields;
            }

            var tokenModel = GetToken();

            if (!tokenModel.IsSuccessful)
            {
                var ex = new EF2ORCustomException("There was a problem connecting to the API.  Make sure the connection can test properly in the Settings page.");
                throw ex;
            }

            var token = tokenModel.Token;
            await Task.Yield();

            return(GetFullJArray <T>(token, fullUrl, apiBaseUrl, maxRecordLimit));
        }
Exemple #3
0
        private async Task <ApiResponse> GetCustomApiResponse(string customUrl)
        {
            var tokenModel = GetToken();

            if (!tokenModel.IsSuccessful)
            {
                var ex = new EF2ORCustomException("There was a problem connecting to the API.  Make sure the connection can test properly in the Settings page.");
                throw ex;
            }

            var token = tokenModel.Token;

            var context    = new ApplicationDbContext();
            var apiBaseUrl = context.ApplicationSettings.FirstOrDefault(x => x.SettingName == ApplicationSettingsTypes.ApiBaseUrl)?.SettingValue;

            using (var client = new HttpClient {
                BaseAddress = new Uri(apiBaseUrl)
            })
            {
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", token);

                var apiResponse = await client.GetAsync(customUrl);

                if (apiResponse.IsSuccessStatusCode == false && apiResponse.ReasonPhrase == "Invalid token")
                {
                    return(new ApiResponse
                    {
                        TokenExpired = true,
                        ResponseArray = null
                    });
                }

                if (apiResponse.IsSuccessStatusCode == false)
                {
                    throw new Exception(apiResponse.ReasonPhrase);
                }

                var responseJson = await apiResponse.Content.ReadAsStringAsync();

                var responseArray = JArray.Parse(responseJson);

                return(new ApiResponse
                {
                    TokenExpired = false,
                    ResponseArray = responseArray
                });
            }
        }