Reputation: 885
I'm about to develop a Java SDK against a REST API and would like to know what would be the best practice approach to building it. I've looked at Google and also used a number of SDKs which connect to REST APIs and there is never much consistency. I've come across some patterns which I find interesting and would like to know which one could be considered best practice, if any, or if there are alternatives?
I've provided sample / pseudo code to facilitate.
1) The models / requests / client are all separated. Example call:
Client client = new Client( ... credentials ... );
try {
Something obj = client.post( new PostSomethingRequest( ..params... ) );
} catch( Exception oops ) { ...handle... }
try {
Something obj2 = client.get( new GetSomethingRequest( id ) );
} catch( Exception oops ) { ...handle... }
2) The models and request are tied together and the client is separate. Example call:
Client client = new Client( ... credentials ... );
try {
Something obj = client.post( new Something( ..params... ) );
} catch( Exception oops ) { ...handle... }
try {
Something obj2 = client.get( new Something( id ) );
} catch( Exception oops ) { ...handle... }
3) The model contains everything. Example call:
Client.setCredentials( ... credentials ... );
Something obj = new Something( ..params... );
try {
obj.post();
} catch( Exception oops ) { ...handle... }
try {
Something obj2 = Something.get( id );
} catch( Exception oops ) { ...handle... }
If there are better ways of building this I'd also be glad to hear about them.
Upvotes: 5
Views: 4014
Reputation: 10352
IF you build an SDK for a special REST API, I would use method names that represent the REST service calls and won't be so generic.
Upvotes: 1