Пример #1
0
        public static async Task <bool> PostAsync(Route route, Method matchingMethod,
                                                  string content,
                                                  Blazored.LocalStorage.ILocalStorageService storage)
        {
            var serverUrl = await storage.GetItemAsync <string>("serverUrl");

            using (var client = new HttpClient())
            {
                var url = new Uri($"{serverUrl}/api/{route.Name}");
                Console.WriteLine($"POSTING to {url.AbsoluteUri}");
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    var token = await storage.GetItemAsync <string>(Comms.authorizationTokenStorageKey);

                    request.Headers.Authorization =
                        new System.Net.Http.Headers.AuthenticationHeaderValue(token);
                    using (request.Content = new StringContent(content))
                    {
                        request.Content.Headers.ContentType =
                            new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                        try
                        {
                            using (var response = await client.SendAsync(request))
                            {
                                return(response.IsSuccessStatusCode);
                            }
                        }
                        catch (Exception)
                        {
                            return(false);
                        }
                    }
                }
            }
        }
Пример #2
0
 public static Task <TResult> TableAsync <TResult>(Route route,
                                                   Blazored.LocalStorage.ILocalStorageService storage,
                                                   Func <Newtonsoft.Json.Linq.JObject[], TResult> onFound,
                                                   Func <string, TResult> onFailure)
 {
     return(TableAsync(route.Name, storage, onFound, onFailure));
 }
Пример #3
0
        public AppState(Blazored.LocalStorage.ILocalStorageService localStorage)
        {
            TimeEntriesKey = "timeEntries";
            _localStorage  = localStorage;
            _timeEntries   = new List <TimeEntry>();

            NotifyStateChanged();
        }
Пример #4
0
        public static async Task <TResult> GetManifestAsync <TResult>(
            Blazored.LocalStorage.ILocalStorageService storage,
            Func <Route[], TResult> onFound,
            Func <string, TResult> onFailure)
        {
            var serverUrl = await storage.GetItemAsync <string>("serverUrl");

            return(await GetManifestAsync(new Uri(serverUrl), onFound, onFailure));
        }
