Skip to content

Instantly share code, notes, and snippets.

@gozuk16
Last active August 29, 2015 14:01
Show Gist options
  • Save gozuk16/5ac1111a382e59a5b9b0 to your computer and use it in GitHub Desktop.
Save gozuk16/5ac1111a382e59a5b9b0 to your computer and use it in GitHub Desktop.
MT Rebuild entry
@Grab('org.apache.httpcomponents:httpclient:4.3.3')
import org.apache.http.*
import org.apache.http.client.*
import org.apache.http.client.params.*
import org.apache.http.client.methods.*
import org.apache.http.client.entity.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.util.EntityUtils
import groovy.json.*
api_host = "https://your-host"
api_base = "your-mt-api.cgi/v1"
def auth() {
def uri = "${api_host}/${api_base}/authentication"
HttpPost post = new HttpPost(uri)
List<Header> headers = new ArrayList<Header>()
headers.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"))
HttpClient httpclient = HttpClientBuilder.create().setDefaultHeaders(headers).build()
List<NameValuePair> params = new ArrayList<NameValuePair>()
params.add(new BasicNameValuePair("username", "foo_uid"))
params.add(new BasicNameValuePair("password", "foo_pass"))
params.add(new BasicNameValuePair("remember", "0"))
params.add(new BasicNameValuePair("clientId", "mt_rebuild"))
post.setEntity(new UrlEncodedFormEntity(params))
response = httpclient.execute(post)
if (response.statusLine.getStatusCode() == 200) {
entity = new JsonSlurper().parseText(response.getEntity().getContent().text)
return entity.get("accessToken")
}
}
def rebuild(accessToken) {
def rebuild_entry = "entry_id"
def uri = "${api_host}/${api_base}/publish/entries?ids=${rebuild_entry}"
HttpGet get = new HttpGet(uri)
List<Header> headers = new ArrayList<Header>()
headers.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"))
headers.add(new BasicHeader("X-MT-Authorization", "MTAuth accessToken=${accessToken}"))
HttpClient httpclient = HttpClientBuilder.create().setDefaultHeaders(headers).build()
response = httpclient.execute(get)
if (response.statusLine.getStatusCode() == 200) {
entity = new JsonSlurper().parseText(response.getEntity().getContent().text)
response.getHeaders('X-MT-Next-Phase-URL').each {
if (entity.get("status") != "Complete") {
rebuild_next(accessToken, "${api_host}/${api_base}${it.value}")
}
}
}
}
def rebuild_next(accessToken, uri) {
HttpGet get = new HttpGet(uri)
List<Header> headers = new ArrayList<Header>()
headers.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"))
headers.add(new BasicHeader("X-MT-Authorization", "MTAuth accessToken=${accessToken}"))
HttpClient httpclient = HttpClientBuilder.create().setDefaultHeaders(headers).build()
response = httpclient.execute(get)
if (response.statusLine.getStatusCode() == 200) {
entity = new JsonSlurper().parseText(response.getEntity().getContent().text)
response.getHeaders('X-MT-Next-Phase-URL').each {
if (entity.get("status") != "Complete") {
rebuild_next(accessToken, "${api_host}/${api_base}/${it.value}")
}
}
}
}
rebuild(auth())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment