How to iterate through the response of this API request in Laravel 9 - laravel-9

I've got this thing which I don't know what it is, an object, a collection... that I am trying to parse. It's the output of below code (response of an Uploadcare PHP API request):
$fileListResponse = $api->file()->listFiles();
$fileCollection = $fileListResponse->getResults();
As you can see in the screenshot below the children are -inner, -api and #elements. The children of the nodes of #elements also have -api and -inner.
When I try the following:
foreach ($fileCollection->elements as $thing) {
...
}
I get the error:
Cannot access protected property Uploadcare\FileCollection::$elements
How can I iterate through the elements?

Related

Understanding seam filter url-pattern and possible conflicts

I made a custom editor plugin, in a Seam 2.2.2 project, which makes file upload this way:
1) config the editor to load my specific xhtml upload page;
2) call the following method inside this page, and return a javascript callback;
public String sendImageToServer()
{
HttpServletRequest request = ServletContexts.instance().getRequest();
try
{
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
processItems(items);//set the file data to specific att
saveOpenAttachment();//save the file to disk
}
//build callback
For this to work I have to put this inside components.xml:
<web:multipart-filter create-temp-files="false"
max-request-size="1024000" url-pattern="*"/>
The attribute create-temp-files do not seems to matter whatever its value.
But url-pattern has to be "" or "/myUploadPage.seam", any other value makes the item list returns empty. Does Anyone know why?
This turns into a problem because when I use a url-pattern that work to this case, every form with enctype="multipart/form-data" in my application stops to submit data. So I end up with other parts of the system crashing.
Could someone help me?
To solve my problem, I changed the solution to be like Seam multipart filter handle requests:
ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
try
{
if (!(request instanceof MultipartRequest))
{
request = unwrapMultipartRequest(request);
}
if (request instanceof MultipartRequest)
{
MultipartRequest multipartRequest = (MultipartRequest) request;
String clientId = "upload";
setFileData(multipartRequest.getFileBytes(clientId));
setFileContentType(multipartRequest.getFileContentType(clientId));
setFileName(multipartRequest.getFileName(clientId));
saveOpenAttachment();
}
}
Now I handle the request like Seam do, and do not need the web:multipart-filter config that was breaking other types of request.

REST GET api - what to return when resource not found

I am using jersey to create rest api. I have GET api which returns xml or json representation of an object instance using JaXB. Everything is fine as long as I can get this instance based on id and return it. But when I don't find instance what should I return. I know 404 response has to be returned. But my method already returns a given type. So how do I setup 404 status is response?
Here is simplified version of my method
#GET
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public GameDAO getGameState(#PathParam("gameId") String gameId)
{
//code to get game instance based on gameId
if(game != null)
{
GameDAO d = new GameDAO(game);
return d; //gets auto converted to xml or json
}
return null; //how to return not found response ?
}
A 404 response is what you want, and I think the best way to get there is by throwing a "not found" WebApplicationException. Here's an example:
throw new WebApplicationException(Response.Status.NOT_FOUND);
There are plenty of ways to customize the error handling; you can find more details in the Jersey docs: https://jersey.java.net/documentation/latest/representations.html

ActivityProfile rest call when using TinCanAPI to update a leaderboard in tetris example

I am trying to update my LMS to be TinCanAPI compliant and while I have been able to capture the statements for the beginning and the end of the Tetris Example game I have been unable to update the ActivityProfile calls to highscores as I am unsure as to what exactly is required to be returned.
I have the following WebAPI controller:
public class ActivitiesController : ApiController
{
private XAPIBiz xvm;
public ActivitiesController()
{
MetaLearning.Data.MetaLearningContext dbcontext = new MetaLearningContext(System.Configuration.ConfigurationManager.ConnectionStrings["MetaLearningContext"].ConnectionString);
xvm = new MetaLearning.Biz.XAPIBiz(dbcontext);
}
// GET api/activity/5
public string Get([FromUri]string profileId, [FromUri]string activityId)
{
return "[{actor:{name:John Paul}, score:9921, date:2014-04-07T14:42:46.492Z},{actor:{name:John Paul}, score:4000, date:2014-04-07T14:42:46.492Z}]";
//var test = profileId;
//var test2 = activityId;
//return "value";
}
.......
}
If I try to view the leaderboard when the course is hosted on Cloud.Scorm.Com then I am able to see the request to /activities/profile?profileId=highscores&activityId=http%3A%2F%2Ftincanapi.com%2FJsTetris_TCAPI
I get the response of
"[{actor:{name:John Paul}, score:9921, date:2014-04-07T14:42:46.492Z},{actor:{name:John Paul}, score:4000, date:2014-04-07T14:42:46.492Z}]"
But If I try to return just the string value of this for testing purposes I get a list of 137 undefined on the leaderboard.
If I return void in this controller action then the leaderboard appears blank.
If I save the text in this file as a json file and save to blob storage and then try to return a URI pointing to the file I get an undefined list of 66 names.
Any help in furthering my understanding of how the ActivityProfile API works would be greatly appreciated. What is the implementation on SCORM cloud?

Does a foreach loop work directly with a JSonStore data object store?

I create a JSonStore with a JSON formatted array of objects.
I have verified it is properly formatted.
I then try to use a dojo forEach loop on it but the JSonStore doesn't seem to have any data in it. I can specify the target in my web page URL and it shows the right data. But using console.log(myJsonStore) shows an object but I don't see the data in Firebug. I also don't see any GET for the service providing the data. It's like specifying the target path in a URL in the browser fires the GET but not when I try to trigger it in the postCreate where my foreach is located.
The answer from Ricardo, i believe is a little incorrect, seeing as the JsonRest.query function returns a dojo.Deferred.
You have a REST call being made asynchroniously through store read api - and once it returns values, it will promise to run whats set as the callback.
Try this for your loop iterator instead
storeObj.query( {} ).then(function ( results ) {
dojo.forEach( results, function( obj ) {
console.log( obj );
});
}
you can do this:
var storeObj = new JsonRest({
target: "/some/resource"
});
storeObj.query({}).forEach(function(obj){console.log(obj);});
that should do the trick

Magento API V2 - add an additional attribute to API response

I'm using the Magento API V2.
When I call salesOrderCreditmemoInfo, I get a response with the credit memo details and a list of the products associated with the order.
But in the list of product items there is no product_type attribute.
I want to manually edit the response to add this attribute.
I tried editing:
app\code\core\Mage\Sales\Model\Order\Creditmemo\Api.php
And replaced:
public function info($creditmemoIncrementId)
{
...
$result['items'] = array();
foreach ($creditmemo->getAllItems() as $item) {
$result['items'][] = $this->_getAttributes($item, 'creditmemo_item');
}
With the following - (basically appending an extra attribute to the array):
public function info($creditmemoIncrementId)
{
...
$result['items'] = array();
foreach ($creditmemo->getAllItems() as $item) {
$product_type = '1'; //test value to check if works
$attribs = $this->_getAttributes($item, 'creditmemo_item');
$attribs['product_type'] = $product_type;
$result['items'][] = $attribs;
}
When I do mage::log($result), the extra attribute seems to be added correctly to the array.
(also indicating that this function is the one getting called)
But it has no impact on the actual API response.
Am I totally looking in the wrong place or is there something else I need to update?
Since You were using SOAP V2, you should update the wsdl.xml to get the output.
For your case it is product_type and refresh cache on server. /tmp to load the new wsdl.xml that already updated. don't forget to go to System -> Cache Management clear all cache.