How do I do basic Auth in Zend Framework 2 http requests? - http

Good morning all,
I'm trying to call an api which requires basic authentication. I'm trying to use Zend Framework 2 Zend\Http\Request, and I'm not sure which the best way of doing Basic Auth is or should I manually set a header?
I'm trying to force myself to use ZF2 and stop using ZF1 so please bear with me, old habits die hard.
ZF2
$http= new Zend\Http\Request();
$http->setUri('https://api.my.tv/analytics/views/daily/');
$http->....
In ZF1 I used to do the following which is really nice and convenient.
$http = new \Zend_Http_Client();
$http->setUri('https://api.my.tv/analytics/views/daily/');
$http->setAuth($my_api_key, 'x', \Zend_Http_Client::AUTH_BASIC);
$request = $http->request(\Zend_Http_Client::POST);
Is there is a similar method available in ZF2 or should I write my own. To be honest I don't know if I'm better off just using guzzle.
Jujhar

The approach is exactly the same also with Zend\Http\Client:
$client = new \Zend\Http\Client();
$client->setAuth($user, $password, \Zend\Http\Client::AUTH_BASIC);
$client->setUri('http://api.buto.tv/analytics/views/daily/');
$client->setMethod(\Zend\Http\Request::METHOD_POST);
$response = $client->send();

Related

LocustIO: How to do batch request

I started to use LocustIO for load testing a 3rd party API which provides a way to do batch requests (http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_BatchRequests).
How can this be done using LocustIO?
I tried with the following:
def batch(self):
response = self.client.request(method="POST", url="/$batch", auth=("ABC", "DEF"), headers={"ContentType": "multipart/mixed; boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b"}, data="Content-Type: application/http\nContent-Transfer-Encoding: binary\n\nGET putyoururlhere HTTP/1.1\nAccept: application/json\n\n\n")
Auth is something I need to have authentication to the API, but that's not the point of the question and "putyoururlhere" should be replaced with the actual url. Either way, it gives errors when executing the test, so I must be doing something wrong.
People with experience how to do this?
Kind regards!
The data parameter should be your POST body (only), you cant put additional headers in it the way you did. You probably just want to add them as additional entries in the dict you pass as headers
Se the documentation for python requests library for more details. https://requests.readthedocs.io/en/master/

Returning ItemStats from Tridion UGC

I was wondering if anyone can offer any pointers on this one. I'm trying to return ItemStats from the Tridion UGC web service but I'm getting the following error when trying to bind the results:-
The closed type TridionWebUGC.CDS.ItemStat does not have a corresponding LastRatedDate settable property.
An example of code is:-
WebServiceClient ugcCall2 = new WebServiceClient();
Uri uri = new Uri("http://new.ugc.service/odata.svc");
CDS.ContentDeliveryService cds = new CDS.ContentDeliveryService(uri);
var myItemStats = cds.ItemStats.Where(p => p.PublicationId == 68 && p.Id == 17792 && p.Type==16);
I can get comments and ratings with no problem. E.g.
var myComments = cds.Comments.Where(p => p.ItemId == 17805).OrderBy(p => p.CreationDate);
It's just ItemStats that are giving me an issue. Anybody any ideas?
Thanks
John
Unfortunately, the metadata of the UGC WebService is not correct in regards to the ItemsStats. For you it means that the webservice metadata does not expose the fact that the ItemStat entity contains the LastRatedDate property. This makes your .NET proxies not to be aware of this property and makes your query fail.
To work-around this defect you have two option:
Add to your service the following property: cds.IgnoreMissingProperties = true;. Advantage of this approach is that you're done with it in 2 sec. Disadvantage is that you will not be able to access that property (in case you actually use it);
Modify the proxies generated by Visual Studio and manually add that property to the ItemStat class. Advantage of this approach is that you will be able to access the property from your project. Disadvantage is that it's totally not manageable from the coding point of view, you need to be careful when you upgrade or regenerate the proxies and it's easy to make a mistake while manually adding the property.
Note 1: to access the metadata of your webServer from the browser your can go to /odata.svc/$metadata.
Note 2: on a closer look there are 2 properties missing from the webService metadata: LastRatedDate and LastCommentedDate.
Hope this helps.

