2012年2月10日金曜日

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.*
import groovy.json.*

// ログイン
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

// ユーザを削除する
uid="9"
method = new HttpDelete("http://localhost/drupal/rest/user/${uid}")
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 件のコメント:

コメントを投稿