Example #1
0
        public FigmaImageResponse GetImages(string fileId, string[] ids, ImageQueryFormat format = ImageQueryFormat.png, float scale = 2)
        {
            var query = new FigmaImageQuery(fileId, ids);

            query.Scale  = scale;
            query.Format = format;
            return(GetImage(query));
        }
Example #2
0
        public static FigmaImageResponse GetFigmaImages(string fileId, IEnumerable <string> ids, ImageQueryFormat format = ImageQueryFormat.png, float scale = 2)
        {
            var query = new FigmaImageQuery(AppContext.Current.Token, fileId, ids);

            query.Scale  = scale;
            query.Format = format;
            return(GetFigmaImage(query));
        }
Example #3
0
        public FigmaImageResponse GetImages(string fileId, IImageNodeRequest[] resourceIds, ImageFormat format = ImageFormat.png, float scale = 2)
        {
            var currentIds = resourceIds.Select(s => s.ResourceId).ToArray();
            var query      = new FigmaImageQuery(fileId, resourceIds);

            query.Scale  = scale;
            query.Format = format;
            return(GetImage(query));
        }
Example #4
0
        //TODO: Change to async multithread
        public static async Task SaveFigmaImageFiles(this FigmaPaint[] paints, string fileId, string directoryPath, string format = ".png")
        {
            var ids    = paints.Select(s => s.ID).ToArray();
            var query  = new FigmaImageQuery(FigmaEnvirontment.Token, fileId, ids);
            var images = FigmaHelper.GetFigmaImage(query);

            if (images != null)
            {
                var urls = paints.Select(s => images.images[s.ID]).ToArray();
                await FigmaHelper.SaveFilesAsync(directoryPath, format, urls);
            }
        }
Example #5
0
        public FigmaImageResponse GetImage(FigmaImageQuery figmaQuery)
        {
            var result = GetContentUrl(figmaQuery,
                                       (e) => {
                var figmaImageUrl = GetFigmaImageUrl(figmaQuery.FileId, figmaQuery.Ids);
                var stringBuilder = new StringBuilder(figmaImageUrl);

                stringBuilder.Append(string.Format("&format={0}", figmaQuery.Format));
                stringBuilder.Append(string.Format("&scale={0}", figmaQuery.Scale));

                if (figmaQuery.Version != null)
                {
                    stringBuilder.Append(string.Format("&version={0}", figmaQuery.Version));
                }
                return(stringBuilder.ToString());
            }
                                       );

            return(JsonConvert.DeserializeObject <FigmaImageResponse> (result));
        }
Example #6
0
        public static FigmaImageResponse GetFigmaImage(FigmaImageQuery figmaQuery)
        {
            var id = string.Join(",", figmaQuery.Ids);

            var stringBuilder = new StringBuilder($"https://api.figma.com/v1/images/{figmaQuery.Document}?ids={id}");

            if (figmaQuery.Scale != null)
            {
                stringBuilder.Append(string.Format("&scale={0}", figmaQuery.Scale));
            }
            if (figmaQuery.Format != null)
            {
                stringBuilder.Append(string.Format("&format={0}", figmaQuery.Format));
            }
            if (figmaQuery.Version != null)
            {
                stringBuilder.Append(string.Format("&version={0}", figmaQuery.Version));
            }

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(stringBuilder.ToString());

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "GET";
            httpWebRequest.Headers["x-figma-token"] = figmaQuery.PersonalAccessToken;

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    return(JsonConvert.DeserializeObject <FigmaImageResponse>(result));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(null);
        }
Example #7
0
        public static FigmaImageResponse GetFigmaImages(string fileId, string[] ids)
        {
            var query = new FigmaImageQuery(FigmaEnvirontment.Token, fileId, ids);

            return(FigmaHelper.GetFigmaImage(query));
        }