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
is correctly typed with content-type
application/json, while
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
If you use the Form tab in the inspector, you can
see the image