Example #1
0
        public async Task <int> ReadAsync(IUser registrar, CancellationToken token = default(CancellationToken))
        {
            if (registrar == null)
            {
                throw new ArgumentException("Registrar should be a valid member");
            }
            string readIdURL = "";

            try
            {
                readIdURL = HFCA_IDENTITY + "/" + EnrollmentId;
                logger.Debug($"identity  url: {readIdURL}, registrar: {registrar.Name}");
                JObject result = await client.HttpGetAsync(readIdURL, registrar, token).ConfigureAwait(false);

                statusCode = result["statusCode"]?.Value <int>() ?? 500;
                if (statusCode < 400)
                {
                    Type           = result["type"]?.Value <string>();
                    MaxEnrollments = result["max_enrollments"]?.Value <int>() ?? 0;
                    Affiliation    = result["affiliation"]?.Value <string>();
                    JArray           attributes = result["attrs"] as JArray;
                    List <Attribute> attrs      = new List <Attribute>();
                    if (attributes != null && attributes.Count > 0)
                    {
                        foreach (JToken attribute in attributes)
                        {
                            Attribute attr = new Attribute(attribute["name"]?.Value <string>(), attribute["value"]?.Value <string>(), attribute["cert"]?.Value <bool>() ?? false);
                            attrs.Add(attr);
                        }
                    }

                    Attributes = attrs;
                    logger.Debug($"identity  url: {readIdURL}, registrar: {registrar} done.");
                }

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