Created
March 23, 2012 18:53
-
-
Save fehmicansaglam/2173771 to your computer and use it in GitHub Desktop.
Play! Framework ile Asenkron HTTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Application extends Controller { | |
public static void index() throws Throwable { | |
Either<User, Throwable> result = await(Client.createUser("testuser", | |
"Foo", | |
"Bar")); | |
if (result._1.isDefined()) { | |
result = await(Client.getUser("testuser")); | |
if (result._1.isDefined()) { | |
final User created = result._1.get(); | |
Logger.info("Kullanıcı oluşturuldu: name:%s surname:%s", | |
created.name, | |
created.surname); | |
} else { | |
throw result._2.get(); | |
} | |
} else { | |
throw result._2.get(); | |
} | |
render(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Client { | |
public static final String BASE_URL = "http://localhost:9001"; | |
public static Promise<Either<User, Throwable>> getUser(final String username) { | |
final Map<String, String> params = new HashMap<String, String>(); | |
params.put("username", username); | |
return new ClientJob<User>(HttpMethod.GET, | |
BASE_URL + "/api/user", | |
params, | |
User.class).now(); | |
} | |
public static Promise<Either<User, Throwable>> createUser(final String username, | |
final String name, | |
final String surname) { | |
final Map<String, String> params = new HashMap<String, String>(); | |
params.put("username", username); | |
params.put("name", name); | |
params.put("surname", surname); | |
return new ClientJob<User>(HttpMethod.POST, | |
BASE_URL + "/api/user", | |
params, | |
User.class).now(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class ClientJob<V> extends Job<Either<V, Throwable>> { | |
private final HttpMethod method; | |
private final String url; | |
private final Map<String, String> params; | |
private final Class<V> clazz; | |
public ClientJob(final HttpMethod method, | |
final String url, | |
final Map<String, String> params, | |
final Class<V> clazz) { | |
this.method = method; | |
this.url = url; | |
this.params = params; | |
this.clazz = clazz; | |
} | |
@Override | |
public Either<V, Throwable> doJobWithResult() throws Exception { | |
try { | |
final String json = doRequest(this.method, this.url, this.params); | |
Logger.debug(json); | |
final V result = new Gson().fromJson(json, this.clazz); | |
return Either._1(result); | |
} catch (final Throwable throwable) { | |
return Either._2(throwable); | |
} | |
} | |
private static String doRequest(final HttpMethod method, | |
final String url, | |
final Map<String, String> params) { | |
final WSRequest wsRequest = WS.url(url).setParameters(params); | |
final HttpResponse response; | |
switch (method) { | |
case HEAD: | |
response = wsRequest.head(); | |
break; | |
case GET: | |
response = wsRequest.get(); | |
break; | |
case PUT: | |
response = wsRequest.put(); | |
break; | |
case POST: | |
response = wsRequest.post(); | |
break; | |
case DELETE: | |
response = wsRequest.delete(); | |
break; | |
default: | |
response = null; | |
} | |
switch (response.getStatus()) { | |
case 200: | |
return response.getString(); | |
default: | |
throw new UnexpectedException(response.getString()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum HttpMethod { | |
HEAD, | |
GET, | |
PUT, | |
POST, | |
DELETE | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Provider extends Controller { | |
public static void getUser(final String username) { | |
final User user = User.find("byUsername", username).first(); | |
renderJSON(user); | |
} | |
public static void createUser(final String username, | |
final String name, | |
final String surname) { | |
final User user = new User(username, name, surname).save(); | |
renderJSON(user); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class User extends Model { | |
@Column(unique = true, nullable = false) | |
public String username; | |
public String name; | |
public String surname; | |
public User(final String username, | |
final String name, | |
final String surname) { | |
this.username = username; | |
this.name = name; | |
this.surname = surname; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment