2012年2月18日土曜日

groovyでdrupalのコメントを更新する

groovyでdrupalのコメントを更新するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.*
import org.apache.http.client.*
import org.apache.http.client.entity.*
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.protocol.*

// ログイン
httpclient = new DefaultHttpClient()
method = new HttpPost("http://localhost/drupal/rest/user/login")
values = [
  new BasicNameValuePair("username", "drupal"),
  new BasicNameValuePair("password", "drupal")
]
method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8))
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
println response.getEntity().getContent().text

// コメントを更新
cid = 3
method = new HttpPut("http://localhost/drupal/rest/comment/${cid}")
values = [
  new BasicNameValuePair('name', 'drupal'),
  new BasicNameValuePair('subject',
    'groovyでのコメント。更新しました。'),
  new BasicNameValuePair('comment_body[und][0][value]',
    'groovyでコメント作成。更新しました。'),
  new BasicNameValuePair('nid', '2')
]
method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8))
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
println response.getEntity().getContent().text

// ログアウト
method = new HttpGet("http://localhost/drupal/user/logout")
response = httpclient.execute(method)
println response.getStatusLine().getStatusCode()

動作環境
groovy 1.8.4, JDK7 Update1, drupal 7.1.0, services-7.x-3.1, ctools-7.x-1.0-rc1, spyc-0.5

0 件のコメント:

コメントを投稿