/// <summary> /// Push all pending changes to dropbox and apply them to the local datastore copy if the commit succeeds /// </summary> /// <returns>True if the changes are accepted without any errors or conflicts</returns> public async Task <bool> PushAsync() { IApiRequest request = PushRequest(); ApiResponse response = await request.GetResponseAsync(); return(PushResponse(response)); }
/// <summary> /// Wait for changes to this datastore from the server and apply them when(if) they arrive. /// This method will throw a DatastoreException if any local changes are pending /// </summary> /// <returns>True if changes were applied before the wait time elapsed</returns> public async Task <bool> AwaitPullAsync() { IApiRequest request = AwaitPullRequest(); ApiResponse response = await request.GetResponseAsync(); return(AwaitPullResponse(response)); }
/// <summary> /// Retrieve a datastore object from dropbox asynchronously /// </summary> /// <param name="id">The id of the datastore</param> /// <param name="options">Whether to retrieve a locally cached version, or to recheck with dropbox</param> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> /// <returns></returns> public void GetAsync(string id, Action <Datastore> success, Action <Exception> failure, DatastoreQueryOptions options = DatastoreQueryOptions.UseCached) { Datastore store; if (!_datastores.TryGetValue(id, out store) || options == DatastoreQueryOptions.ForceRefresh) { IApiRequest request = GetRequest(id); request.GetResponseAsync(response => { try { store = GetResponse(id, store, response); success(store); } catch (Exception ex) { failure(ex); } }); } else { success(store); } }
/// <summary> /// Pull all pending deltas asynchronously (or get a new snapshot if this database hasn't been loaded before) and apply them. /// Will throw a DatastoreException if this method is called when any local changes are pending /// </summary> public async Task PullAsync() { foreach (var table in _tables) { if (table.Value.HasPendingChanges) { throw new DatastoreException("Unable to pull in remote changes to Datastore with id " + Id + " as it has local changes pending"); } } if (Rev == 0) { IApiRequest request = GetSnapshotRequest(); ApiResponse response = await request.GetResponseAsync(); GetSnapshotResponse(response); } else { IApiRequest request = GetDeltasRequest(); ApiResponse response = await request.GetResponseAsync(); GetDeltasResponse(response); } }
/// <summary> /// Delete a datastore and all its tables and data asynchronously /// </summary> /// <param name="datastore">The datastore to delete</param> /// <returns>True if the deletion succeeds</returns> public async Task <bool> DeleteAsync(Datastore datastore) { IApiRequest request = DeleteRequest(datastore); ApiResponse response = await request.GetResponseAsync(); return(DeleteResponse(datastore, response)); }
/// <summary> /// Wait asynchronously for up to a minute while checking for changes to the datastore list. /// </summary> /// <returns>If a change occurs during this time, returns True</returns> public async Task <bool> AwaitListChangesAsync() { IApiRequest request = AwaitListChangesRequest(); ApiResponse response = await request.GetResponseAsync(); return(AwaitListChangesResponse(response)); }
/// <summary> /// Creates a new globally unique datastore asynchronously /// </summary> /// <param name="key">Used to generate a hash that will comprise the created datastores dropbox id</param> /// <returns>A Tuple containing the id of the created datastore, and the datastore itself</returns> public async Task <KeyValuePair <string, Datastore> > CreateAsync(string key) { string id; IApiRequest request = CreateRequest(key, out id); ApiResponse response = await request.GetResponseAsync(); return(new KeyValuePair <string, Datastore>(id, CreateResponse(key, id, response))); }
/// <summary> /// List all dropbox datastores asynchronously /// </summary> /// <param name="options">Whether to retrieve a locally cached version, or to recheck with dropbox</param> /// <returns>A list of all Dropbox datastores</returns> public async Task <IEnumerable <Datastore> > ListAsync(DatastoreQueryOptions options = DatastoreQueryOptions.UseCached) { if (string.IsNullOrEmpty(_token) || options == DatastoreQueryOptions.ForceRefresh) { IApiRequest request = ListRequest(); var response = await request.GetResponseAsync(); ListResponse(response); } return(_datastores.Values); }
/// <summary> /// Wait for up to a minute asynchronously while checking for changes to any datastore. The list of datastores /// is defined by internally calling the <see cref="ListAsync(DatastoreQueryOptions)" /> method /// </summary> /// <param name="changed">Adds every datastore which had a change during the waiting time</param> /// <returns>True if any changes were detected</returns> public async Task <bool> AwaitDatastoreChangesAsync(List <Datastore> changed) { var datastores = await ListAsync(); IApiRequest request = AwaitDatastoreChangesRequest(datastores); ApiResponse response = await request.GetResponseAsync(); bool result = AwaitDatastoreChangesResponse(changed, datastores, response); return(result); }
/// <summary> /// Retrieve a datastore object from dropbox asynchronously /// </summary> /// <param name="id">The id of the datastore</param> /// <param name="options">Whether to retrieve a locally cached version, or to recheck with dropbox</param> /// <returns></returns> public async Task <Datastore> GetAsync(string id, DatastoreQueryOptions options = DatastoreQueryOptions.UseCached) { Datastore store; if (!_datastores.TryGetValue(id, out store) || options == DatastoreQueryOptions.ForceRefresh) { IApiRequest request = GetRequest(id); var response = await request.GetResponseAsync(); store = GetResponse(id, store, response); } return(store); }
/// <summary> /// Delete a datastore and all its tables and data asynchronously /// </summary> /// <param name="datastore">The datastore to delete</param> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> public void DeleteAsync(Datastore datastore, Action <bool> success, Action <Exception> failure) { IApiRequest request = DeleteRequest(datastore); request.GetResponseAsync(response => { try { success(DeleteResponse(datastore, response)); } catch (Exception ex) { failure(ex); } }); }
/// <summary> /// Wait asynchronously for up to a minute while checking for changes to the datastore list. /// </summary> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> /// <returns>If a change occurs during this time, returns True</returns> public void AwaitListChangesAsync(Action <bool> success, Action <Exception> failure) { IApiRequest request = AwaitListChangesRequest(); request.GetResponseAsync(response => { try { success(AwaitListChangesResponse(response)); } catch (Exception ex) { failure(ex); } }); }
/// <summary> /// Push all pending changes to dropbox and apply them to the local datastore copy if the commit succeeds /// </summary> /// <returns>True if the changes are accepted without any errors or conflicts</returns> public void PushAsync(Action <bool> success, Action <Exception> failure) { IApiRequest request = PushRequest(); request.GetResponseAsync(response => { try { success(PushResponse(response)); } catch (Exception ex) { failure(ex); } }); }
/// <summary> /// Pull all pending deltas asynchronously (or get a new snapshot if this database hasn't been loaded before) and apply them. /// Will throw a DatastoreException if this method is called when any local changes are pending /// </summary> public void PullAsync(PullDelegate success, Action <Exception> failure) { foreach (var table in _tables) { if (table.Value.HasPendingChanges) { failure(new DatastoreException("Unable to pull in remote changes to Datastore with id " + Id + " as it has local changes pending")); return; } } if (Rev == 0) { IApiRequest request = GetSnapshotRequest(); request.GetResponseAsync(response => { try { GetSnapshotResponse(response); success(); } catch (Exception ex) { failure(ex); } }); } else { IApiRequest request = GetDeltasRequest(); request.GetResponseAsync(response => { try { GetDeltasResponse(response); success(); } catch (Exception ex) { failure(ex); } }); } }
/// <summary> /// Creates a new globally unique datastore /// </summary> /// <param name="key">Used to generate a hash that will comprise the created datastores dropbox id</param> /// <param name="id">A SHA256 hash of the key (see https://www.dropbox.com/developers/datastore/docs/http#create_datastore for details on how to generate the id from a key)</param> /// <returns>The datastore created</returns> public async Task <Datastore> Create(string key, string id) { if (!_keyRegex.IsMatch(key)) { throw new DatastoreException("Key did not match [-_A-Za-z0-9]{32,1000}"); } if (_datastores.ContainsKey(id)) { throw new DatastoreException("Datastore with id " + id + " already exists"); } IApiRequest request = ApiRequestFactory.Current.CreateRequest("POST", "create_datastore", ApiToken); request.AddParam("dsid", id); request.AddParam("key", key); ApiResponse response = await request.GetResponseAsync(); return(CreateResponse(key, id, response)); }
/// <summary> /// Creates a new globally unique datastore asynchronously /// </summary> /// <param name="key">Used to generate a hash that will comprise the created datastores dropbox id</param> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> public void CreateAsync(string key, Action <KeyValuePair <string, Datastore> > success, Action <Exception> failure) { string id; try { IApiRequest request = CreateRequest(key, out id); request.GetResponseAsync(response => { try { success(new KeyValuePair <string, Datastore>(id, CreateResponse(key, id, response))); } catch (Exception ex) { failure(ex); } }); } catch (Exception ex) { failure(ex); } }
/// <summary> /// Wait for up to a minute asynchronously while checking for changes to any datastore. The list of datastores /// is defined by internally calling the <see cref="ListAsync(Action<IEnumerable<Datastore>>, Action<Exception>, DatastoreQueryOptions)" /> method /// </summary> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> public void AwaitDatastoreChangesAsync(Action <List <Datastore> > success, Action <Exception> failure) { ListAsync(list => { IApiRequest request = AwaitDatastoreChangesRequest(list); request.GetResponseAsync(response => { try { List <Datastore> datastores = new List <Datastore>(); AwaitDatastoreChangesResponse(datastores, list, response); success(datastores); } catch (Exception ex) { failure(ex); } }); }, ex => { failure(ex); }); }
/// <summary> /// List all dropbox datastores asynchronously /// </summary> /// <param name="options">Whether to retrieve a locally cached version, or to recheck with dropbox</param> /// <param name="success">Callback if the method is successful</param> /// <param name="failure">Callback if the method fails</param> /// <returns></returns> public void ListAsync(Action <IEnumerable <Datastore> > success, Action <Exception> failure, DatastoreQueryOptions options = DatastoreQueryOptions.UseCached) { if (string.IsNullOrEmpty(_token) || options == DatastoreQueryOptions.ForceRefresh) { IApiRequest request = ListRequest(); request.GetResponseAsync(response => { try { ListResponse(response); success(_datastores.Values); } catch (Exception ex) { failure(ex); } }); } else { success(_datastores.Values); } }