Accessing Twitter with Akka Camel to return JSON - twitter

I was using an HTTP POST method using the URL
"https://stream.twitter.com/1.1/statuses/filter.json" and in the body I was posting the key/value I wanted to get tweets from - for example "track=london". This was working fine.
Now I am trying to switch to AKKA-CAMEL and I am using their twitter consumer. I am using an endpoint URL of:
def endpointUri: String = s"twitter:////search?type=direct&keywords=${Settings.queryList()}&consumerKey=${tweeterCredentials.consumerKey}&consumerSecret=${tweeterCredentials.consumerSecret}&accessToken=${tweeterCredentials.accessToken}&accessTokenSecret=${tweeterCredentials.accessTokenSecret}"
I get a response from twitter but it is not in JSON and it is not the same information about the tweet as before. It just return the tweet text but before I was getting the whole metadata which I need to analyze.
Does somebody knows how to configure Camel URI to return JSON and the whole metadata as before?
Thanks

I got this to work by using the following syntax:
def endpointUri: String = s"twitter://streaming/filter?type=event&keywords=${Settings.queryList()}&consumerKey=${tweeterCredentials.consumerKey}&consumerSecret=${tweeterCredentials.consumerSecret}&accessToken=${tweeterCredentials.accessToken}&accessTokenSecret=${tweeterCredentials.accessTokenSecret}"
Where: Settings.queryList return a comma separated list of keyworkds. The object tweeterCredentials holds the keys from Tweeter to access the site.
Also it is necessary to set autoAck like this in Camel:
override def autoAck = true
This prevents a timeout exception.

Related

Using Doubleclick Bid Manager API

