Ejemplo n.º 1
0
        public async Task <int> CreateAsync(IUser registrar, CancellationToken token = default(CancellationToken))
        {
            if (IsDeleted)
            {
                throw new IdentityException("Identity has been deleted");
            }
            if (registrar == null)
            {
                throw new ArgumentException("Registrar should be a valid member");
            }
            string createURL = "";

            try
            {
                createURL = client.GetURL(HFCA_IDENTITY);
                logger.Debug($"identity  url: {createURL}, registrar: {registrar.Name}");
                string  body   = client.ToJson(IdToJsonObject());
                JObject result = await client.HttpPostAsync(createURL, body, registrar, token).ConfigureAwait(false);

                statusCode = result["statusCode"]?.Value <int>() ?? 500;
                if (statusCode >= 400)
                {
                    GetHFCAIdentity(result);
                    logger.Debug($"identity  url: {createURL}, registrar: {registrar} done.");
                }

                IsDeleted = false;
                return(statusCode);
            }
            catch (HTTPException e)
            {
                string            msg = $"[Code: {e.StatusCode}] - Error while creating user '{EnrollmentId}' from url '{createURL}': {e.Message}";
                IdentityException identityException = new IdentityException(msg, e);
                logger.Error(msg);
                throw identityException;
            }
            catch (Exception e)
            {
                string            msg = $"Error while creating user '{EnrollmentId}' from url '{createURL}':  {e.Message}";
                IdentityException identityException = new IdentityException(msg, e);
                logger.Error(msg);
                throw identityException;
            }
        }