Bynder/bynder-c-sharp-sdk

Has issue when uploading file

erik-nguyen opened this issue · 4 comments

Hi team,
I have used Bynder SDK in .NET Core 3.1 Console app and tried to upload an image file and I've got 2 exceptions:

  1. It can't deserialize {"status":"ok"} because of Unexpected character encountered while parsing value: {. Path '', line 1, position 1. in the response of the POST request to /api/v4/upload/fi5.BXpTNa7Bhqx6qSyajtT77vYqAYZWv4B2NRT5qNe_BKiwMphQNRPqN3Mv6D2Cde9C0TqqXl7EKxBo9fqN8FZmmPPt1lWP_iZEYc1hPuroVujpt2TGdDQ4PemXbzdkElWN2dPEztVoWE4kBAQ--/ (ApiRequestSender.cs line ~97).
  2. If I use a try-catch to bypass the previous one, I've got 400 Bad Request response when calling polling because in ( FileUploader.cs line 174 ) PollQuery.cs , the ListConverter is never used to convert the Items which is a List to string.

If I use try-catch with the 1 and convert by myself for the 2, the uploading will be successful.

Do you have any ideas?

Thanks and best regards,
Toan K

Hi Toan.

I ran into this same problem and posted a issue that I ended up eventually closing myself after I created a workaround in the SDK. Here is the code change I made in the ApiRequestSender.cs SendRequestAsync method (notice the Try/Catch block managing the return values). The issue is that on Posts, return values were not being handled appropriately.

    public async Task<T> SendRequestAsync<T>(Requests.Request<T> request)
    {
        if (request.Authenticated && !_credentials.AreValid())
        {
            await _semaphore.WaitAsync().ConfigureAwait(false);
            if (!_credentials.AreValid())
            {
                try
                {
                    await RefreshToken().ConfigureAwait(false);
                }
                finally
                {
                    _semaphore.Release();
                }
            }
        }

        var httpRequest = CreateHttpRequest(request);
        var responseString = await _httpSender.SendHttpRequest(httpRequest).ConfigureAwait(false);
        if (!string.IsNullOrEmpty(responseString))
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(responseString);
            }
            catch (Exception ex)
            {
                return (T)(object)responseString;
            }
        }

        return default(T);
    }

Hi damagour,
Thanks for your reply. How about ListConverter ? Do you have any issues with this?

I didn't have to change the ListConverter at all but did need to change the inheritance of of the ITypeToStringConverter.cs file because the Activator.CreateInstance in the QueryDecoder file was returning null on lists. I also needed to add my own DictionaryConverter class to manage dictionary items like Metaproperty Options. Here is the change to the ITypeToStringConverter.cs file, let me know if this helps...

// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

using System;
using Bynder.Sdk.Query.Decoder;

namespace Bynder.Sdk.Api.Converters
{
///


/// Interface for type converters used to decode specific
/// parameters to strings
///

public interface ITypeToStringConverter : IParameterDecoder
{
///
/// Checks if the converter can convert a specific type
///

/// Type to convert from
/// true if it can convert the type
new bool CanConvert(Type typeToConvert);

    /// <summary>
    /// Converts the value to string
    /// </summary>
    /// <param name="value">value to be converted</param>
    /// <returns>converted string value</returns>
    new string Convert(object value);
}

}

@erik-nguyen This issue should be resolved as of version 2.2.2 of the SDK.