Import CSV file using pystardog stardog.content.ImportFile

I am trying a simple CSV file import with pystardog/Python, but am stuck with stardog.exceptions.StardogException: [415].

import stardog

connection_details = {
  'endpoint': 'myURLtoStardog',
  'username': 'myusername',
  'password': 'myuserpwd',
  'database': 'mystardogdbname'
}

# Connect to DB
with stardog.Connection(**connection_details) as connection:

    connection.begin()
    
    # import CSV file using a SMS2 mapping file
    connection.add(stardog.content.ImportFile('cars.csv'))
    connection.add(stardog.content.MappingFile('cars_mappings.sms'))
    
    connection.commit()

I am using the official Stardog example csv files. I am following the syntax/examples described in the pystardog documentation. But I end up with below error.

Has anyone an idea what I am doing wrong? Or is my assumption incorrect that I can use pystardog to do the same csv import as with the Stardog CLI?

Traceback (most recent call last):
  File "C:\Users\me\stardog_import-csv.py", line 16, in <module>
    connection.add(stardog.content.ImportFile('cars.csv'))
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\connection.py", line 165, in add
    self.client.post("/{}/add".format(self.transaction), **args)
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\http\client.py", line 49, in post
    return self.__wrap(self.session.post(self.url + path, **kwargs))
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\http\client.py", line 71, in __wrap
    raise exceptions.StardogException(
stardog.exceptions.StardogException: [415] :

Here's an example in the pystardog source code.

Thanks for the quick reply, Michael.

The issue isn't with stardog.content.ImportFile - this is working fine I think. The problem seems to be with connection.add, at least the server doesn't seems to to accept the csv file and provides a return code 415 = unsupported media type.

By the way, the example you provided is for stardog.admin.import_file, a different functions. But I also find an example for stardog.content.ImportFile here.

The problem seems to be with connection.add

That's correct. That's the wrong function to call. That's why I pointed you to the right function to use.

Regarding the example you pointed you, you'll notice that it's not a test for import, its testing content type detection. The example I provided is the most relevant example for you to follow.

You were right, Mike. With those hints, I finally got it working. Many thanks!

Here is my working code. A great and simple alternative to the CLI command "stardog-admin virtual import" to import a CSV file with a SMS2 mapping file.

import stardog

admin = stardog.Admin(
        endpoint='myURLtoStardog:port',
        username='myusername',
        password='myuserpwd'
)

admin.import_file(
        'mystardogdbname',
        stardog.content.MappingFile('cars_mappings.sms'),
        stardog.content.ImportFile('cars.csv')
)