Grails 2.0 - Upgrading Controller Testing and losing renderArgs and redirectArgs

Problem:
I upgraded from Grails 1.3.7 to Grails 2.0.
I had to then refactor my controller tests to use annotations: #TestFor(controller) and Mock(domain) which took care of a lot of problems.
However, in 1.3.7 controller.renderArgs and controller.redirectArgs were available. This seems to no longer be the case in 2.0 and I am having a difficult time figuring out how to get the data these very convenient variables provided.
It appears that this data will not be provided based off of this Grails Jira posting. The last comment in this post by James Lang, which has no response, is my exact question:
In Grails 2.0 controller unit testing, how can you get to the renderArgs such as action, id, params, etc?
The Jira comments only supply inputs to getting to the response.redirectUrl
Any ideas?
Thanks for the update and the link to the post by Graeme.
Our solution to the problem was to metaclass the controller to get to these variables.
I created corresponding member variables and then added the following code to the setup of the unit test:
controller.metaClass.redirect = { Map map ->
redirectAction = map.action
redirectID = map.id
redirectUrl = map.url
}
controller.metaClass.render = {Map map ->
renderView = map.view
renderModel = map.model
}
At the moment it appears you can't. I saw this forum post where Graeme replied and mentioned it would take a feature request.

Twitter Library for ASP.NET MVC that implements REST API

I been looking for a twitter lib for my ASP.NET NET MVC 3 software, but I need to implement the REST API functions that I nor found in Twitter Helper or Twitterizer. Rest API allow me to use a Find People query.
There is another one could solve my problem?
My suggestion would be to try looking into a library, such as TweetSharp that already has wrappers around a substantial amount of the Twitter API methods. If that doesn't work for you, consider writing your own wrapper around the methods you need using libraries such as HammockRest or RestSharp that have support for working with http, oauth and other such features that may assist you.
Looking better in Twitterizer I found what I need, perform Search People, and other functions. I recommend Twitterizer to use twitter in ASP.NET MVC 3, ´cause the oAuth process has a better code.
Here the Sample code to peform Search People in twitter using Twitterizer :
UserSearchOptions options = new UserSearchOptions();
options.NumberPerPage = 40;
options.Page = 1;
TwitterResponse<TwitterUserCollection> usersResponse = TwitterUser.Search(tokens,pesquisa.Conteudo,options);
if (usersResponse.Result == RequestResult.Success)
{
StringBuilder list = new StringBuilder();
foreach (var u in usersResponse.ResponseObject)
{
list2.Append(u.Name + "-" + u.Id);
}
ViewBag.Result_Twitter = list.ToString();
}

Flex slow first Http request

When i use loader.load(request); for the first time, my flex freeze for 10 secondes before posting the data (i can see the web server result in real time).
However if redo a similar POST with other data but same request.url, it's instantaneous.
// Multi form encoded data
variables = new URLVariables();
variables.user = "aaa";
variables.boardjpg = new URLFileVariable(data.boardBytes, "foo.jpg");
request = new URLRequestBuilder(variables).build();
request.url = "http://localhost:8000/upload/";
loader.load(request);
How can i see what is taking so long ?
Thanks !
Ok, this is an old question, anyway I find it searching for other things so quick adding this
URLFileVariables nor URLRequestBuilder are core classes in AS3, so I guess you're using some custom library to build your request. I don't know which library you use, but it seems that the purpose is to serialize some binary data to build a POST. Serializing usually takes some times the first time (lookup initialization and the like) and goes faster next, a well known example is Remoting in his different flavours

Resources