diff --git a/FireSharp/Config/FirebaseConfig.cs b/FireSharp/Config/FirebaseConfig.cs index 6152f8d..b89c18b 100644 --- a/FireSharp/Config/FirebaseConfig.cs +++ b/FireSharp/Config/FirebaseConfig.cs @@ -11,6 +11,7 @@ public FirebaseConfig() } public string BasePath { get; set; } + public string Host { get; set; } public string AuthSecret { get; set; } public TimeSpan? RequestTimeout { get; set; } diff --git a/FireSharp/Config/IFirebaseConfig.cs b/FireSharp/Config/IFirebaseConfig.cs index b6e07c2..df22a2e 100644 --- a/FireSharp/Config/IFirebaseConfig.cs +++ b/FireSharp/Config/IFirebaseConfig.cs @@ -6,6 +6,7 @@ namespace FireSharp.Config public interface IFirebaseConfig { string BasePath { get; set; } + string Host { get; set; } string AuthSecret { get; set; } TimeSpan? RequestTimeout { get; set; } ISerializer Serializer { get; set; } diff --git a/FireSharp/FirebaseClient.cs b/FireSharp/FirebaseClient.cs index b1b9edf..f1cd73d 100644 --- a/FireSharp/FirebaseClient.cs +++ b/FireSharp/FirebaseClient.cs @@ -13,6 +13,7 @@ namespace FireSharp public class FirebaseClient : IFirebaseClient, IDisposable { private readonly IRequestManager _requestManager; + private readonly IFirebaseConfig _config; private readonly Action _defaultErrorHandler = (statusCode, body) => { @@ -26,6 +27,7 @@ public class FirebaseClient : IFirebaseClient, IDisposable public FirebaseClient(IFirebaseConfig config) : this(new RequestManager(config)) { + _config = config; } internal FirebaseClient(IRequestManager requestManager) @@ -188,6 +190,110 @@ public async Task PushAsync(string path, T data) } } + public FirebaseResponse ResetPassword(string email, string password) + { + /* + /v2/luminous-heat-7934/users/tedwards@hachisoft.com/password?&email=tedwards%40hachisoft.com&_method=POST&v=node-2.3.2&transport=json&suppress_status_codes=true + * */ + try + { + string query = string.Format(@"&email={0}&_method=POST&transport=json&suppress_status_codes=true", Uri.EscapeDataString(email)); + string path = string.Format("users/{0}/password", email); + + HttpResponseMessage response = _requestManager.RequestApiAsync(HttpMethod.Post, path, query, null).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + + public FirebaseResponse ChangePassword(string email, string oldPassword, string newPassword) + { + /* + /v2/luminous-heat-7934/users/tedwards@hachisoft.com/password?&email=tedwards%40hachisoft.com&oldPassword=oldPassword&newPassword=newPassword&_method=PUT&password=newPassword&v=node-2.3.2&transport=json&suppress_status_codes=true + * */ + try + { + string query = string.Format(@"&email={0}&oldPassword={1}&newPassword={2}&_method=PUT&password={2}&v=node-2.3.2&transport=json&suppress_status_codes=true", Uri.EscapeDataString(email), Uri.EscapeDataString(oldPassword), Uri.EscapeDataString(newPassword)); + string path = string.Format("users/{0}/password",email); + + HttpResponseMessage response = _requestManager.RequestApiAsync(HttpMethod.Put, path, query, null).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + + public FirebaseResponse CreateUser(string email, string password) + { + try + { + string query = string.Format(@"&email={0}&password={1}&_method=POST&v=node-2.3.2&transport=json&suppress_status_codes=true", Uri.EscapeDataString(email),Uri.EscapeDataString(password)); + string path = "users"; + + HttpResponseMessage response = _requestManager.RequestApiAsync(HttpMethod.Post, path,query, null).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + + public FirebaseResponse RemoveUser(string email, string password) + { + /* + /v2/luminous-heat-7934/users/tedwards@hachisoft.com?&email=tedwards%40hachisoft.com&password=password&_method=DELETE&v=node-2.3.2&transport=json&suppress_status_codes=true + * */ + try + { + string query = string.Format(@"&email={0}&password={1}&_method=DELETE&transport=json&suppress_status_codes=true", Uri.EscapeDataString(email), Uri.EscapeDataString(password)); + string path = string.Format("users/{0}", email); + + HttpResponseMessage response = _requestManager.RequestApiAsync(HttpMethod.Delete, path, query, null).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + + public FirebaseResponse ChangeEmail(string oldEmail, string password, string newEmail) + { + /* + /v2/luminous-heat-7934/users/tedwards@hachisoft.com/email?&oldEmail=tedwards%40hachisoft.com&password=password&newEmail=tedwards%40hachisoft.com&_method=PUT&email=tedwards%40hachisoft.com&v=node-2.3.2&transport=json&suppress_status_codes=true + * */ + try + { + string query = string.Format(@"&oldEmail={0}&password={1}&newEmail={2}&_method=PUT&email={2}&v=node-2.3.2&transport=json&suppress_status_codes=true", Uri.EscapeDataString(oldEmail), Uri.EscapeDataString(password), Uri.EscapeDataString(newEmail)); + string path = string.Format("users/{0}/email", oldEmail); + + HttpResponseMessage response = _requestManager.RequestApiAsync(HttpMethod.Put, path, query, null).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + + + public async Task DeleteAsync(string path) { try diff --git a/FireSharp/Interfaces/IFirebaseClient.cs b/FireSharp/Interfaces/IFirebaseClient.cs index 9373c54..5d11362 100644 --- a/FireSharp/Interfaces/IFirebaseClient.cs +++ b/FireSharp/Interfaces/IFirebaseClient.cs @@ -21,6 +21,11 @@ public interface IFirebaseClient PushResponse Push(string path, T data); FirebaseResponse Delete(string path); FirebaseResponse Update(string path, T data); + FirebaseResponse CreateUser(string email, string password); + FirebaseResponse ChangeEmail(string oldEmail, string password, string newEmail); + FirebaseResponse RemoveUser(string email, string password); + FirebaseResponse ResetPassword(string email, string password); + FirebaseResponse ChangePassword(string email, string oldPassword, string newPassword); [Obsolete("This method is obsolete use OnAsync instead.")] Task ListenAsync(string path, diff --git a/FireSharp/Interfaces/IRequestManager.cs b/FireSharp/Interfaces/IRequestManager.cs index ef66041..78e9476 100644 --- a/FireSharp/Interfaces/IRequestManager.cs +++ b/FireSharp/Interfaces/IRequestManager.cs @@ -10,5 +10,6 @@ internal interface IRequestManager : IDisposable Task RequestAsync(HttpMethod method, string path, object payload = null); Task ListenAsync(string path, string query); Task RequestAsync(HttpMethod method, string path, string query, object payload = null); + Task RequestApiAsync(HttpMethod method, string path, string query, object payload = null); } } \ No newline at end of file diff --git a/FireSharp/RequestManager.cs b/FireSharp/RequestManager.cs index c7c49fb..df34d73 100644 --- a/FireSharp/RequestManager.cs +++ b/FireSharp/RequestManager.cs @@ -74,6 +74,21 @@ public Task RequestAsync(HttpMethod method, string path, ob } } + public Task RequestApiAsync(HttpMethod method, string path, string query, object payload) + { + try + { + var request = PrepareApiRequest(method, path, query, payload); + + return GetClient().SendAsync(request, HttpCompletionOption.ResponseContentRead); + } + catch (Exception ex) + { + throw new FirebaseException( + string.Format("An error occured while execute request. Path : {0} , Method : {1}", path, method), ex); + } + } + public Task RequestAsync(HttpMethod method, string path, string query, object payload) { try @@ -115,6 +130,21 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, string return request; } + private HttpRequestMessage PrepareApiRequest(HttpMethod method, string path, string query, object payload) + { + var uri = PrepareApiUri(path,query); + + var request = new HttpRequestMessage(method, uri); + + if (payload != null) + { + var json = _config.Serializer.Serialize(payload); + request.Content = new StringContent(json); + } + + return request; + } + private Uri PrepareUri(string path, string query) { var authToken = !string.IsNullOrWhiteSpace(_config.AuthSecret) @@ -129,5 +159,12 @@ private Uri PrepareUri(string path, string query) return new Uri(url); } + + private Uri PrepareApiUri(string path, string query) + { + var url = string.Format("{0}/{1}/{2}?{3}", "https://auth.firebase.com/v2",_config.Host,path,query); + + return new Uri(url); + } } } \ No newline at end of file