Using ZnClient>>#contentWriter

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using ZnClient>>#contentWriter

Esteban A. Maringolo
Hi all,

I'm doing this and it works
ZnClient new
  method: #PUT;
  url: 'http://127.0.0.1:5984/baseball/2';
  contents:  (NeoJSONWriter toString: { 'foo' -> 'baz'} asDictionary);
  contentType: ZnMimeType applicationJson;
  execute.


But if try to use a contentWriter it fails because it can't determine
the contentType of the entity.

ZnClient new
  method: #PUT;
  url: 'http://127.0.0.1:5984/baseball/2';
  "contentType: ZnMimeType applicationJson;"
  contentWriter: [ :entity | NeoJSONWriter toString: entity ];
  contents:  { 'foo' -> 'baz'} asDictionary;
  execute.

If I uncomment the "contentType:" message send it complains about
setting the content type before setting the entity, which makes sense
but baffles me.

My Idea is to directly pass the object as contents, and let the
content writer deal with how it's going to be serialized on the
request (a JSON string, for the most part).

Although the first expression works, I know it can be done better, but
I can't manage to get it working :)

Thanks,

ps: I also wonder by there aren't #bePut, #bePost, #beDelete, etc.
convenience methods instead of passing the HTTP method as a symbol :)


Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: Using ZnClient>>#contentWriter

Sven Van Caekenberghe-2
Hi Esteban,

> On 21 Feb 2019, at 23:36, Esteban Maringolo <[hidden email]> wrote:
>
> Hi all,
>
> I'm doing this and it works
> ZnClient new
>  method: #PUT;
>  url: 'http://127.0.0.1:5984/baseball/2';
>  contents:  (NeoJSONWriter toString: { 'foo' -> 'baz'} asDictionary);
>  contentType: ZnMimeType applicationJson;
>  execute.
>
>
> But if try to use a contentWriter it fails because it can't determine
> the contentType of the entity.
>
> ZnClient new
>  method: #PUT;
>  url: 'http://127.0.0.1:5984/baseball/2';
>  "contentType: ZnMimeType applicationJson;"
>  contentWriter: [ :entity | NeoJSONWriter toString: entity ];
>  contents:  { 'foo' -> 'baz'} asDictionary;
>  execute.
>
> If I uncomment the "contentType:" message send it complains about
> setting the content type before setting the entity, which makes sense
> but baffles me.
>
> My Idea is to directly pass the object as contents, and let the
> content writer deal with how it's going to be serialized on the
> request (a JSON string, for the most part).
>
> Although the first expression works, I know it can be done better, but
> I can't manage to get it working :)

Yes, it can be done more elegantly.

Please check ZnClient>>#forJsonREST that does set a reader and writer (latest Zn).

> Thanks,
>
> ps: I also wonder by there aren't #bePut, #bePost, #beDelete, etc.
> convenience methods instead of passing the HTTP method as a symbol :)

Because ZnClient already has way too many methods ;-)

What I typically do is configure and re-use a client (for talking to the same service),
where only the URL changes (merging with the same endpoint, same username/password, etc )

myClient get: '/user/1'.
myClient post: '/users' with: { #name->'foo' } asDictionary.
myClient get: '/user?name=foo'

(in my case, my client wraps a ZnClient instance, adds a cache, does processing of errors).

> Esteban A. Maringolo

Sven