/// <summary> /// Returns a collection of all repositories of the underlying user. /// </summary> /// <typeparam name="TCollection">The type of the collection.</typeparam> /// <returns>The collection of repositories.</returns> public async Task <TCollection> GetAll <TCollection>() where TCollection : global::System.Collections.Generic.ICollection <Repository>, new() { using (var rest = User.Endpoint.Client.CreateBaseClient()) { var resp = await rest.GetAsync("users/" + HttpUtility.UrlEncode(User.Username) + "/repos"); Exception exception = null; if (resp.StatusCode != HttpStatusCode.OK) { switch (resp.StatusCode) { case HttpStatusCode.InternalServerError: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } var json = await resp.Content.ReadAsStringAsync(); var repoList = JsonConvert.DeserializeObject <IEnumerable <Repository> > ( await resp.Content.ReadAsStringAsync() ); var userRepos = new TCollection(); using (var e = repoList.GetEnumerator()) { while (e.MoveNext()) { var r = e.Current; r.Owner.Endpoint = User.Endpoint; userRepos.Add(r); } } return(userRepos); } }
/// <summary> /// Returns a collection of all followers. /// </summary> /// <typeparam name="TCollection">The type of the collection.</typeparam> /// <returns>The collection of followers.</returns> public async Task <TCollection> GetFollowers <TCollection>() where TCollection : global::System.Collections.Generic.ICollection <User>, new() { using (var rest = Endpoint.Client.CreateBaseClient()) { var resp = await rest.GetAsync("users/" + HttpUtility.UrlEncode(Username) + "/followers"); Exception exception = null; if (resp.StatusCode != HttpStatusCode.OK) { switch ((int)resp.StatusCode) { case 500: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } var users = JsonConvert.DeserializeObject <IEnumerable <User> > ( await resp.Content.ReadAsStringAsync() ); var followers = new TCollection(); using (var e = users.GetEnumerator()) { while (e.MoveNext()) { var f = e.Current; Endpoint.SetupUser(f); followers.Add(f); } } return(followers); } }
public async Task <User> Save() { using (var rest = User.Endpoint.Client.CreateBaseClient()) { var req = new HttpRequestMessage ( new HttpMethod("PATCH"), "admin/users/" + HttpUtility.UrlEncode(User.Username) ); req.Content = new StringContent(ToJson(), Encoding.UTF8, "application/json"); var resp = await rest.SendAsync(req); Exception exception = null; if (resp.StatusCode != HttpStatusCode.OK) { switch ((int)resp.StatusCode) { case 403: case 422: case 500: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } var updatedUser = JsonConvert.DeserializeObject <User> ( await resp.Content.ReadAsStringAsync() ); User.Endpoint.SetupUser(updatedUser); return(updatedUser); } }
/// <summary> /// Starts migration. /// </summary> /// <returns>The new repository</returns> public async Task <Repository> Start() { using (var rest = User.Endpoint.Client.CreateBaseClient()) { var resp = await rest.PostAsync( "repos/migrate", new StringContent(ToJson(), Encoding.UTF8, "application/json") ); Exception exception = null; if (resp.StatusCode != HttpStatusCode.Created) { switch ((int)resp.StatusCode) { case 422: case 500: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } var repo = JsonConvert.DeserializeObject <Repository> ( await resp.Content.ReadAsStringAsync() ); if (repo?.Owner != null) { repo.Owner.Endpoint = User.Endpoint; } return(repo); } }
/// <summary> /// Creates a new user. /// </summary> /// <returns>The new user.</returns> public async Task <User> Create() { using (var rest = Endpoint.Client.CreateBaseClient()) { var resp = await rest.PostAsync( "admin/users", new StringContent(ToJson(), Encoding.UTF8, "application/json") ); Exception exception = null; if (resp.StatusCode != HttpStatusCode.Created) { switch ((int)resp.StatusCode) { case 403: case 422: case 500: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } var newUser = JsonConvert.DeserializeObject <User> ( await resp.Content.ReadAsStringAsync() ); Endpoint.SetupUser(newUser); return(newUser); } }
/// <summary> /// Creates an user object from a HTTP response. /// </summary> /// <param name="resp">The response.</param> /// <returns>The created object.</returns> protected virtual async Task <User> CreateUserObject(HttpResponseMessage resp) { User user = null; if (resp != null) { Exception exception = null; if (resp.StatusCode != HttpStatusCode.OK) { switch (resp.StatusCode) { case HttpStatusCode.NotFound: exception = new ApiException(null, (int)resp.StatusCode, resp.ReasonPhrase); break; case HttpStatusCode.InternalServerError: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } user = JsonConvert.DeserializeObject <User> ( await resp.Content.ReadAsStringAsync() ); } SetupUser(user); return(user); }
/// <summary> /// Deletes that user. /// </summary> public async Task Delete() { var current = await Endpoint.GetCurrent(); if (current.ID == ID) { throw new InvalidOperationException(); // cannot kill yourself ;-) } using (var rest = Endpoint.Client.CreateBaseClient()) { var resp = await rest.DeleteAsync("admin/users/" + HttpUtility.UrlEncode(Username)); Exception exception = null; if (resp.StatusCode != HttpStatusCode.NoContent) { switch ((int)resp.StatusCode) { case 403: case 422: case 500: exception = new ApiException(JsonConvert.DeserializeObject <ApiError> ( await resp.Content.ReadAsStringAsync() ), (int)resp.StatusCode, resp.ReasonPhrase); break; default: exception = new UnexpectedResponseException((int)resp.StatusCode, resp.ReasonPhrase); break; } } if (exception != null) { throw exception; } } }