Beispiel #1
0
        public async Task <AppSettings> GetAppSettings()
        {
            try
            {
                if (!Settings.HasAzureSiteName)
                {
                    throw new InvalidOperationException("`Settings.AzureAppName` must have a value before getting or updating app settings");
                }

                NetworkIndicator.ToggleVisibility(true);

                var response = await HttpClient.GetAsync(Routes.GetAppSettings);

                var stringContent = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode || string.IsNullOrEmpty(stringContent))
                {
                    throw new Exception($"Error getting app settings from server");
                }

                return(JsonConvert.DeserializeObject <AppSettings> (stringContent));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }
Beispiel #2
0
        public async Task <bool> AddNewFileAsync(AvContent avContent, StorageToken storageToken)
        {
            try
            {
                Log.Debug(storageToken?.ToString());

                var sasUri = new Uri(storageToken.SasUri);

                var blockBlob = new CloudBlockBlob(sasUri);

                blockBlob.Metadata [DocumentUpdatedMessage.DocumentIdKey] = avContent.Id;

                blockBlob.Metadata [DocumentUpdatedMessage.CollectionIdKey] = typeof(AvContent).Name;

                NetworkIndicator.ToggleVisibility(true);

                await blockBlob.UploadFromFileAsync(avContent.LocalInboxPath);

                Log.Debug($"Finished uploading new file.");

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return(false);
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }
Beispiel #3
0
        public async Task <StorageToken> GetStorageToken <T> (T content)
            where T : Content
        {
            var collectionId = typeof(T).Name;

            if (content?.HasId ?? false)
            {
                try
                {
                    NetworkIndicator.ToggleVisibility(true);

                    var response = await HttpClient.GetAsync(Routes.StorageToken(collectionId, content.Id));

                    var stringContent = await response.Content.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode || string.IsNullOrEmpty(stringContent))
                    {
                        throw new Exception($"Error getting new storage token from server for {collectionId} with Id: {content.Id}");
                    }

                    return(JsonConvert.DeserializeObject <StorageToken> (stringContent));
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
                finally
                {
                    NetworkIndicator.ToggleVisibility(false);
                }
            }

            return(null);
        }
Beispiel #4
0
        public async Task Publish <T> (T content, string notificationTitle = null, string notificationMessage = null, UserRoles?publishTo = null)
            where T : Content
        {
            if (content?.HasId ?? false)
            {
                try
                {
                    var updateMessage = new DocumentUpdatedMessage(content.Id, typeof(T).Name, publishTo ?? content.PublishedTo)
                    {
                        Title   = notificationTitle,
                        Message = notificationMessage
                    };

                    Log.Debug(updateMessage.NotificationTags);

                    NetworkIndicator.ToggleVisibility(true);

                    var response = await HttpClient.PostAsync(Routes.PublishContent, new StringContent (JsonConvert.SerializeObject(updateMessage), Encoding.UTF8, Routes.Json));

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception($"Error posting document update message: {updateMessage}");
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
                finally
                {
                    NetworkIndicator.ToggleVisibility(false);
                }
            }
        }
Beispiel #5
0
        public async Task AuthenticateUser(string providerToken, string providerAuthCode)
        {
            try
            {
                if (string.IsNullOrEmpty(providerToken))
                {
                    throw new ArgumentNullException(nameof(providerToken));
                }
                if (string.IsNullOrEmpty(providerAuthCode))
                {
                    throw new ArgumentNullException(nameof(providerAuthCode));
                }

                ResetUser();

                var auth = JObject.Parse($"{{'id_token':'{providerToken}','authorization_code':'{providerAuthCode}'}}").ToString();

                NetworkIndicator.ToggleVisibility(true);

                var authResponse = await HttpClient.PostAsync(Routes.LoginGoogle, new StringContent (auth, Encoding.UTF8, Routes.Json));

                if (authResponse.IsSuccessStatusCode)
                {
                    var azureUser = JsonConvert.DeserializeObject <AzureAppServiceUser> (await authResponse.Content.ReadAsStringAsync());

                    Keychain.SaveItemToKeychain(AzureAppServiceUser.AuthenticationHeader, "azure", azureUser.AuthenticationToken);

                    _httpClient = null;

                    var userConfigJson = await HttpClient.GetStringAsync(Routes.AuthenticateUser);

                    authUser = JsonConvert.DeserializeObject <AuthUserConfig> (userConfigJson);

                    authUser.SaveToKeychain(Keychain);

                    Log.Debug(authUser.ToString());

                    _user = null;

                    CurrentUserChanged?.Invoke(this, User);
                }
                else
                {
                    Log.Error(auth);
                    Log.Error(authResponse?.ToString());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }
Beispiel #6
0
        public async Task <string> GetContentToken <T> (bool refresh = false)
            where T : Entity
        {
            const string errorToken = "{\"Message\":\"An error has occurred.\"}";

            var collectionId = typeof(T).Name;

            try
            {
                var resourceToken = Settings.GetContentToken <T> ();

                if (refresh || string.IsNullOrEmpty(resourceToken) || resourceToken == errorToken)
                {
                    Log.Info($"Getting new content token from server for {collectionId}");

                    NetworkIndicator.ToggleVisibility(true);

                    var response = await HttpClient.GetAsync(Routes.ContentToken(collectionId));

                    var stringContent = await response.Content.ReadAsStringAsync();

                    resourceToken = stringContent.Trim('"');

                    if (!response.IsSuccessStatusCode || resourceToken == errorToken)
                    {
                        throw new Exception($"Error getting new content token from server for {collectionId}");
                    }

                    Settings.SetContentToken <T> (resourceToken);
                }
                else
                {
                    Log.Info($"Found existing content token for {collectionId}");
                }

                return(resourceToken);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }
Beispiel #7
0
        async Task <List <T> > ExecuteWithRetry <T> (Func <Task <List <T> > > task)
            where T : Entity
        {
            try
            {
                if (client == null)
                {
                    await RefreshResourceToken <T> (false);
                }

                NetworkIndicator.ToggleVisibility(true);

                return(await task());
            }
            catch (DocumentClientException dex)
            {
                Log.Debug(dex.Print());

                switch (dex.StatusCode)
                {
                case HttpStatusCode.NotFound: return(null);

                case HttpStatusCode.Forbidden:

                    await RefreshResourceToken <T> ();

                    return(await task());

                default: throw;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }