/// <summary> /// Creates a table and/or fully replaces its schema. /// </summary> /// <param name="schema">the schema</param> /// <returns>returns true if the table was created, false if the table already exists. In case of any other error it throws a WebException.</returns> public async Task <bool> CreateTableAsync(TableSchema schema, RequestOptions options = null) { schema.ArgumentNotNull("schema"); var optionToUse = options ?? _globalRequestOptions; return(await optionToUse.RetryPolicy.ExecuteAsync(() => CreateTableAsyncInternal(schema, optionToUse))); }
/// <summary> /// Creates a table and/or fully replaces its schema. /// </summary> /// <param name="schema">the schema</param> /// <returns>returns true if the table was created, false if the table already exists. In case of any other error it throws a HttpRequestException.</returns> public async Task <bool> CreateTableAsync(TableSchema schema) { schema.ArgumentNotNull(nameof(schema)); if (string.IsNullOrEmpty(schema.Name)) { throw new ArgumentException("schema.Name was either null or empty!", nameof(schema)); } using (var webResponse = await PutRequestAsync(schema.Name + "/schema", null, schema).ConfigureAwait(false)) { if (webResponse.WebResponse.StatusCode == HttpStatusCode.Created) { return(true); } // table already exits if (webResponse.WebResponse.StatusCode == HttpStatusCode.OK) { return(false); } throw new HttpRequestException($"Couldn't create table {schema.Name}! Response code was: {webResponse.WebResponse.StatusCode}, expected either 200 or 201! Response body was: {await webResponse.WebResponse.Content.ReadAsStringAsync().ConfigureAwait(false)}"); } }
/// <summary> /// Modifies a table schema. /// If necessary it creates a new table with the given schema. /// If something went wrong, a WebException is thrown. /// </summary> /// <param name="table">the table name</param> /// <param name="schema">the schema</param> public async Task ModifyTableSchemaAsync(string table, TableSchema schema, RequestOptions options = null) { table.ArgumentNotNullNorEmpty("table"); schema.ArgumentNotNull("schema"); var optionToUse = options ?? _globalRequestOptions; await optionToUse.RetryPolicy.ExecuteAsync(() => ModifyTableSchemaAsyncInternal(table, schema, optionToUse)); }
private async Task <bool> CreateTableAsyncInternal(TableSchema schema, string alternativeEndpointBase = null) { schema.ArgumentNotNull("schema"); if (string.IsNullOrEmpty(schema.name)) { throw new ArgumentException("schema.name was either null or empty!", "schema"); } while (true) { IRetryPolicy retryPolicy = _retryPolicyFactory.Create(); try { using (HttpWebResponse webResponse = await PutRequestAsync(schema.name + "/schema", schema, alternativeEndpointBase)) { if (webResponse.StatusCode == HttpStatusCode.Created) { return(true); } // table already exits if (webResponse.StatusCode == HttpStatusCode.OK) { return(false); } // throw the exception otherwise using (var output = new StreamReader(webResponse.GetResponseStream())) { string message = output.ReadToEnd(); throw new WebException( string.Format( "Couldn't create table {0}! Response code was: {1}, expected either 200 or 201! Response body was: {2}", schema.name, webResponse.StatusCode, message)); } } } catch (Exception e) { if (!retryPolicy.ShouldRetryAttempt(e)) { throw; } } } }
/// <summary> /// Modifies a table schema. /// If necessary it creates a new table with the given schema. /// If something went wrong, a HttpRequestException is thrown. /// </summary> /// <param name="table">the table name</param> /// <param name="schema">the schema</param> public async Task ModifyTableSchemaAsync(string table, TableSchema schema) { table.ArgumentNotNullNorEmpty(nameof(table)); schema.ArgumentNotNull(nameof(schema)); using (var webResponse = await PostRequestAsync(table + "/schema", schema).ConfigureAwait(false)) { if (webResponse.WebResponse.StatusCode == HttpStatusCode.OK || webResponse.WebResponse.StatusCode == HttpStatusCode.Created) { return; } throw new HttpRequestException($"Couldn't modify table schema {table}! Response code was: {webResponse.WebResponse.StatusCode}, expected either 200 or 201! Response body was: {await webResponse.WebResponse.Content.ReadAsStringAsync().ConfigureAwait(false)}"); } }
private async Task ModifyTableSchemaAsyncInternal(string table, TableSchema schema, string alternativeEndpointBase = null) { table.ArgumentNotNullNorEmpty("table"); schema.ArgumentNotNull("schema"); while (true) { IRetryPolicy retryPolicy = _retryPolicyFactory.Create(); try { using (HttpWebResponse webResponse = await PostRequestAsync(table + "/schema", schema, alternativeEndpointBase)) { if (webResponse.StatusCode != HttpStatusCode.OK || webResponse.StatusCode != HttpStatusCode.Created) { using (var output = new StreamReader(webResponse.GetResponseStream())) { string message = output.ReadToEnd(); throw new WebException( string.Format( "Couldn't modify table {0}! Response code was: {1}, expected either 200 or 201! Response body was: {2}", table, webResponse.StatusCode, message)); } } else { return; } } } catch (Exception e) { if (!retryPolicy.ShouldRetryAttempt(e)) { throw; } } } }