incraigulous / contentful-sdk

A modern PHP SDK for Contentful delivery and management APIs.
MIT License
13 stars 11 forks source link

Asset creation #7

Closed vburghelea closed 9 years ago

vburghelea commented 9 years ago

Hey, Craig,

Very nice work on the API implementation, just wanted to try to fix either my perception or the real issues with the Contentful API that I uncovered.

My biggest problem is with either the implementation of parameters in the Entry / Asset PayloadBuilders not allowing the mentioning of ID, or the Contentful API's lack of RESTfulness. What I mean is that when POSTing a new entity (Entry / Asset / whatever) there's nothing in the response related to the generated ID, which I'm guessing is because the data is added via async operations.

This hasn't been a big issue (just an annoyance) so far when working with Entries, but it really hurts on the Assets side. I'm creating an Asset and then searching for it in order to get an ID to pass to process(). Very crappy. Searching assets, by the way, isn't currently working for me, neither by title or original file name.

The Management API documentation suggests the possibility to create assets by specifying the ID as part of the url, which would make it easier, I guess, to use the functionality via your SDK - if we could pass the id to the constructor for Assets. What do you think?

Cheers, Elis.

vburghelea commented 9 years ago

Well, my vent aside, it seems the Assets are, indeed, created asynchronously (including the non-binary metadata blueprint), which makes the regular metadata unavailable for searching immediately. I timed asset searches at between 1.5 and 3 seconds after the initial creation. I can keep polling, so it's not that big a deal.

However, now the process step isn't working - but that seems to be more of a Contentful issue :)

I think the discussion about allowing (at least) Assets to be created with a pre-generated ID stands. Entries can be created pretty quickly, but it seems like Assets are not. Searching for one will definitely require polling, so if we can give them pre-assigned ID's we remove a headache.

Cheers, Elis.

incraigulous commented 9 years ago

Hey Elis! Thanks for pointing this out. I think you'll just need to use the put method instead of the post method.

You would end up with something like:

$result = $managementSDK->assets()->put('MY_CUSTOM_ID', new Asset([
                    new AssetField('title', 'Bacon Pancakes'),
                    new File("image/jpeg", "example.jpg", "https://example.com/example.jpg")
                ])
        );

Does that work for you? If so, I'll update the documentation to reflect.

vburghelea commented 9 years ago

Hey, Craig,

I was apparently too tired yesterday evening and somehow completely glossed over the fact that the Contentful API process() method is called via PUT. I did a curl PUT call and it worked just fine, however from the code side it looks like there's a GET call triggered afterwards when attempting to process() - but I don't know where it's coming from. I'm a little bit lost in the abstractions, maybe you could help?

Cheers, Elis.

vburghelea commented 9 years ago

I found the problem - the delete() method is called in process() instead of put(). I'll make a pull request.

incraigulous commented 9 years ago

Hey Elis,

I merged in your fix. I'll tag a new release probably later on today. You shouldn't have any issues working off of master or your own fork until then if you want.

I'll go ahead and update the documentation to demonstrate how to create an asset/entry while specifying an ID. Thanks for all your help man. You rock!