07 darwino rest services

17
Darwino REST Services

Transcript of 07 darwino rest services

Page 1: 07   darwino rest services

Darwino REST Services

Page 2: 07   darwino rest services

Darwino REST Services

• Darwino comes with built-in REST services similar to the ExtLib’s DAS or other document data store HTTP APIs

• These can form the basis of a full application, or just be used for bootstrapping while custom services are developed

• These are in the “/$darwino-jstore” URL space– This is a common convention: “$” URLs can be used to avoid the need to worry

about the place in the URL hierarchy

Page 3: 07   darwino rest services

Darwino Playground

playground.darwino.com

Page 4: 07   darwino rest services

I hope you like lists!

Page 5: 07   darwino rest services

Database Services

• GET /databases – returns the list of the available databases• GET /databases/{id} – returns information about a specific database• GET /databases/{id}/documentscount – returns the count of documents• DELETE /databases/{id}/documents – delete all documents• GET /databases/{id}/documents/{docid} – get document by internal ID• GET /databases/{id}/documentexists/{docid} – check document existence by

internal ID

Page 6: 07   darwino rest services

Database Maintenance/Meta Info

• GET /databases/{id}/updateftindex – update full-text index• GET /databases/{id}/instances – get list of available instances• POST /databases/{id}/reset – removes all documents• POST /databases/{id}/deploy – deploy (overwriting if needed) a database to

the server• GET /databases/{id}/undeploy – undeploy a database• GET /database/{id}/stores – get list of available stores• GET /database/{id}/stores/{store}/indexes – get list of indexes in a store

Page 7: 07   darwino rest services

Document CRUD

• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…– POST /documents/{unid} – creates a new document (UNID optional)

• Include doc content in “json” property of posted JSON• POST /documents/{unid}/content – shorthand to create a document by content

– GET /documents/{unid} – read a document by UNID• GET /documents/{unid}/content – shorthand to read a document’s content

– PUT /documents/{unid} – update a document by UNID• PUT /documents/{unid}/content – shorthand to update just a document’s content

– DELETE /documents/{unid} – delete a document by UNID

Page 8: 07   darwino rest services

Document Attachments

• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/documents/{unid}/…– GET /attachments – get list of attachments– GET /attachments/{name} – download a named attachment– HEAD /attachments/{name} – check if a named attachment exists– POST /attachments/{name} – create a named attachment– PUT /attachments/{name} – update an existing named attachment– DELETE /attachments/{name} – delete a named attachment

Page 9: 07   darwino rest services

Indexes (like views)

• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…– GET /indexes/{index_id}/documentexists/{key} – check index entry existence– GET /indexes/{index_id}/documents/{key} – get document by key– PUT /indexes/{index_id}/documents/{key} – update document by key– DELETE /indexes/{index_id}/documents/{key} – delete document by key

Page 10: 07   darwino rest services

Document Meta Services

• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…– GET /documentexists/{unid} – check existence– GET /documentdeleted/{unid} – check deleted– GET /childrencount/{unid} – check child (response) count– GET /children/{unid} - list of children by parent– GET /slaves/{unid} – list of slave (associated) documents– GET /documents/{unid}/social – read social data– POST /documents/{unid}/social – set social data

Page 11: 07   darwino rest services

Custom Services

• Darwino has the concept of “service factories” for custom REST services• Intended to be cross-platform, so they work consistently on “full” web

servers as well as embedded mobile/desktop servers• Each newly-created app has an “AppServiceFactory” class that provides the

entry point to register these• /$darwino-app URL space

Page 12: 07   darwino rest services

HttpService and HttpServiceContext Classes

• HTTP Services should extend the small abstract class com.darwino.commons.services.HttpService

• The primary method to implement is service(HttpServiceContext context)• HttpServiceContext is a consistent representation of HTTP data, similar to

what you’d expect with the javax.servlet classes– Access to the request data (most get* and is* methods)– Provides authenticated-user information tied to the Darwino user service– Access to the response, with convenience methods (e.g. emitJson(…))

Page 13: 07   darwino rest services

Custom Services Example: Social Analyzerbinders.add(new RestServiceBinder("feeds") { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new FeedsService(); }});

binders.add(new RestServiceBinder("userActions", "assign") { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new AssignUserService(); }});

binders.add(new RestServiceBinder("users", null) { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new UserInfoService(parts[1]); }});

Page 14: 07   darwino rest services

Social Analyzer: FeedsService

JsonObject result = new JsonObject();for(FeedProvider<?, ?, ?> service : Platform.findExtensions(FeedProvider.class)) { JsonObject node = new JsonObject(); node.put(PROP_USERCLASS, service.getUserClass().getName()); node.put(PROP_NAME, service.getName()); node.put(PROP_ICONCLASS, service.getIconClass()); node.put(PROP_CLASSNAME, service.getClass().getName()); result.put(service.getKey(), node);}context.emitJson(result);

Page 15: 07   darwino rest services

Social Analyzer: AssignUserServiceprotected void doPost(HttpServiceContext context) throws Exception { Object jsonObj = context.getContentAsJson(); JsonObject json = (JsonObject)jsonObj; String unid = json.getAsString("unid"); String name = json.getAsString(PROP_NAME);

Session session = DarwinoContext.get().getSession(); Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME); Store store = database.getStore(Database.STORE_DEFAULT); Document doc = store.loadDocument(unid); Document userDoc = findOrCreateUserDoc(database, name); doc.set(PROP_USERDOCUNID, userDoc.getUnid()); doc.set(PROP_USERDOCNAME, userDoc.get(PROP_NAME)); doc.save(); context.emitJson(doc.getJson());}

Page 16: 07   darwino rest services

Social Analyzer: UserInfoServicepublic UserInfoService(String userId) { this.userId = userId;}

protected void doGet(HttpServiceContext context) throws Exception { String dn = context.getUserDn(); Session session = DarwinoContext.get().getSession(); Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME); Store store = database.getStore(Database.STORE_DEFAULT); Document userDoc = store.loadDocument(this.userId); JsonObject result = new JsonObject(); result.put(PROP_USER, userDoc.getJson()); result.put(PROP_KEY, userDoc.getUnid());

// *snip* tons of code context.emitJson(result);}

Page 17: 07   darwino rest services

Thank you for your attention!