Exemplo n.º 1
0
        public void BasicRewrite()
        {
            var json = "{ \"name\": \"foo\", \"@microsoft.graph.downloadUrl\": \"https://foo/bar/baz\", \"@microsoft.graph.conflictBehavior\": \"fail\" }";
            var map  = new Dictionary <string, string>();

            map.Add("@microsoft.graph.downloadUrl", "@oneDrive.downloadUrl");
            map.Add("@microsoft.graph.", "@oneDrive.");
            var output = JsonRewriter.RewriteJsonProperties(json, map, new Error.IssueLogger());
        }
        public static void RewriteResponseBodyNamespaces(this HttpResponse response, IServiceAccount account, IssueLogger issues)
        {
            if (account.Transformations?.Response?.Properties == null)
            {
                return;
            }
            if (!response.IsMatchingContentType("application/json"))
            {
                return;
            }
            if (response.Body.Length == 0)
            {
                return;
            }

            var translatedBody = JsonRewriter.RewriteJsonProperties(response.Body, account.Transformations.Response.Properties, issues);

            response.Body = translatedBody;
        }
        /// <summary>
        /// Apply any namespace translations defined in the account to the parameters in the request body
        /// </summary>
        /// <param name="request"></param>
        /// <param name="account"></param>
        public static void RewriteRequestBodyNamespaces(this HttpRequest request, IServiceAccount account, IssueLogger issues)
        {
            if (account.Transformations?.Request?.Properties == null)
            {
                return;
            }

            if (request.IsMatchingContentType("application/json"))
            {
                var translatedBody = JsonRewriter.RewriteJsonProperties(request.Body, account.Transformations.Request.Properties, issues);
                request.Body = translatedBody;
            }
            else if (request.IsMatchingContentType("multipart/related"))
            {
                var parsedBody     = new MultipartMime.MultipartMimeContent(request.ContentType, request.Body);
                var partsToRewrite = parsedBody.PartsWithContentType("application/json");
                foreach (var part in partsToRewrite)
                {
                    part.Body = JsonRewriter.RewriteJsonProperties(part.Body, account.Transformations.Request.Properties, issues);
                }
                request.Body = parsedBody.ToString();
            }
        }