Create extension using integration studio

Create extension using integration studio

  

Hi all.

I'm trying to use Integration Studio for the first time to create an extension to make an API call with a file upload. Unfortunately, I'm not very well versed with .NET. This is the code I'm trying to replicate on Integration Studio:

public static async Task<PendingAttachment> Upload(string FilePath)
    {
        byte[] byteFile = System.IO.File.ReadAllBytes(FilePath);
        FileInfo fi = new FileInfo(FilePath);

        PendingAttachment pa = null;
        using (var client = new HttpClient())
        {
            string token = "XXXXXXXXXXXXX";
            client.DefaultRequestHeaders.Add("Authorization", "Bearer" + token);
            using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
            {
                content.Add(new StreamContent(new MemoryStream(byteFile)), "attachment", fi.Name);

                using (var message = await client.PostAsync("https://www.yammer.com/api/v1/pending_attachments", content))
                {
                    if (message.IsSuccessStatusCode)
                    {
                        var result = await message.Content.ReadAsStringAsync();
                        pa = Newtonsoft.Json.JsonConvert.DeserializeObject<PendingAttachment>(result);
                        return pa;
                    }
                }
            }
        }
        return null;
    }

So, this is what I've tried. I have created the following structures on Integration Studio:

public class PendingAttachment
    {
        public int id { get; set; }
        public int network_id { get; set; }
        public string url { get; set; }
        public string web_url { get; set; }
        public string type { get; set; }
        public string name { get; set; }
        public string original_name { get; set; }
        public string full_name { get; set; }
        public string description { get; set; }
        public string content_type { get; set; }
        public string content_class { get; set; }
        public string created_at { get; set; }
        public int owner_id { get; set; }
        public bool official { get; set; }
        public string small_icon_url { get; set; }
        public string large_icon_url { get; set; }
        public string download_url { get; set; }
        public string thumbnail_url { get; set; }
        public string preview_url { get; set; }
        public string large_preview_url { get; set; }
        public int size { get; set; }
        public string owner_type { get; set; }
        public string last_uploaded_at { get; set; }
        public int last_uploaded_by_id { get; set; }
        public string last_uploaded_by_type { get; set; }
        public object uuid { get; set; }
        public object transcoded { get; set; }
        public object streaming_url { get; set; }
        public string path { get; set; }
        public int y_id { get; set; }
        public string overlay_url { get; set; }
        public string privacy { get; set; }
        public object group_id { get; set; }
        public bool is_pending { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public string scaled_url { get; set; }
        public Image image { get; set; }
        public int latest_version_id { get; set; }
        public string status { get; set; }
        public Latest_Version latest_version { get; set; }
        public Stats stats { get; set; }
        public string _OriginalFileName { get; set; }
    }

    public class Image
    {
        public string url { get; set; }
        public int size { get; set; }
        public string thumbnail_url { get; set; }
    }

    public class Latest_Version
    {
        public int id { get; set; }
        public int file_id { get; set; }
        public string content_type { get; set; }
        public int size { get; set; }
        public int uploader_id { get; set; }
        public string created_at { get; set; }
        public string path { get; set; }
        public string download_url { get; set; }
        public string thumbnail_url { get; set; }
        public string preview_url { get; set; }
        public string large_preview_url { get; set; }
        public string post_processed_id { get; set; }
        public object streaming_url { get; set; }
        public string revert_url { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public string scaled_url { get; set; }
        public string thumbnail_path { get; set; }
        public string preview_path { get; set; }
        public string large_preview_path { get; set; }
        public string status { get; set; }
    }
    public class Stats
    {
        public int following { get; set; }
        public int followers { get; set; }
        public int updates { get; set; }
        public object first_reply_id { get; set; }
        public object first_reply_at { get; set; }
        public int latest_reply_id { get; set; }
        public string latest_reply_at { get; set; }
        public int shares { get; set; }
    }

As this extension will be used for mobile, I have added 2 input parameters, to send the FileData as well as the FileName, to avoid having to extract that data. I have added an output parameter with the data type PendingAttachment which is the structure mentioned above.

So the issue I'm facing is that since it is an async method, Visual Studio isn't allowing me to have an output parameter. So is there a way to fix this?

P.S: Yes, I have tried implementing this normally on Service Studio, by referencing the Box File Upload plugin. It did not work (gave a 500 error). I have also been assured that this method works properly.

Thank you for your help!



Solution

Hi Saurav,

I noticed that you wrote this: "As this extension will be used for mobile". What do you mean by this? You cannot direclty use extensions in Mobile Apps, though you can call an Action from an Extension as a Server Action. But this will only work when the app is online.

In general though, I can only advise you to stay away from C# if you're not comfortable with it, or run the risk of writing inefficient or bad code that you don't know how it works (or not).

Solution