smartsheet-platform / smartsheet-csharp-sdk

Library that uses C# to connect to Smartsheet services.
Apache License 2.0
35 stars 30 forks source link

There was an issue connecting c# #124

Open pavelgaiko opened 4 years ago

pavelgaiko commented 4 years ago

Hi! I use VS 2019 on my laptop (windows 10 Pro, 1903, x64)

create a new c# console app, add sdk from NuGet

NetFramework = 4.5.2, Nlog+Newtonsoft+RestShart = last versoin

next step

//add in project

using Smartsheet.Api;

using Smartsheet.Api.Internal.Http;

using Smartsheet.Api.Models;

//and build connection

SmartsheetClient sm = new SmartsheetBuilder().SetAccessToken("myToken").Build();

// after - get all sheets

PaginatedResult sheets = sm.SheetResources.ListSheets(

   new SheetInclusion[] { SheetInclusion.SOURCE },

   new PaginationParameters(

    true,      // includeAll

    null,      // int pageSize

    null)      // int page

  );

get error - Smartsheet.Api.Internal.Http.HttpClientException: "There was an issue connecting." PS: in project from git i have same error, RetryHttpClient() and ProxyHttpClient("localhost", 8888) do not work :(

pavelgaiko commented 4 years ago

public class DefaultHttpClient : HttpClient used ->> protected readonly RestClient httpClient; In public virtual HttpResponse Request(HttpRequest smartsheetRequest, string objectType, string file, string fileType)
httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority)); and In public virtual HttpResponse Request(HttpRequest smartsheetRequest) httpClient.BaseUrl = null; // is default

restResponse = httpClient.Execute(restRequest); (if httpClient.BaseUrl = null return HttpClientException with "There was an issue connecting.")

if add in public virtual HttpResponse Request(HttpRequest smartsheetRequest) httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority)); it will be worked, but my laptot use russian culture and httpClient.UserAgent generate value like - "smartsheet-csharp-sdk/2.101.0.0/sdk-csharp-sample-v2/Майкрософт Windows 10 Pro-.NETFramework,Version=v4.5.2" - with that I have same error Smartsheet.Api.Internal.Http.HttpClientException: "There was an issue connecting."

finally, if use api like

static void Main(string[] args)
        {
            // Initialize client
            SmartsheetClient smartsheet = new SmartsheetBuilder()
                .SetAccessToken("MY_TOKEN")       
                .Build();

            UserProfile userProfile = smartsheet.UserResources.GetCurrentUser();

            Console.ReadLine();
        }

I must rewrite in class DefaultHttpClient function public virtual HttpResponse Request(HttpRequest smartsheetRequest) us

public virtual HttpResponse Request(HttpRequest smartsheetRequest)
        {
            Util.ThrowIfNull(smartsheetRequest);
            if (smartsheetRequest.Uri == null)
            {
                 throw new System.ArgumentException("A Request URI is required.");
            }

            int attempt = 0;
            HttpResponse smartsheetResponse = null;

            Stopwatch totalElapsed = new Stopwatch();
            totalElapsed.Start();

            while (true)
            {
                smartsheetResponse = new HttpResponse();

                restRequest = CreateRestRequest(smartsheetRequest);

                // Set HTTP Headers
                if (smartsheetRequest.Headers != null)
                {
                    foreach (KeyValuePair<string, string> header in smartsheetRequest.Headers)
                    {
                        restRequest.AddHeader(header.Key, header.Value);
                    }
                }

                if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
                {
                    restRequest.AddParameter(smartsheetRequest.Entity.ContentType, Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()), 
                        smartsheetRequest.Entity.ContentType, ParameterType.RequestBody);
                }

                Stopwatch timer = new Stopwatch();
               // my code
                httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority));
                httpClient.UserAgent = null; // or "smartsheet-csharp-sdk/2.101.0.0/sdk-csharp-sample-v2";

                // Make the HTTP request
                timer.Start();
                restResponse = httpClient.Execute(restRequest);
                timer.Stop();

                LogRequest(restRequest, restResponse, timer.ElapsedMilliseconds);

                if (restResponse.ResponseStatus == ResponseStatus.Error)
                {
                    throw new HttpClientException("There was an issue connecting.");
                }

                // Set returned Headers
                smartsheetResponse.Headers = new Dictionary<string, string>();
                foreach (var header in restResponse.Headers)
                {
                    smartsheetResponse.Headers[header.Name] = (String)header.Value;
                }
                smartsheetResponse.StatusCode = restResponse.StatusCode;

                // Set returned entities
                if (restResponse.Content != null)
                {
                    HttpEntity entity = new HttpEntity();
                    entity.ContentType = restResponse.ContentType;
                    entity.ContentLength = restResponse.ContentLength;

                    entity.Content = restResponse.RawBytes;
                    smartsheetResponse.Entity = entity;
                }

                if (smartsheetResponse.StatusCode == HttpStatusCode.OK)
                {
                    break;
                }

                if (!ShouldRetry(++attempt, totalElapsed.ElapsedMilliseconds, smartsheetResponse))
                {
                    break;
                }
            }
            return smartsheetResponse;
        }
pavelgaiko commented 4 years ago

file Ulis.cs

string result = string.Empty;
            ManagementObjectCollection.ManagementObjectEnumerator enumerator = null;

            try
            {
                enumerator = (
                        new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem")
                    ).Get()
                    .GetEnumerator();

                if (enumerator.MoveNext())
                {
                    result = ((ManagementObject)enumerator.Current)["Caption"].ToString(); // return cyrillic characters for RU PC
                }
            }
            catch (UnauthorizedAccessException)
            {
                // Hosted solution - Many not allow access to WMI
                return "Hosted";
            }