I am writing a Python program to read line items from Doubleclick Bid Manager using its API, but facing issue while making a query to getlineitems.
To Authenticate, here is my code:
authorize_url = flow.step1_get_authorize_url()
# After entering the verification for code,
code = raw_input('Code: ').strip()
credential = flow.step2_exchange(code)
I successfully get my credential as a oauth2client.client.OAuth2Credentials object.
Then using following parameters, I make a http request.
params = dict(
api_key='xxxxxxxxxxxxxxxxxx' # client secret from the API JSON file
)
url = 'https://developers.google.com/bid-manager/v1/lineitems/downloadlineitems'
r = requests.get(url = url, params=params)
But my request returns 404; not found code. Per the API guidelines (https://developers.google.com/bid-manager/v1/lineitems/downloadlineitems), you need to make following HTTP request.
POST https://www.googleapis.com/doubleclickbidmanager/v1/lineitems/downloadlineitems?key={YOUR_API_KEY}
Any help will be appreciated.
I don't know much about python, but since the call is a POST request, should you be using requests.get()? Is there a request.post() method?

routing help in Txroutes library

I need to route the following to a function :
http://www.example.com/docs/?key1=value1&key2=value2
And the route code for it is
dispatcher.connect(name='xyz', route='/docs/{item}', controller=c, action='docs')
so, can you help me with the “route” part as in what should come there so for an incoming request the action gets called.
As in how to relate route='/docs/{item}' and /docs/?key1=value1&key2=value2
HTTP GET request (/docs/) followed by a query string (?key1=value1&key2=value2). You'd use Twisted's normal facilities for accessing the query string.
def docs(self, request):
return '<html><body>Got %s args</body></html>' % request.args
to retrieve from the array use request.args['key1'][0] to get 'value1'.

HTTPClient GetAsync post object

We currently have a generic MVC method that GET's data from ASP.NET Web API
public static T Get<T>(string apiURI, object p)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(Config.API_BaseSite);
HttpResponseMessage response = client.GetAsync(apiURI).Result;
// Check that response was successful or throw exception
if (response.IsSuccessStatusCode == false)
{
string responseBody = response.Content.ReadAsStringAsync().Result;
throw new HttpException((int)response.StatusCode, responseBody);
}
T res = response.Content.ReadAsAsync<T>().Result;
return (T)res;
}
}
Our question is:- obviously, we can not send 'p' as you would with a post,
client.PostAsync(apiURI, new StringContent(p.ToString(), Encoding.UTF8, "application/json")
but how we go about sending this object / JSON with a get.
We have seen sending it as part of the URL, however, is there an alternative?
GET sends the values with the query string (end of url), in regards to "but how we go about sending this object / JSON with a get. We have seen sending it as part of the URL, however, is there an alternative?".
The alternative is POST or PUT.
PUT is best used when the user creates the key/url. You can look at examples such as cnn.com - where the URL's are just short versions of the article title. You want to PUT a page at that URL.
Example:
http://newday.blogs.cnn.com/2014/03/19/five-things-to-know-for-your-new-day-wednesday-march-19-2014/?hpt=hp_t2
has the url of "five-things-to-know-for-your-new-day-wednesday-march-19-2014", which was generated from the article title of "Five Things to Know for Your New Day – Wednesday, March 19, 2014"
In general, you should follow these guidelines:
Use GET when you want to fetch data from the server. Think of search engines. You can see your search query in the query string. You can also book mark it. It doesn't change anything on the server at all.
Use POST when you want to create a resource.
Use PUT when you want to create resources, but it also overwrites them. If you PUT an object twice, the servers state is only changed once. The opposite is true for POST
Use DELETE when you want to delete stuff
Neither POST nor PUT use the query string. GET does

Stripe Connect - Handling Response

In the process of checking out Stripe Connect.
I've got to the stage where I can access the token (which is explained here https://stripe.com/docs/connect/oauth#token-request) but I'm having trouble accessing other parts of the object.
To retrieve the object I use:
response = #client.auth_code.get_token(code, params)
and to get at the token I use:
#token = response.token #strangely it isn't response.access_token
My problem is that I can't access other parts of Stripe's response which contains amongst other things a publishable key.
I have debugged the response and I get this (trimmed down and sensitive data altered):
...#token="sk_test_abc123", #refresh_token="rt_abc123", #expires_in=nil, #expires_at=nil, #options={:mode=>:header, :header_format=>"Bearer %s", :param_name=>"bearer_token"}, #params={"livemode"=>false, "token_type"=>"bearer", "stripe_publishable_key"=>"pk_test_abc123", "stripe_user_id"=>"abc123", "scope"=>"read_write"}
Can anyone tell me how to access the stripe_publishable_key in this response?
I've tried:
response.params[:stripe_publishable_key]
and
response.params.stripe_publishable_key
Have you tried response.params['stripe_publishable_key']
The hash is using strings rather than symbols for keys. You have to access the values using a string like this:
response.params["stripe_publishable_key"]

How to access items in an api return string in Rails

I'm accessing the Google places API using HTTParty. Here's my code.
query = GOOGLE_API["search"].merge(:location => latlng.join(","))
response = HTTParty.get(GOOGLE_API["search"]["url"], :query => query)
#businessInfo = response
#business info contains a string of data from Google as expected, however, when I try to acccess the items using #businessInfo.index(0).item like I would with data from my database, I get nil.
This is a sample of what is contained in the variable -- {"html_attributions"=>[], "results"=>[{"geometry"=>{"location"=>{"lat"=>33.762835, "lng"=>-84.392724}}, "icon"=>"http://maps.gstatic.com/mapfiles/place_api/icons/art_gallery-71.png", "id"=>"c551a5fdc78c273e6f498aa920733037199ebe01", "name"=>"World of Coca-Cola", "reference"=>"CnRoAAAAvpKSnn971Ur5ABYStk-EJfMvyFFFlBtd9LzwRT4H-PF50vS0CQtDCGkoW0QqKLHwFHV7Qmj32bgg-KjthkVENsBpGPxNAq_vcg4do-TQyi97y6mKxf3qUgoGxzGHePEAcqg15aATTl6Xdsq7Pl2b6hIQpzVIr4KO4ZDSx4tIqcH-ARoUPn-9yBSLi35lBM7gFm2KTPGREa0", "types"=>["art_gallery", "store", "establishment"], "vicinity"=>"Baker Street Northwest, Atlanta"}, {"geometry"=>{"location"=>{"lat"=>33.759925, "lng"=>-84.387158}}, "icon"=>"http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", "id"=>"32815dbf0963cb55dee871b96cc5100335f40400", "name"=>"Hard Rock Cafe Atlanta", "reference"=>"CnRtAAAApKIX3M3emqAzdsN3f0ntsi-M-
My question is, What syntax do I use to access the items and values so that I can work with them.
Thanks.
Your response object is an hash so you can access your response content in the following way:
puts response["results"]

Resources