Пример #5
0
        public static async Task <TResult> TableAsync <TResult>(string routeName,
                                                                Blazored.LocalStorage.ILocalStorageService storage,
                                                                Func <Newtonsoft.Json.Linq.JObject[], TResult> onFound,
                                                                Func <string, TResult> onFailure)
        {
            using (var client = new HttpClient())
            {
                var serverUrl = await storage.GetItemAsync <string>("serverUrl");

                var url = new Uri($"{serverUrl}/api/{routeName}");
                Console.WriteLine(url.AbsoluteUri);
                using (var request = new HttpRequestMessage(HttpMethod.Get, url))
                {
                    var tableInfoToken = await storage.GetItemAsync <string>(storageTableInformationTokenName);

                    request.Headers.Add("StorageTableInformation", tableInfoToken);
                    request.Headers.Add("X-StorageTableInformation-List", "true");
                    try
                    {
                        using (var response = await client.SendAsync(request))
                        {
                            var json = await response.Content.ReadAsStringAsync();

                            Console.WriteLine(json);
                            if (!response.IsSuccessStatusCode)
                            {
                                return(onFailure(json));
                            }
                            try
                            {
                                var responseData = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

                                var entities    = (JArray)responseData;
                                var entityTypes = entities
                                                  .Select(
                                    entity =>
                                {
                                    var jObj = entity as JObject;
                                    return(jObj);
                                })
                                                  .ToArray();
                                return(onFound(entityTypes));
                            }
                            catch (Exception)
                            {
                                return(onFailure(json));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return(onFailure(ex.Message));
                    }
                }
            }
        }
Пример #6
0
 public HeroApi(
     IInteropService interopService,
     ILogger logger,
     HttpClient httpClient,
     Blazored.LocalStorage.ILocalStorageService LocalStore)
 {
     _interopService = interopService;
     _logger         = logger;
     _httpClient     = httpClient;
     _LocalStore     = LocalStore;
 }
Пример #7
0
        public UserSession(ISessionAPI sessionApi, Blazored.SessionStorage.ISessionStorageService sessionStorage,
                           Blazored.LocalStorage.ILocalStorageService localStorage)
        {
            this._sessionApi     = sessionApi;
            this._sessionStorage = sessionStorage;
            this._localStorage   = localStorage;

            _sessionApi.NewTokenCreated += async(s, e) =>
            {
                await SessionApiOnNewTokenCreated(s, e);
            };
        }
 public AuthenticationManager(MiracleListAPI.MiracleListProxy proxy, Blazored.LocalStorage.ILocalStorageService localStorage)
 {
     this.proxy        = proxy;
     this.localStorage = localStorage;
     Console.WriteLine("Backend: " + proxy.BaseUrl);
 }
Пример #9
0
 public LocalStorageService(Blazored.LocalStorage.ILocalStorageService localStorageService)
 {
     _localStorageService = localStorageService;
 }
 public ToiletRepository(Blazored.LocalStorage.ILocalStorageService storage)
 {
     _storage = storage;
 }
Пример #11
0
        public static async Task <TResult> GetAsync <TResult>(Route route, Method matchingMethod,
                                                              IDictionary <string, string> parameters,
                                                              Blazored.LocalStorage.ILocalStorageService storage,
                                                              Func <EntityType[], TResult> onFound,
                                                              Func <string, TResult> onFailure)
        {
            var serverUrl = await storage.GetItemAsync <string>("serverUrl");

            using (var client = new HttpClient())
            {
                var url = matchingMethod.Parameters.Aggregate(
                    new Uri($"{serverUrl}/api/{route.Name}"),
                    (current, parameter) =>
                {
                    var paramValue = parameters.ContainsKey(parameter.Name) ?
                                     parameters[parameter.Name]
                            :
                                     string.Empty;
                    var uriBuilder        = new UriBuilder(current);
                    var query             = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
                    query[parameter.Name] = paramValue;
                    uriBuilder.Query      = query.ToString();
                    return(uriBuilder.Uri);
                    //return current.AddQueryParameter(parameter.Name, paramValue);
                });
                Console.WriteLine(url.AbsoluteUri);
                using (var request = new HttpRequestMessage(HttpMethod.Get, url))
                {
                    var token = await storage.GetItemAsync <string>(Comms.authorizationTokenStorageKey);

                    request.Headers.Authorization =
                        new System.Net.Http.Headers.AuthenticationHeaderValue(token);
                    try
                    {
                        using (var response = await client.SendAsync(request))
                        {
                            var json = await response.Content.ReadAsStringAsync();

                            Console.WriteLine(json);
                            if (!response.IsSuccessStatusCode)
                            {
                                return(onFailure(json));
                            }
                            try
                            {
                                var responseData = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

                                int Ordering(JProperty prop)
                                {
                                    var orderIndex = route.Properties
                                                     .Select(
                                        (rProp, index) =>
                                    {
                                        if (rProp.Name == prop.Name)
                                        {
                                            return(index);
                                        }
                                        return(-1);
                                    })
                                                     .Where(i => i >= 0)
                                                     .First(
                                        (i, next) => i,
                                        () => int.MaxValue);

                                    return(orderIndex);
                                }

                                if (responseData is JArray)
                                {
                                    var entities    = (JArray)responseData;
                                    var entityTypes = entities
                                                      .Select(
                                        entity =>
                                    {
                                        var jObj  = entity as JObject;
                                        var props = jObj
                                                    .Properties()
                                                    .OrderBy(Ordering)
                                                    .Select((prop, index) => new JTokenDataType(prop, index))
                                                    .ToArray();
                                        return(new EntityType()
                                        {
                                            Properties = props,
                                        });
                                    })
                                                      .ToArray();
                                    return(onFound(entityTypes));
                                }
                                if (responseData is JObject)
                                {
                                    var jObj  = responseData as JObject;
                                    var props = jObj
                                                .Properties()
                                                .OrderBy(Ordering)
                                                .Select((prop, index) => new JTokenDataType(prop, index))
                                                .ToArray();
                                    var entity = new EntityType()
                                    {
                                        Properties = props,
                                    };
                                    return(onFound(entity.AsArray()));
                                }
                                return(onFailure(json));
                            }
                            catch (Exception)
                            {
                                return(onFailure(json));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return(onFailure(ex.Message));
                    }
                }
            }
        }