sscheetz / etsy-python2

Python access to the Etsy API
http://developer.etsy.com
GNU General Public License v3.0
32 stars 18 forks source link

strange json format #9

Open ihartaras opened 4 years ago

ihartaras commented 4 years ago

Hello oauth works and I get responds from Etsy API, thanks

However I get a file with strange json format

for example this request etsy.getUser (user_id = "ndbeylss") saves a json like this [{'user_id': 147441992, 'login_name': 'ndbeylss', 'creation_tsz': 1522924784, 'referred_by_user_id': None, 'feedback_info': {'count': 48, 'score': 100}}] it is impossible to use the parser and checks from third-party services give errors

If I just make the same request in the browser: https://openapi.etsy.com/v2/users/ndbeylss?api_key=xxxxxxxxxxxx I will get this code {"count": 1, "results": [{"user_id": 147441992, "login_name": "ndbeylss", "creation_tsz": 1522924784, "referred_by_user_id": null, "feedback_info": {"count": 48, "score": 100}}], "params": {"user_id": "ndbeylss"}, "type": "User", "pagination": {}}

Maybe I missed something? would appreciate help

vyrzdev commented 4 years ago

This library extracts the "results" portion of the JSON before returning it to you.

You can access the count variable for pagination by referencing etsy.count on your instance of the API.

This is abnormal behaviour and I have submitted an issue relating to it here: #10

ihartaras commented 4 years ago

Thank you very much for your reply! Can I ask you to illustrate with a small example? I am just trying to learn and the examples are very effective to understand. If possible, just a few lines, for example, how to get the correct answer to a request etsy.getUser (user_id = "ndbeylss")

would be very grateful

vyrzdev commented 4 years ago

Yes I can!

responseJSON = etsy.getUser(user_id="ndbeylss")
# This returns a list, as you can see from the json you posted: [{"user_id": 3274394293,  etc etc}]
# This happens because, while the response from Etsy's servers is in the format: 
# {"count": 1, "results": [{"user_id": 3274394293,  etc etc}], etc etc}
# This Etsy API Wrapper for python extracts and returns only the "results" portion, thus:
# [{"user_id": 3274394293,  etc etc}]

# To get the first element of this list, the dictionary containing your user's json:
userJSON = responseJSON.pop()
# or
userJSON = responseJSON[0]
# The pop method removes the json from the list, whereas the [0] way simply copies it 

# As such you now have the user's json.

There is unfortunately no way to reverse engineer the value returned by getUser, or any of the other methods, back to the etsy server's output, without directly modifiying this libraries source.

I may make a PR and propose changes to this, but it would break a lot of existing code if updated to.

vyrzdev commented 4 years ago

In relation to this, for requests of larger datasets, where pagination is necessary, here's another, likely very poor example!

from etsy2 import Etsy
etsyClient = Etsy()
shop_id = "fakeshopid"

# Starting values for variables defined
finishedReading = False # A flag!
totalAmountOfResourcesFetched = 0 # A counter!
page = 1 # First page is where we start, indexing does in fact start from 1
limit = 100 # 100 is max allowed on Etsy per request!
fetchedResourceJSONList = list()
while not finishedReading:
    print(f"Requesting Page: {page}")

    # Make the request, the limit kwarg is to say how many listings you want to fetch per request, the page kwarg is well... the page no. you want.
    responseJSON = etsyClient.findAllShopListingsActive(shop_id=shop_id, limit=limit, page=page)

    # Fetch the count variable from the request, for some reason this is not returned by the etsy2 library, but instead the results are unpacked, and the count var is assigned to clientVar.count .
    totalAmountOfResourcesOnEtsy = etsyClient.count
    totalAmountOfResourcesFetched += len(responseJSON)
    fetchedResourceJSONList = fetchedResourceJSONList + responseJSON
    if totalAmountOfResourcesOnEtsy == totalAmountOfResourcesFetched:
        finishedReading = True
    else:
        page += 1
        finishedReading = False

print(fetchedResourceJSONList)