Hi all
I have recently taken up golang and as usual the best way for me to learn is to
read a lot and dive head in.
I am attempting to write an API client as a an exercise and i have run into
some best practice design questions which I hope to get some feedback and ideas
on how to best approach this.
I have a large http API in the form of
/resourceA/*
/resourceB/*
And so forth. There are several different types of resources with several
different operations in each
What I would like to achieve in terms of API usability is this.
// Omitted all fields
client := &clientAPI{}
//Omitted arguments
client.resourceAMethod1()
client.resourceAMethod2()
client.resourceBMethod1()
client.resourceBMethod2()
And so forth.
At first I started with a simple naive implementation where i have this
type worker interface {
NewRequest(opts ...)
Do(Request, Response) error
resourceAMethod1()
resourceAMethod2()
resourceBMethod1()
resourceBMethod2()
}
where all resource methods will call NewRequest followed by a Do() reusing the
http client logic.
>From a testability and structuring point of view I feel that the client
>(holding things like hostname, http.client and such) should be decoupled from
>the resource logic.
This is where i am stuck a bit.
It feels natural to have ClientAPI have 2 methods called newRequest() and Do()
for performing the actual works and have the resource parts use those however
how to go about that in a way that it is both test friendly, scalable and user
friendly
Digital ocean seems to have a more elegant solution in terms of testability
https://github.com/digitalocean/godo/blob/master/godo.go
However I am not a huge fan of having to manually initialize each resource in
the client constructor manually (and they don't embed though i suppose that
would be a non-issue as you can embedded and initialize the embedded resource
instead)
Is there any way to achieve the level of testability and separation I see on
the digital ocean client implementation without having to initialize each
separate resource and pass a client into it?
Sorry for the long post, if anybody actually reaches the end I would appreciate
comments and feedback on how can this be done better and if i am thinking about
this completely wrong
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.