Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Parsing JSON of Retrieved Material #385

Open
c-heat16 opened this issue Oct 11, 2023 · 0 comments
Open

Error Parsing JSON of Retrieved Material #385

c-heat16 opened this issue Oct 11, 2023 · 0 comments

Comments

@c-heat16
Copy link

Describe the bug
When retrieving a material (api.search(...)) that has previously been created and saved, an error is thrown when trying to parse the json. Upon inspecting the json with @nh916 it appears the cause is an unexpected 'project' item in the retrieved json. Upon further inspection, however, it seems that not all material nodes have this issue. Removing the 'project' item alleviates the issue, but doesn't seem like a long-term solution.

To Reproduce
Code to reproduce is below. Uncommenting del j['project'] alleviates the issue.

import os
import json
import cript


def make_fetch_node(
        api, node_name, node_type=cript.Project,
        search_mode=cript.SearchModes.EXACT_NAME,
        **kwargs
):
    res_paginator = api.search(
        node_type=node_type,
        search_mode=search_mode,
        value_to_search=node_name
    )

    try:
        print('* selecting first result on first page *')
        to_return = res_paginator.current_page_results[0]
        print('* dumping json and loading *')
        j = json.loads(json.dumps(to_return))
        if node_type == cript.Material:
            print('j:\n{}'.format(json.dumps(j, indent=2)))
            # del j['project']
        j = json.dumps(j)
        to_return = cript.load_nodes_from_json(j)
        print('* NODE RETRIEVED *')
        return to_return

    except Exception as ex:
        print('ex: {}'.format(ex))
        print('* CREATING NODE *')
        kwargs['name'] = node_name
        if node_type != cript.Project and node_type != cript.Collection:
            kwargs['identifier'] = []
        return node_type(**kwargs)

api_token = os.environ.get('CRIPT_API_TOKEN')
storage_token = os.environ.get('CRIPT_STORAGE_TOKEN')

api = cript.API(
    host='https://api.criptapp.org',
    api_token=api_token, storage_token=storage_token
)
api.connect()

print('* making/fetching project *')
project = make_fetch_node(api, node_name='My Toy Project 1234567')
print('* project obtained *')
print('* making/fetching material *')
mat1 = make_fetch_node(
    api, node_name='mat2134123134314', node_type=cript.Material
)
print('* material obtained *')
api.save(mat1)
api.save(project)

The ill-formed json, without deleting the 'project' item, is below:

{
  "component_count": 0,
  "computational_forcefield_count": 0,
  "created_at": "2023-10-11T21:38:40.521954Z",
  "identifier_count": 0,
  "locked": false,
  "model_version": "1.0.0",
  "name": "mat2134123134314",
  "node": [
    "Material"
  ],
  "project": [
    {
      "name": "Toy Inventory",
      "node": [
        "Inventory"
      ],
      "uid": "0x149a2b",
      "uuid": "c3cd9f37-f4a8-46d8-8366-b6085b3cb6b9"
    },
    {
      "node": [
        "Ingredient"
      ],
      "uid": "0x149a2f",
      "uuid": "39c7721c-2532-4789-bda5-709cbb392bee"
    }
  ],
  "property_count": 0,
  "public": false,
  "uid": "0x149a29",
  "updated_at": "2023-10-11T21:38:40.52196Z",
  "uuid": "d70288e0-0a9e-408e-9f1e-ade394dd34fa"
}

Expected behavior
CRIPT is able to parse json of retrieved material.

Additional context
May be related to trying to retrieve a material name which previously existed but has since been deleted?

@c-heat16 c-heat16 changed the title Error Parson JSON of Retrieved Material Error Parsing JSON of Retrieved Material Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant