SAP / python-pyodata

Enterprise-ready Python OData client
Apache License 2.0
220 stars 93 forks source link

Convert the filtered data to JSON #252

Closed paulian829 closed 1 year ago

paulian829 commented 1 year ago

Hi I had been struggling to convert the fetched data to a JSON format.

def initialize():
    session = requests.Session()
    session.auth = (ODATA_USERNAME, ODATA_PASSWORD)

    param = {"sap-client": "200", "inlinecount": "allpages", "format": "json"}
    session.params = param

    custom_config = Config(
        default_error_policy=PolicyFatal(),
        custom_error_policies={
            ParserError.ANNOTATION: PolicyIgnore(),
            ParserError.ASSOCIATION: PolicyIgnore(),
            ParserError.PROPERTY: PolicyIgnore(),
            ParserError.ENUM_TYPE: PolicyIgnore(),
            ParserError.ENTITY_TYPE: PolicyIgnore(),
            ParserError.COMPLEX_TYPE: PolicyIgnore(),
        },
    )

    return session, custom_config

def get_accounts_payable_line_items(
    session,
    custom_config,
    company_code: str,
    since: datetime.datetime,
    until: datetime.datetime,
    top,
    skip,
):
    since = adjust_since_hack(since, until)
    since_iso = since.strftime("%Y-%m-%d")
    until_iso = until.strftime("%Y-%m-%d")

    account_payable_lines = pyodata.Client(
        FAP_VENDOR_LINE_ITEMS_SRV, session, config=custom_config
    )

    data = (
        account_payable_lines.entity_sets.Items.get_entities()
        .filter(
            " and ".join(
                [
                    f"PostingDate ge datetime'{since_iso}T00:00:00'",
                    f"PostingDate lt datetime'{until_iso}T00:00:00'",
                    f"CompanyCode eq '{company_code}'",
                ]
            )
        )
        .order_by("PostingDate asc")
        .top(top)
        .skip(skip)
        .execute()
    )

    return data

    result = get_accounts_payable_line_items(
        session, custom_config, "820", since, until, 20, 0
    )

...

#this will work if you know the properties of the entity
    for idx in result:
         print(idx.AccountCreatedByUser)

# What I wanted to do is directly convert the entity to a JSON file

I need to convert the filtered Entity to a JSON file directly. This is possible if you know each property, the problem is we have over 300+ properties and it would be a problem if It will be hard coded.

idx['AccountCreatedByUser'] will also not work so make a list of the properties and then looping it to create a key object pair will not work.

phanak-sap commented 1 year ago

Hi @paulian829, I have read the issue several time but still have a problem understand this.

You should have IMHO have everything you need in the "return data" part. Pyodata will for sure not write directly to file, but since you have either many dictionaries or even direct json response it should not be a problem. If you want to go through entity properties manually to somehow store json file as a just part of overall response, still does not need to be hardcoded - you should be able to go trough service.schema to place a loop on entity which is only important to you. Or grab the raw response and use a python library for json manipulation.

If this this is still not enough answer for you, please provide a failing test for a bug/feature request. I am sure you can find in existing test suite something as a good starting point. I am not actually able to see the real problem here just from the provided general description of the problem - so far seems more like a adapter code outside of pyodata library for your use case than something that should be part of this library.

phanak-sap commented 1 year ago

Hi @paulian829 I see no response, so closing. If the problem still persist, please reopen with additional informations requested above.