ferristseng / rust-ipfs-api

IPFS HTTP client in Rust
Apache License 2.0
246 stars 68 forks source link

`dag_get` does not decode gracefully #109

Open mrd0ll4r opened 1 year ago

mrd0ll4r commented 1 year ago

I'm trying to move from object_links (or object_get) to dag_get, since the former are deprecated.

dag_get currently returns Bytes. I selected DagCodec::Json for the encoding, and the returned value is indeed JSON, but cannot be parsed as DagGetResponse. I'm not sure if this type is even intended for this use case, but I figured, from the name...

Request code:

let object_links_resp_raw = client
            .dag_get_with_options(DagGet {
                path: cid,
                codec: Some(DagCodec::Json),
            })
            .map_ok(|chunk| chunk.to_vec())
            .try_concat()
            .await
            .context("unable to object links")?;
println!("{:?}",String::from_utf8(object_links_resp_raw.clone()));
let object_links_resp = serde_json::from_slice::<DagGetResponse>(&object_links_resp_raw)
            .context("unable to decode response")?;

which gives me this JSON:

"{\"Data\":{\"/\":{\"bytes\":\"CAUSFQEABAAAAAAAAAEAAAgAAAAAAAAQACgiMIAC\"}},\"Links\":[{\"Hash\":{\"/\":\"QmPQVLHXAcDLvdf6ni24YWgGwitVTwtpiFaKkMfzZKquUB\"},\"Name\":\"0C-\",\"Tsize\":237360},{\"Hash\":{\"/\":\"QmNYBYYjwtCYXdA2KC68rX8RqXBu9ajBM6Gi8CBrXjvk1j\"},\"Name\":\"43I\",\"Tsize\":4432
3016540},{\"Hash\":{\"/\":\"QmehSxmTPRCr85Xjgzjut6uWQihoTfqg9VVihJ892bmZCp\"},\"Name\":\"58wiki\",\"Tsize\":613715579624},{\"Hash\":{\"/\":\"QmaeP3RagknCH4gozhE6VfCzTZRU7U2tgEEfs8QMoexEeG\"},\"Name\":\"92M\",\"Tsize\":826},{\"Hash\":{\"/\":\"QmdgiZFqdzUGa7vAFycnA5Xv2mbcdHSsPQHsMyhpuzm9xb\"},\"
Name\":\"A0index.html\",\"Tsize\":165}]}"

pretty:

{
  "Data": {
    "/": {
      "bytes": "CAUSFQEABAAAAAAAAAEAAAgAAAAAAAAQACgiMIAC"
    }
  },
  "Links": [
    {
      "Hash": {
        "/": "QmPQVLHXAcDLvdf6ni24YWgGwitVTwtpiFaKkMfzZKquUB"
      },
      "Name": "0C-",
      "Tsize": 237360
    },
    {
      "Hash": {
        "/": "QmNYBYYjwtCYXdA2KC68rX8RqXBu9ajBM6Gi8CBrXjvk1j"
      },
      "Name": "43I",
      "Tsize": 44323016540
    },
    {
      "Hash": {
        "/": "QmehSxmTPRCr85Xjgzjut6uWQihoTfqg9VVihJ892bmZCp"
      },
      "Name": "58wiki",
      "Tsize": 613715579624
    },
    {
      "Hash": {
        "/": "QmaeP3RagknCH4gozhE6VfCzTZRU7U2tgEEfs8QMoexEeG"
      },
      "Name": "92M",
      "Tsize": 826
    },
    {
      "Hash": {
        "/": "QmdgiZFqdzUGa7vAFycnA5Xv2mbcdHSsPQHsMyhpuzm9xb"
      },
      "Name": "A0index.html",
      "Tsize": 165
    }
  ]
}

which does not fit DagGetResponse:

pub struct DagGetResponse {
    pub data: Option<String>,

    #[serde(deserialize_with = "serde::deserialize_vec")]
    pub links: Vec<DagIpfsHeader>,
}

Field names are PascalCase, data is ... something, but not a String.

I'm not sure if the format changed at some point, since there is a test case for a different (old?) format...