Exemplo n.º 1
0
        // <summary>
        /// Creates a JSON API document for the specified data
        /// </summary>
        /// <param name="resources">the document's "primary data" resource collection</param>
        /// <param name="meta">a meta object that contains non-standard meta-information</param>
        /// <param name="jsonapi">details about the api</param>
        /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
        internal static JDocument For(
            object data,
            JObject meta         = null,
            JApi jsonapi         = null,
            JLinks links         = null,
            JResource[] included = null)
        {
            var document = new JDocument(new JProperty(nameof(data), data));

            if (meta != null)
            {
                document.Add(nameof(meta), meta);
            }
            if (jsonapi != null)
            {
                document.Add(nameof(jsonapi), jsonapi);
            }
            if (links != null && links.HasValues)
            {
                document.Add(nameof(links), links);
            }
            if (included != null)
            {
                document.Add(nameof(included), new JArray(included));
            }
            return(document);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a JSON API document that represents one or more errors
        /// </summary>
        /// <param name="errors">one or more error objects</param>
        /// <param name="meta"></param>
        /// <param name="jsonapi"></param>
        /// <param name="links"></param>
        /// <param name="included"></param>
        public JDocument For(
            IEnumerable <JError> errors,
            JObject meta = null,
            JApi jsonapi = null,
            JLinks links = null,
            IEnumerable <JResource> included = null)
        {
            var validErrors = errors?.Where(JError.IsValid)?.ToArray();

            if (validErrors == null || !validErrors.Any())
            {
                throw new ArgumentNullException(nameof(errors));
            }

            var document = new JDocument(new JProperty(nameof(errors), validErrors));

            if (meta != null)
            {
                document.Add(nameof(meta), meta);
            }
            if (jsonapi != null)
            {
                document.Add(nameof(jsonapi), jsonapi);
            }
            if (links != null && links.HasValues)
            {
                document.Add(nameof(links), links);
            }
            if (included != null)
            {
                document.Add(nameof(included), new JArray(included));
            }
            return(document);
        }
Exemplo n.º 3
0
 // <summary>
 /// Creates a JSON API document for a resource collection
 /// </summary>
 /// <param name="resources">the document's "primary data" resource collection</param>
 /// <param name="meta">a meta object that contains non-standard meta-information</param>
 /// <param name="jsonapi">details about the api</param>
 /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
 public static JDocument For(
     IEnumerable <JResource> resources,
     JObject meta         = null,
     JApi jsonapi         = null,
     JLinks links         = null,
     JResource[] included = null)
 {
     if (resources == null)
     {
         throw new ArgumentNullException(nameof(resources));
     }
     return(For(
                data: resources.ToArray(),
                meta: meta,
                jsonapi: jsonapi,
                links: links,
                included: included
                ));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a JSON API document that represents a single error
 /// </summary>
 /// <param name="error">an error object</param>
 /// <param name="meta"></param>
 /// <param name="jsonapi"></param>
 /// <param name="links"></param>
 /// <param name="included"></param>
 public JDocument For(
     JError error,
     JObject meta = null,
     JApi jsonapi = null,
     JLinks links = null,
     IEnumerable <JResource> included = null)
 {
     if (error == null)
     {
         throw new ArgumentNullException(nameof(error));
     }
     return(For(
                errors: new[] { error },
                meta: meta,
                jsonapi: jsonapi,
                links: links,
                included: included
                ));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a JSON API document for a single resource
        /// </summary>
        /// <param name="resource">the document's "primary data" resource</param>
        /// <param name="meta">a meta object that contains non-standard meta-information</param>
        /// <param name="jsonapi">details about the api</param>
        /// <param name="links">the links relevant to the current resource context</param>
        /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
        public static JDocument For(
            JResource resource,
            JObject meta         = null,
            JApi jsonapi         = null,
            JLinks links         = null,
            JResource[] included = null)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            return(For(
                       data: resource,
                       meta: meta,
                       jsonapi: jsonapi,
                       links: links,
                       included: included
                       ));
        }
Exemplo n.º 6
0
        /// <summary>
        /// An error object MAY have the following members:
        /// </summary>
        /// <param name="id">a unique identifier for this particular occurrence of the problem</param>
        /// <param name="aboutHref">a link that leads to further details about this particular occurrence of the problem.</param>
        /// <param name="aboutMeta">meta data for the about link</param>
        /// <param name="status"> the HTTP status code applicable to this problem, expressed as a string value.</param>
        /// <param name="code">an application-specific error code, expressed as a string value.</param>
        /// <param name="title">a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.</param>
        /// <param name="detail">a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.</param>
        /// <param name="sourcePointer">an object containing references to the source of the error, optionally including any of the following members: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute].</param>
        /// <param name="sourceParameter">a string indicating which URI query parameter caused the error.</param>
        /// <param name="meta">a meta object containing non-standard meta-information about the error.</param>
        public static JError For(
            string id              = null,
            string aboutHref       = null,
            JObject aboutMeta      = null,
            string status          = null,
            string code            = null,
            string title           = null,
            string detail          = null,
            string sourcePointer   = null,
            string sourceParameter = null,
            JObject meta           = null)
        {
            var error = new JError();

            if (id != null)
            {
                error.Add(nameof(id), id);
            }
            var links = JLinks.For(
                JLink.For("about", aboutHref, aboutMeta));

            if (links != null)
            {
                error.Add(nameof(links), links);
            }
            if (status != null)
            {
                error.Add(nameof(status), status);
            }
            if (code != null)
            {
                error.Add(nameof(code), code);
            }
            if (title != null)
            {
                error.Add(nameof(title), title);
            }
            if (detail != null)
            {
                error.Add(nameof(detail), detail);
            }
            var source = new JObject();

            if (sourcePointer != null)
            {
                source.Add("pointer", sourcePointer);
            }
            if (sourceParameter != null)
            {
                source.Add("parameter", sourceParameter);
            }
            if (source.HasValues)
            {
                error.Add(nameof(source), source);
            }
            if (meta != null)
            {
                error.Add(nameof(meta), meta);
            }
            return(error);
        }