olivierbon / Placid

Placid is a Craft plugin which makes it easy to consume REST services in your twig templates
77 stars 5 forks source link

Returns NULL after update #15

Closed vegardsando closed 8 years ago

vegardsando commented 8 years ago

I'm using Placid to set up a twitter and facebook feed. Which has been working nicely. However, the Facebook feed suddenly started to return NULL from the placid.request object.

So I updated Placid to the latest version and now the Twitter feed returns NULL as well.

What could be the problem? Both Placid and OAuth is the latest versions (placid: 1.6.2, OAth: 0.9.73)

alecritson commented 8 years ago

Hey @vegardsando

Is there anything in craft/storage/runtime/logs/placid.log or does this exist or if not is there anything in craft.log namespaced Placid - ?

vegardsando commented 8 years ago

Thank you for your quick reply! Yes, I deleted the placid.log and to make sure it's fresh. And this is what it contains:

Unable to parse response body into XML: String could not be parsed as XML in /Users/mustasj/Documents/git/bjugn.no/craft/app/etc/plugins/BasePlugin.php (65) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (449) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (108)

alecritson commented 8 years ago

No problem :) ahh XML is somewhat of a new feature to Placid and now it seems it could be a bit flaky. Do you have the endpoint you are trying to request so I can take a look this end? obviously if its OAuth I will use my own creds to try it, so just the endpoint is fine.

In the meantime it might be worth going back to 1.5.2 untill I can get a fix up

vegardsando commented 8 years ago

Alright, I'll try that version. It seems as though any request gives the same results. And for now I'm just doing a basic search by a single hash tag https://api.twitter.com/1.1/search/tweets.json?q=#bjugn

Previously if there was some error in the request, I got a returned JSON with some info on the object returned. But in this case, the object contains nothing at all.

alecritson commented 8 years ago

Hey @vegardsando

I just managed to do a test and it was actually something really simple that I missed out which was causing the bug. I've managed to test the endpoint you sent successfully and pushed a latest version.

You can download 1.6.3 now, sorry messing you about downloading a previous version! but hopefully this will solve your issue.

vegardsando commented 8 years ago

Good to know! You would have noticed it sooner or later anyway though :) Thanx, I'll try it out

vegardsando commented 8 years ago

I'd like to reopen this one. I might have stumbled onto another issue that might be related to this one, not sure.

I noticed that if my twitter feed contains a Query string that doesn't match anything, I also get a NULL object returned. And my template fails completely. This didn't happen previously though. I tried to insert q as Key and #7160as Value for the twitter feed, which doesn't exist as of now. And I get NULL in return. But it works if I change the query string to something that gives results.

We're planning on trying to get people to use that hash tag, but as of now, nobody does. So it doesn't return anything. But it shouldn't fail just because of that.

The URL passed is https://api.twitter.com/1.1/search/tweets.json

Edit: For now I just use {% if twitter %} so it doesn't fail. Maybe I should do that on every request, just as a precation anyway

alecritson commented 8 years ago

I'm having a hard time replicating this, if I put a hashtag that doesn't have any results i get:

array(2) {
  ["statuses"]=>
  array(0) {
  }
  ["search_metadata"]=>
  array(8) {
    ["completed_in"]=>
    float(0.01)
    ["max_id"]=>
    int(659363017289674752)
    ["max_id_str"]=>
    string(18) "659363017289674752"
    ["query"]=>
    string(10) "%237133360"
    ["refresh_url"]=>
    string(60) "?since_id=659363017289674752&q=%237133360&include_entities=1"
    ["count"]=>
    int(15)
    ["since_id"]=>
    int(0)
    ["since_id_str"]=>
    string(1) "0"
  }
}

The only two scenarios in which you would get a NULL back is if the response body cannot be parsed to the templates or if an exception is thrown when requesting the data (usually when the API returns anything other than a 200 response). The exception message is then written to the logs so you can see whats going wrong.

vegardsando commented 8 years ago

Yes, you're right. I can see in the Placid log that I get a 400 error:

2015/10/28 13:47:22 [error] [plugin] Client error response [status code] 400 [reason phrase] Bad Request [url] https://graph.facebook.com/v2.5/7160no/feed?access_token=41125710-BAHyAvmvj46eD2AgGfKw9d1mCTWRZh2JmREJCuxkT in /Users/mustasj/Documents/git/bjugn.no/craft/app/etc/plugins/BasePlugin.php (65) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (414) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (108)

I have two Placid request object in the template.

`{% set twitter_nyheter = craft.placid.request("twitternyheter") %}
{% for tweet in twitter_nyheter.statuses %}
        Tweet: {{tweet.text}}
{% endfor %}`

And the Facebook feed

`{% set facebook = craft.placid.request('facebook') %}

{% for post in facebook.data %}
    Message: {{post.message}}
{% endfor %}`

And what is truly strange is that this code gives me a NULL error on the facebook object. However, if I comment out the Twitter feed, then there is no NULL error on the facebook feed. Which makes no sense to me at all.

Same visa versa, if I comment out the Facebook feed, then the Twitter feed is ouput correctly. So it's like these two cannot coexist together.

alecritson commented 8 years ago

That is really bizarre, i've not had an issue with more than one request in a template at a time!

I will take a look into this, if you could paste as much from both request configs as you can so I can clone what your doing I will hopefully get the same outcome and be able to squash the error!

vegardsando commented 8 years ago

Yes, I've narrowed down the template now to include only the following:

`{% set twitter_nyheter = craft.placid.request("twitternyheter") %}

{% set facebook = craft.placid.request("facebook") %}
{{dump(facebook)}}`

