Third Light Developer Exchange

Code and templating community forum for developers and software integrators

You are not logged in.

Announcement

If you wish to join the Developer Exchange, please contact your account manager - this is to avoid unnecessary spam in the forums. Many thanks for your understanding.

#1 2015-01-06 17:12:55

AndyNewman
Member
Registered: 2015-01-06
Posts: 2

API Woes - Session Expiry / returning Null

Hi all,

We have hit a bit of a dead end with our JSON API integration, we're basically pulling in a list of assets into a webpage with filters and download options.

As of last month we had the integration working as desired (was nice and straight forward) but unfortunately every couple of weeks we find that the JSON API stops responding or returns NULL / empty, we had originally thought this may be down to our stage / production environments sharing exactly the same Third light API Key and a conflict was occuring, which we have moved to resolve with each environment having it's own key.

A reboot of the application pools upon the servers always seems to restore the integration, so my theory is that the session is expiring due to a break in the persistent connection between our application and the thirdlight solution, we are using the following url end point in our solution.

"api.json.tlx"

Wondering if anyone else has experienced a similar scenario where the API responds with an empty list until reboot.

Cheers

Andy.

Offline

#2 2015-01-06 17:35:25

dominic
Third Light Staff
Registered: 2013-06-06
Posts: 119

Re: API Woes - Session Expiry / returning Null

Hi Andy,

Could you post a full request/response pair that goes wrong, including headers?

Sessions created through Core.LoginWithKey do expire - but this would be after 16 hours, which is much less then the multiple weeks that you are reporting. You can detect if this has occurred by making a call to Core.GetUserDetails - in the event that the session had expired, this would return just 'type' and 'userref' keys.

Dominic

Offline

#3 2015-01-06 17:36:57

BezerkSquid
Member
Registered: 2015-01-06
Posts: 2

Re: API Woes - Session Expiry / returning Null

Hello AndyNewman, Can you post your code here so I can take a look buddy?

Offline

#4 2015-01-06 18:03:23

AndyNewman
Member
Registered: 2015-01-06
Posts: 2

Re: API Woes - Session Expiry / returning Null

Hi guys, interesting point re: Session expiry.

In terms of debug code, the issue is difficult to replicate, we've put more advanced logging around the API calls so should know this once it nexts goes wrong.

Here's an example of our call for featured campaigns (custom meta), we get a response of null / object not found when it fails.

 private static readonly string ApiKey = WebConfigurationManager.AppSettings["ThirdLightApiKey"];

// e.g. "domain.thirdlight.com/api.json.tlx"
private static readonly string ApiBaseUrl = WebConfigurationManager.AppSettings["ThirdLightApiUrl"];
private static readonly string DownloadUrl = WebConfigurationManager.AppSettings["ThirdLightDownloadUrl"];

//Query

private static string SendRequest(Request request)
        {
            var reply = String.Empty;
            try
            {
                // Add SessionId to request if we have one.
                if (!_sessionId.IsNullOrWhiteSpace())
                {
                    request.SessionId = _sessionId;
                }

                // Create a WecLient to send and recieve the request.
                var webClient = new WebClient();

                // Serialise the request data to a JSON object.
                var json = JsonConvert.SerializeObject(request);

                // Debug request data
                System.Diagnostics.Debug.WriteLine(json);

                // Set the encoding and content-type.
                webClient.Encoding = System.Text.Encoding.UTF8;
                webClient.Headers["Content-Type"] = "application/json";

                // POST the request and wait for the response.+
                reply = webClient.UploadString(ApiBaseUrl, json);
                
                // Debug reply data
                System.Diagnostics.Debug.WriteLine(reply);
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(ThirdLightService), "SendRequest() failed.", ex);
            }

            return reply; 
        }

//Example function
public static List<LibraryResultViewModel> FeaturedCampaigns()
        {
            var imageResults = new List<LibraryResultViewModel>();

            try
            {
                var request = new Request
                {
                    Action = "Search.AdvancedSearch"
                };
                request.Parameters.Add("query", new List<SearchParameter>
                {
                    new SearchParameter
                    {
                        FieldId = 144, // Featured campaign
                        ConditionId = 3, // Is
                        Value = "FEATURED"
                    }
                });
                request.Parameters.Add("fields", new
                {
                    metadata = "true",
                    publishedFiles = "true"
                });

                var results = JsonConvert.DeserializeObject<SearchResponse>(SubmitRequest(request));

                LogThirdLightResponse(results, "FeaturedCampaigns");

                if (results != null)
                {
                    
                    if (results.Parameters.Files == null || !results.Parameters.Files.Any()) return imageResults;

                    imageResults.AddRange(results.Parameters.Files.Select(file => new LibraryResultViewModel
                    {
                        Description = file.metadata.description,
                        FileName = file.filename,
                        FileUrl = GetDownloadFileUrl(file),
                        Id = file.id,
                        ImageUrl = file.previewUrl,
                        Title = file.metadata.object_name
                    }));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(ThirdLightService), "FeaturedCampaigns() failed.", ex);
            }

            return imageResults;
        }

Cheers

Andy

Last edited by AndyNewman (2015-01-06 18:16:34)

Offline

#5 2015-01-06 18:56:00

BezerkSquid
Member
Registered: 2015-01-06
Posts: 2

Re: API Woes - Session Expiry / returning Null

Perhaps try to debug the exact response from the server.  System.Diagnostics.Debug.WriteLine(reply);

You also want to fix the null pointer here:

if (results != null)
{

    if (results.Parameters.Files == null || !results.Parameters.Files.Any()) return imageResults;

Offline

Board footer