SparkPost / gosparkpost

SparkPost client library for the Go Programming Language
https://www.sparkpost.com/
Other
62 stars 40 forks source link

Impossible to publish a draft template #102

Closed roosmaa closed 7 years ago

roosmaa commented 7 years ago

I've created a draft template using this library. However, it is literally impossible to publish it using the provided APIs. Mostly getting the [{"message":"Resource could not be found","code":"","description":""}] error from the server.

A simplified version of what I'm doing, is more or less like so:

tmpl := sp.Template{
    ID: "my-template-id",
}

// Get draft
_, err := spClient.TemplateGet(&tmpl, true)
if err != nil {
    panic(err)
}

// Publish draft
tmpl.Published = true
_, err := spClient.TemplateUpdate(&tmpl)
if err != nil {
    panic(err)
}

It appears that this library is setting the update_published GET parameter in addition to passing in the published field as JSON. Which seem to make the SparkPost API very much confused. Thus it ends up returning the misleading error above.

_Though, according to the API docs, this shouldn't really happen as the update_published should be ignored when published field is true. But, eh..._

Removing the update_published from the TemplateUpdateContext function seems to make things work as they should. Inspecting the web-app it doesn't use the update_published query parameter either.

Long story short: it would be nice to be able to publish draft templates using this library. 😃

yargevad commented 7 years ago

You're right, Template.Published and the update_published parameters should be separated. There may be some doc updates we can do to make that more clear too.

yargevad commented 7 years ago

For posterity, Resource could not be found is being returned because update_published means "get the version of the template that's currently published, and update it using this template," and the first part is failing since there is no currently published template.

yargevad commented 7 years ago

I've opened a ticket on our internal tracker to add some more detail to that Resource could not be found error message. The PR just above adds a parameter to TemplateUpdate to control update_published separately from Template.Published

Thanks for the feedback @roosmaa !

kakysha commented 7 years ago

You still can't publish draft template using short way (only sending published: true in Update method (https://developer.sparkpost.com/api/templates.html#templates-update-put , 2nd example).

I digged into the code and there are many checks of supplied struct in Update method before sending it to API, thus it seems easier to just make a separate set of methods like Publish for that short way. I implemented it, but without tests, can make a PR if you wish. @yargevad

yargevad commented 7 years ago

Yes please @kakysha, that would be great, thanks

yargevad commented 7 years ago

I'm going to pull in the PR from @kakysha under this issue

yargevad commented 7 years ago

117 which includes the changes from above closes this for real