/// <summary> /// Adds a new user to an existing account /// </summary> /// <param name="users">Object representing the users to add</param> /// <returns>List of created users</returns> /// <exception cref="ArgumentNullException">If email, users or accountId are missing</exception> public CreatedUsers AddUser(AddUsers users) { if (users == null) { throw new ArgumentNullException("NewUser"); } if (string.IsNullOrEmpty(this.Email) == true) { throw new ArgumentNullException("Email"); } if (string.IsNullOrEmpty(this.AccountId) == true) { throw new ArgumentNullException("AccountId"); } try { RequestInfo req = new RequestInfo(); req.RequestContentType = "application/json"; req.AcceptContentType = "application/json"; req.HttpMethod = "POST"; req.LoginEmail = this.Email; req.LoginPassword = string.IsNullOrEmpty(this.ApiPassword) == false ? this.ApiPassword : this.Password; req.DistributorCode = RestSettings.Instance.DistributorCode; req.DistributorPassword = RestSettings.Instance.DistributorPassword; req.IntegratorKey = RestSettings.Instance.IntegratorKey; string method = string.Format("/accounts/{0}/users?api_password=true", this.AccountId); if (String.IsNullOrEmpty(this.BaseUrl)) { req.Uri = string.Format("{0}{1}", RestSettings.Instance.WebServiceUrl, method); } else { req.Uri = string.Format("{0}{1}", this.BaseUrl, "/users?api_password=true"); } req.IntegratorKey = RestSettings.Instance.IntegratorKey; RequestBuilder utils = new RequestBuilder(); utils.Proxy = this.Proxy; List<RequestBody> requestBodies = new List<RequestBody>(); RequestBody rb = new RequestBody(); rb.Text = users.Serialize(); if (string.IsNullOrEmpty(rb.Text) == true) { return null; } requestBodies.Add(rb); req.RequestBody = requestBodies.ToArray(); utils.Request = req; ResponseInfo response = utils.MakeRESTRequest(); this.Trace(utils, response); if (response.StatusCode != HttpStatusCode.Created) { this.ParseErrorResponse(response); return null; } return CreatedUsers.FromJson(response.ResponseText); } catch { } return null; }
/// <summary> /// Serializes Json classes into a string /// </summary> /// <param name="user">NewUser object</param> /// <returns>json string</returns> private string ConstructNewUserJson(NewUser user) { try { if (string.IsNullOrEmpty(user.password.Trim()) == true) { Guid g = Guid.NewGuid(); user.password = g.ToString(); } AddUsers au = new AddUsers(); List<NewUser> users = new List<NewUser>(); users.Add(user); au.newUsers = users.ToArray(); string output = au.Serialize(); return output; } catch { return string.Empty; } }