Skip to content

Commit

Permalink
user managment functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tillerstarredwards committed Jan 6, 2016
1 parent e11ede7 commit d6a802c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions FireSharp/Config/FirebaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions FireSharp/Config/IFirebaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
106 changes: 106 additions & 0 deletions FireSharp/FirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace FireSharp
public class FirebaseClient : IFirebaseClient, IDisposable
{
private readonly IRequestManager _requestManager;
private readonly IFirebaseConfig _config;

private readonly Action<HttpStatusCode, string> _defaultErrorHandler = (statusCode, body) =>
{
Expand All @@ -26,6 +27,7 @@ public class FirebaseClient : IFirebaseClient, IDisposable
public FirebaseClient(IFirebaseConfig config)
: this(new RequestManager(config))
{
_config = config;
}

internal FirebaseClient(IRequestManager requestManager)
Expand Down Expand Up @@ -188,6 +190,110 @@ public async Task<PushResponse> PushAsync<T>(string path, T data)
}
}

public FirebaseResponse ResetPassword(string email, string password)
{
/*
/v2/luminous-heat-7934/users/[email protected]/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/[email protected]/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/[email protected]?&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/[email protected]/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<FirebaseResponse> DeleteAsync(string path)
{
try
Expand Down
5 changes: 5 additions & 0 deletions FireSharp/Interfaces/IFirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface IFirebaseClient
PushResponse Push<T>(string path, T data);
FirebaseResponse Delete(string path);
FirebaseResponse Update<T>(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<EventStreamResponse> ListenAsync(string path,
Expand Down
1 change: 1 addition & 0 deletions FireSharp/Interfaces/IRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ internal interface IRequestManager : IDisposable
Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, object payload = null);
Task<HttpResponseMessage> ListenAsync(string path, string query);
Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, string query, object payload = null);
Task<HttpResponseMessage> RequestApiAsync(HttpMethod method, string path, string query, object payload = null);
}
}
37 changes: 37 additions & 0 deletions FireSharp/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, ob
}
}

public Task<HttpResponseMessage> 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<HttpResponseMessage> RequestAsync(HttpMethod method, string path, string query, object payload)
{
try
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
}

0 comments on commit d6a802c

Please sign in to comment.