And this is enough for the page to display NULL. Facebook request in control panel has the URL https://graph.facebook.com/v2.5/7160no/posts I also tried with https://graph.facebook.com/v2.5/7160no/feed. For authentication I use an App created in the Facebook Developer section. Which seems to be working correctly.

Then the twitter request has the URL https://api.twitter.com/1.1/search/tweets.json with the query q as key and #bjugn as Value. Don't think it should matter what the query contains though

I don't think there's anything unusual about this setup, so you should be able to reproduce it. Placid and OAuth are both the latest versions.

The placid log contains the same error 400 as pasted above:

2015/10/28 14:22:25 [error] [plugin] Client error response [status code] 400 [reason phrase] Bad Request [url] https://graph.facebook.com/v2.5/7160no/posts?access_token=41125710-BAHyAvmvj46eD2AgGfKw9d1mCTWRZh2JmREJCuxkT in /Users/mustasj/Documents/git/bjugn.no/craft/app/etc/plugins/BasePlugin.php (65) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (414) in /Users/mustasj/Documents/git/bjugn.no/plugins/placid/services/Placid_RequestsService.php (108)

If I change the order of the requests, and move the set facebook line above the twitter one, and then try to dump the twitter object, then it's the twitter object that returns NULL. With the exact same error message in the log file, only it's twitter's URL that gives a bad request error.

I also noticed that I can use multiple twitter feeds with no problem. But as soon as I mix facebook and Twitter feeds, the error occurs.

alecritson commented 8 years ago

Heu @vegardsando

I've been taking a look and its a strange issue you're getting, not one i've come across lately, but from what I have been able to figure out its being caused by Guzzle.

Basically when you are setting your facebook request you are setting some OAuth credentials and these then get assigned to the Guzzle client which makes the request. The issue is that if you then put another request in the template, although a new client is made, Guzzle seems to keep the OAuth credentials (from a cached state, because for Craft its all one request) from the previous request and attaches them to the Twitter client (not what you want), I need either A) Figure out a way to flush the Guzzle cache or B) figure out a new way to attach OAuth.

I will take a look into this but I'm not sure what the timeframe will be as it might involve a bit of rewriting. However whilst looking for this problem I found another one (standard) which was preventing the correct access token being adding to the facebook requests, i pushed an update (again) which should solve facebook returning null

vegardsando commented 8 years ago

I updated to 1.6.5 now and it seems to be working. As long as I have the {% set facebook = craft.placid.request("facebook") %} before the twitter one. But if it's visa versa, it fails.

However, if I add a second twitter request, it fails again

Update: Scratch that. It seemed to be working because of the cache. Turned that off and it fails like it used to.

vegardsando commented 8 years ago

Any progress? I tried to get around the problem by having the requests in their own separate templates. And then importing those templates into a merge template. But the same thing happens nonetheless. Probably because it's merged into one template as if it was one template.

Any ideas on how to get around this? In any way?

alecritson commented 8 years ago

Hey @vegardsando

Could you try downloading the master branch again and update. I think I may have fixed the issue :pray:

vegardsando commented 8 years ago

Seems it did the trick!

alecritson commented 8 years ago

Awesome, thanks for taking the time to help with these issues! :smile:

vegardsando commented 8 years ago

No problem! It seems as though these are issues you would have encountered sooner or later anyway

alecritson commented 8 years ago

Maybe, but you helped find them sooner! :bow:

RobErskine commented 8 years ago

Hate to be that guy, but I'm running into a similar issue. I'm making a public github request, which works great, however I'm not having much luck with my Stack Overflow call.

My call looks like this on the backend: placid call With the following config

  {% set stackoverflowConfig ={
    path: 'users/' ~ user.stackoverflowUserId,
    method: 'GET'
  } %}
  {% set stackoverflowUser = craft.placid.request('stackOverflowUser', stackoverflowConfig) %}

Where user.stackoverflowUserId = 1047958. The request should look like this: https://api.stackexchange.com/users/1047958?site=stackoverflow, which returns fine. However I'm getting the following error in craft/storage/runtime/logs/placlid.log

******************************************************************************************************
2015/11/17 17:04:05 [error] [plugin] Unable to parse response body into JSON: 5
in /Users/roberskine/Sites/hhcc/hhdevs/craft/app/etc/plugins/BasePlugin.php (65)
in /Users/roberskine/Sites/hhcc/hhdevs/craft/plugins/placid/services/Placid_RequestsService.php (449)
in /Users/roberskine/Sites/hhcc/hhdevs/craft/plugins/placid/services/Placid_RequestsService.php (106)

******************************************************************************************************

Is this a related issue? Or am I doing something entirely incorrect? Thanks in advanced

alecritson commented 8 years ago

Hey @RobErskine

It looks like you are running into issues with the stackexchange api call because Guzzle is not seeing it as valid json, from the Guzzle docs:

JSON Responses You can easily parse and use a JSON response as an array using the json() method of a response. This method will always return an array if the response is valid JSON or if the response body is empty. You will get an exception if you call this method and the response is not valid JSON.

See here

I think this is due to them not using quotes " for their indexes

I ran https://api.stackexchange.com/users/1047958?site=stackoverflow through http://jsonlint.com/ and it got a bunch of errors, which went when I wrapped the indexes with double quotes, so it kinda confirms this.

So unless SE make changes their API return valid JSON I don't think there is a way past this. :-1:

RobErskine commented 8 years ago

Ah bummer. I see what you're saying. I've abandoned placid for this feature and am instead just rolling with javascript ajax GET calls. Hopefully I'll get to use it for something else!