pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

how the best deal with this

RW
Roelof Wobben
Mon, Sep 28, 2020 5:28 AM

Hello,

I have this code :

getMoreData     | url url2 json json2 artObjectJson levelsJson|     url := 'https://www.rijksmuseum.nl/api/nl/collection/' , objectNumber         , '?key=14OGzuak&format=json'.     url2 := 'https://www.rijksmuseum.nl/api/nl/collection/'         , objectNumber , '/tiles?key=14OGzuak'.     "Read the data from the api respons"     json := NeoJSONReader fromString: (ZnEasy get: url) contents.     artObjectJson := json at: #artObject.     painter := ((artObjectJson at: #principalMakers) at: 1) at: #name.     title := artObjectJson at: #title.     "Read the imageUrl from the api respons"     json2 := NeoJSONObject fromString: (ZnEasy get: 'https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=14OGzuak') contents utf8Decoded.     levelsJson := (json2 at: #levels).     imageUrl :=  levelsJson at: 1.

but now I see that somethimes ZnEasy get: url   gives a document of zero bytes. Now can I put the rest in a big ifFalse block or make another function for it or is there a better more smalltalk way of dealing with this "problem".

Roelof

Hello, I have this code : getMoreData     | url url2 json json2 artObjectJson levelsJson|     url := 'https://www.rijksmuseum.nl/api/nl/collection/' , objectNumber         , '?key=14OGzuak&format=json'.     url2 := 'https://www.rijksmuseum.nl/api/nl/collection/'         , objectNumber , '/tiles?key=14OGzuak'.     "Read the data from the api respons"     json := NeoJSONReader fromString: (ZnEasy get: url) contents.     artObjectJson := json at: #artObject.     painter := ((artObjectJson at: #principalMakers) at: 1) at: #name.     title := artObjectJson at: #title.     "Read the imageUrl from the api respons"     json2 := NeoJSONObject fromString: (ZnEasy get: 'https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=14OGzuak') contents utf8Decoded.     levelsJson := (json2 at: #levels).     imageUrl :=  levelsJson at: 1. but now I see that somethimes ZnEasy get: url   gives a document of zero bytes. Now can I put the rest in a big ifFalse block or make another function for it or is there a better more smalltalk way of dealing with this "problem". Roelof
SV
Sven Van Caekenberghe
Mon, Sep 28, 2020 2:19 PM

On 28 Sep 2020, at 07:28, Roelof Wobben via Pharo-users pharo-users@lists.pharo.org wrote:

Hello,

I have this code :

getMoreData | url url2 json json2 artObjectJson levelsJson| url := 'https://www.rijksmuseum.nl/api/nl/collection/' , objectNumber , '?key=14OGzuak&format=json'. url2 := 'https://www.rijksmuseum.nl/api/nl/collection/' , objectNumber , '/tiles?key=14OGzuak'. "Read the data from the api respons" json := NeoJSONReader fromString: (ZnEasy get: url) contents. artObjectJson := json at: #artObject. painter := ((artObjectJson at: #principalMakers) at: 1) at: #name. title := artObjectJson at: #title. "Read the imageUrl from the api respons" json2 := NeoJSONObject fromString: (ZnEasy get: 'https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=14OGzuak') contents utf8Decoded. levelsJson := (json2 at: #levels). imageUrl :=  levelsJson at: 1.

but now I see that somethimes ZnEasy get: url  gives a document of zero bytes. Now can I put the rest in a big ifFalse block or make another function for it or is there a better more smalltalk way of dealing with this "problem".

Roelof

I think it would be good if you ask de rijksmuseum api guys to explain why

https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368

is correctly typed with content-type application/json, while

https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles

has a missing content-type (while it is clearly also application/json).

Your code can be improved as well. If you do your parsing via NeoJSONObject, you do not get Dictionaries back, but instances of NeoJSONObject (a subclass of Dictionary). These behave more like JavaScript maps/objects.

You can use accessors, as if they exist, and they will turned into #at: accesses, with the additional feature of returning nil when the property is not found.

So, for example, at the end, you can say

json2 levels.

Instead of at: 1 you can use first, together this gives you

json2 levels first.

The same approach is possible for the first json expression and its subsequent use (but parse via NeoJSONObject fromString:).

json artObject principalMarkers first name.

Coming back to your problem, 'sometimes' is not a bug report we can work on, it does not sound good. It could be that the server returns an empty response. But normally, if something is wrong, you should get a real error.

The API seems cool though, I got a nice painting back retrieving one of the embedded URLs (artObject->webImage->url) using

ZnEasy getJpeg: 'https://lh3.googleusercontent.com/N2m90mImdcoLacUybb_rxcktTwtr0LFhtuzxbSE9elIhElF6jpWngx96_uZ0L1TGNof5pNt4n_Ygb4KYlPTpA9o6788=s0'.

If you use the Form tab in the inspector, you can see the image

> On 28 Sep 2020, at 07:28, Roelof Wobben via Pharo-users <pharo-users@lists.pharo.org> wrote: > > Hello, > > I have this code : > > getMoreData > | url url2 json json2 artObjectJson levelsJson| > url := 'https://www.rijksmuseum.nl/api/nl/collection/' , objectNumber > , '?key=14OGzuak&format=json'. > url2 := 'https://www.rijksmuseum.nl/api/nl/collection/' > , objectNumber , '/tiles?key=14OGzuak'. > "Read the data from the api respons" > json := NeoJSONReader fromString: (ZnEasy get: url) contents. > artObjectJson := json at: #artObject. > painter := ((artObjectJson at: #principalMakers) at: 1) at: #name. > title := artObjectJson at: #title. > "Read the imageUrl from the api respons" > json2 := NeoJSONObject fromString: (ZnEasy get: 'https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=14OGzuak') contents utf8Decoded. > levelsJson := (json2 at: #levels). > imageUrl := levelsJson at: 1. > > but now I see that somethimes ZnEasy get: url gives a document of zero bytes. > Now can I put the rest in a big ifFalse block or make another function for it or is there a better more smalltalk way of dealing with this "problem". > > Roelof I think it would be good if you ask de rijksmuseum api guys to explain why https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368 is correctly typed with content-type application/json, while https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles has a missing content-type (while it is clearly also application/json). Your code can be improved as well. If you do your parsing via NeoJSONObject, you do not get Dictionaries back, but instances of NeoJSONObject (a subclass of Dictionary). These behave more like JavaScript maps/objects. You can use accessors, as if they exist, and they will turned into #at: accesses, with the additional feature of returning nil when the property is not found. So, for example, at the end, you can say json2 levels. Instead of at: 1 you can use first, together this gives you json2 levels first. The same approach is possible for the first json expression and its subsequent use (but parse via NeoJSONObject fromString:). json artObject principalMarkers first name. Coming back to your problem, 'sometimes' is not a bug report we can work on, it does not sound good. It could be that the server returns an empty response. But normally, if something is wrong, you should get a real error. The API seems cool though, I got a nice painting back retrieving one of the embedded URLs (artObject->webImage->url) using ZnEasy getJpeg: 'https://lh3.googleusercontent.com/N2m90mImdcoLacUybb_rxcktTwtr0LFhtuzxbSE9elIhElF6jpWngx96_uZ0L1TGNof5pNt4n_Ygb4KYlPTpA9o6788=s0'. If you use the Form tab in the inspector, you can see the image
RW
Roelof Wobben
Mon, Sep 28, 2020 2:36 PM