Results 1 to 5 of 5

Thread: [RESOLVED] Append response data to an object

  1. #1

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Resolved [RESOLVED] Append response data to an object

    I have recursive repeat of an requests to some REST API. And I need to append all outputs to my object.
    Basically since limit is 50, I keep requesting new data until I reach end.

    Code:
    private void LoadOpenSeaEvents()
            {
                string Uri = "https://api.opensea.io/api/v1/events";
                Uri += "?account_address=" + te_walletaddress.Text;
                Uri += "&only_opensea=False";
                Uri += "&event_type=successful";
                Uri += "&limit=50";
                Uri += "&offset={0}";
    
                int offset = 0;
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("X-API-KEY", "SECURED");
    
                string json_response = webClient.DownloadString(string.Format(Uri, offset));
                var responseData = JsonConvert.DeserializeObject<Assets>(json_response);
                var OpenSeaData = JsonConvert.DeserializeObject<Assets>(json_response);
                List<Assets> ListOfTotal = new List<Assets>();
                ListOfTotal.Add(OpenSeaData);
    
                while (responseData.asset_events.Count() > 0)
                {
                    offset += responseData.asset_events.Count();
                    json_response = webClient.DownloadString(string.Format(Uri, offset));
                    responseData = JsonConvert.DeserializeObject<Assets>(json_response);
                    OpenSeaData = JsonConvert.DeserializeObject<Assets>(json_response);
                    ListOfTotal.Add(OpenSeaData);
                }
     
            }
    I tried almost everything that came to my mind but i just can't append two response data to same object.
    Only way i found is to add output data to a ListOf objects, is there any way to avoid using ListOfTotal and have all data appended ?

    Class available at https://pastebin.com/5ZjSEe9A

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,490

    Re: Append response data to an object

    Something like this, but everything really depends on the structure of asset…

    Code:
    while (responseData.asset_events.Count() > 0)
    {
        offset += responseData.asset_events.Count();
        json_response = webClient.DownloadString(string.Format(Uri, offset));
        responseData = JsonConvert.DeserializeObject<Assets>(json_response);
        OpenSeaData &= JsonConvert.DeserializeObject<Assets>(json_response);
    }

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,490

    Re: Append response data to an object

    Looking at the Assets class, it doesn’t make sense to append. Every one of those classes has a distinct ID and values. A List(of Assets) is the best option

  4. #4

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: Append response data to an object

    Quote Originally Posted by .paul. View Post
    Looking at the Assets class, it doesn’t make sense to append. Every one of those classes has a distinct ID and values. A List(of Assets) is the best option
    Thank you for your answer. With setting your first post suggestion I'm getting
    Code:
    Error	CS0019	Operator '&=' cannot be applied to operands of type 'Assets' and 'Assets'
    The other solution Idea is I should keep it the way as I have, in this case I have a list of responses, every item in list contains 50 Assets, wanted to have them inside one object so I can proceed with one for loop.

  5. #5

    Thread Starter
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: Append response data to an object

    Ok got it.

    Thanks anyway

    Code:
                List<AssetEvent> AssetEvents = new List<AssetEvent>();
                AssetEvents.AddRange(responseData.asset_events.ToList());
    
                while (responseData.asset_events.Count() > 0)
                {
                    offset += responseData.asset_events.Count();
                    json_response = webClient.DownloadString(string.Format(Uri, offset));
                    responseData = JsonConvert.DeserializeObject<Assets>(json_response);
                    AssetEvents.AddRange(responseData.asset_events.ToList());
                }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width