|
| 1 | +# APIWrapper support |
| 2 | + |
| 3 | +Create an API session that facilitates usage of the API methods supported by our Python API. |
| 4 | + |
| 5 | +The APIWrapper class is a wrapper for all of the supported API methods in the Python Client API. This makes it easier to use the supported API methods without having to manually pass a ServerContext object to each API method. |
| 6 | + |
| 7 | +### Using the APIWrapper class |
| 8 | + |
| 9 | +The APIWrapper class is imported from api_wrapper.py: |
| 10 | + |
| 11 | +```python |
| 12 | +from labkey.api_wrapper import APIWrapper |
| 13 | +``` |
| 14 | + |
| 15 | +It includes the following arguments: |
| 16 | + |
| 17 | +**domain** |
| 18 | +- This is the base URL for a LabKey Server instance. |
| 19 | +- Example: 'www.labkey.org' |
| 20 | + |
| 21 | +**container_path** |
| 22 | +- This is the path to the targeted project, folder, or subfolder in a LabKey Server instance. This path will be used as the default container path for all API requests, you can override this default by passing a container_path to any API method you are using. This parameter does not need to be supplied to any of the APIWrapper class's methods once it is defined in the class, unless the container_path intended for a method differs from the one used when instantiating the class. |
| 23 | +- Example: 'Project/Folder/Subfolder' |
| 24 | + |
| 25 | +**context_path** |
| 26 | +- The default value is None. Depending on how the LabKey Server instance is implemented, it may be necessary to include a value for the context_path argument. If your LabKey Server instance has text after the base URL, that is the context path. |
| 27 | +- Example: If your home project has a URL such as https://labkey.org/contextpath/home/project-begin.view, then the context path is 'contextpath'. |
| 28 | + |
| 29 | +**use_ssl** |
| 30 | +- The default value is True. This should be set to True if your server is configured to use SSL. If you are not sure if your server uses SSL, refer to any URL for accessing your server. Servers using SSL will have a URL that begins with `https://` instead of `http://`. LabKey Sample Manager-only clients must have this argument set to True. |
| 31 | + |
| 32 | +**verify_ssl** |
| 33 | +- The default value is True. This argument toggles whether or not the SSL certificate is validated when attempting to connect to a server. This flag is useful when you are connecting to a development server with a self-signed SSL certificate, which would otherwise cause a failure. You should never disable this flag if you are connecting to a production server with a proper SSL certificate. |
| 34 | + |
| 35 | +**api_key** |
| 36 | +- The default value is None. Scripts can authenticate their LabKey API calls by using either a netrc file (details on that here, https://www.labkey.org/Documentation/wiki-page.view?name=netrc) or an API key (details about API keys and how to generate and manage them are here, https://www.labkey.org/Documentation/wiki-page.view?name=apikey). |
| 37 | + |
| 38 | +**disable_csrf** |
| 39 | +- The default value is False. In most cases, this argument must be set to False for API calls to work successfully as CSRF tokens are a fundamental security mechanism. For more info about using CSRF with your LabKey Server instance, see here, https://www.labkey.org/Documentation/wiki-page.view?name=csrfProtection. |
| 40 | + |
| 41 | +### Using LabKey Python APIs |
| 42 | + |
| 43 | +The labkey-api-python library can be used to select rows, insert rows, edit containers, edit storage, modify security settings and permissions, as well as many other functions. To learn more about these different functions, see the other documentation pages in this docs folder. |
| 44 | + |
| 45 | +See below for an example of how to properly use the APIWrapper class to create a session and run the select_rows method. |
| 46 | + |
| 47 | +```python |
| 48 | +from labkey.api_wrapper import APIWrapper |
| 49 | + |
| 50 | +print("Create an APIWrapper") |
| 51 | +labkey_server = 'localhost:8080' |
| 52 | +project_name = 'ModuleAssayTest' # Project folder name |
| 53 | +contextPath = 'labkey' |
| 54 | +schema = 'core' |
| 55 | +table = 'Users' |
| 56 | +api = APIWrapper(labkey_server, project_name, contextPath, use_ssl=False) |
| 57 | + |
| 58 | +result = api.query.select_rows(schema, table) |
| 59 | + |
| 60 | +if result is not None: |
| 61 | + print(result['rows'][0]) |
| 62 | + print("select_rows: Number of rows returned: " + str(result['rowCount'])) |
| 63 | +else: |
| 64 | + print('select_rows: Failed to load results from ' + schema + '.' + table) |
| 65 | +``` |
| 66 | + |
| 67 | +### Automatic script generation |
| 68 | + |
| 69 | +In LabKey Server, data grids by default provide the ability to generate the Python code to export the displayed grid view using the APIWrapper class and the select_rows method. This is often an easy and convenient way to create a starting point for further Python development. For more information on this topic: https://www.labkey.org/Documentation/wiki-page.view?name=exportScripts |
0 commit comments