datagrok-api 0.0.6__tar.gz → 0.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ build/
8
8
  develop-eggs/
9
9
  dist/
10
10
  downloads/
11
+ docs_out/
11
12
  eggs/
12
13
  .eggs/
13
14
  lib/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datagrok-api
3
- Version: 0.0.6
3
+ Version: 0.0.7
4
4
  Summary: Client library for integration with datagrok API
5
5
  Project-URL: Homepage, https://github.com/datagrok-ai/public
6
6
  Project-URL: Issues, https://github.com/datagrok-ai/public/issues
@@ -8,7 +8,7 @@ Author-email: Andrew Skalkin <askalkin@datagrok.ai>, Amelichev Konstantin <kamel
8
8
  Classifier: License :: OSI Approved :: MIT License
9
9
  Classifier: Operating System :: OS Independent
10
10
  Classifier: Programming Language :: Python :: 3
11
- Requires-Python: >=3.5
11
+ Requires-Python: >=3.8
12
12
  Requires-Dist: pandas
13
13
  Requires-Dist: requests
14
14
  Requires-Dist: requests-toolbelt
@@ -1,4 +1,4 @@
1
- from typing import List, Optional, Dict, Any, Union, BinaryIO
1
+ from typing import List, Optional, Dict, Any, Set, Union
2
2
  import requests
3
3
  import pandas as pd
4
4
  import json
@@ -12,23 +12,39 @@ import shutil
12
12
  from .group import Group, GroupMembershipRequest
13
13
 
14
14
  class DatagrokClient:
15
+ """A client for interacting with the Datagrok API.
16
+
17
+ This class provides methods to interact with various Datagrok services including:
18
+ - Table operations (download/upload)
19
+ - File operations (download/upload/sync)
20
+ - Dashboard management
21
+ - Group management
22
+ - Function calls
23
+
24
+ The client handles authentication and provides a convenient interface for making
25
+ API calls to the Datagrok server.
26
+
27
+ Attributes
28
+ ----------
29
+ api_key : str
30
+ The API key used for authentication. Should start with "Bearer".
31
+ base_url : str
32
+ The base URL of the Datagrok API (e.g., "https://public.datagrok.ai/api")
33
+ headers : Dict[str, str]
34
+ HTTP headers used for authentication in API requests
35
+ """
36
+
15
37
  def __init__(self, api_key: str, base_url: str) -> None:
16
- '''
17
- Create a datagrok api client.
18
-
38
+ """Initialize a new DatagrokClient instance.
39
+
19
40
  Parameters
20
41
  ----------
21
- api_key: str
22
- Api key for a client. Can be obtained from user profile page.
23
- Starts with "Bearer".
24
- base_url: str
25
- Url of datagrok API. For example, "https://public.datagrok.ai/api"
26
-
27
- Returns
28
- -------
29
- DatagrokClient
30
- New instance of Datagrok Client
31
- '''
42
+ api_key : str
43
+ API key for authentication. Can be obtained from user profile page.
44
+ Should start with "Bearer".
45
+ base_url : str
46
+ Base URL of the Datagrok API (e.g., "https://public.datagrok.ai/api")
47
+ """
32
48
  self.api_key = api_key
33
49
  self.base_url = base_url
34
50
  self.headers = {
@@ -62,8 +78,7 @@ class DatagrokClient:
62
78
  Parameters
63
79
  ----------
64
80
  name: str
65
- Name for new table.
66
- If in grok format, can also specify project and namespace.
81
+ Name for new table. If in grok format, can also specify project and namespace.
67
82
  dataframe: pandas.Dataframe
68
83
  table data to save
69
84
 
@@ -79,8 +94,7 @@ class DatagrokClient:
79
94
  return response.json()
80
95
 
81
96
  def download_file(self, connector: str, path: str) -> Union[pd.DataFrame, bytes]:
82
- '''
83
- Download a file from Datagrok.
97
+ '''Download a file from Datagrok.
84
98
 
85
99
  Parameters
86
100
  ----------
@@ -143,24 +157,12 @@ class DatagrokClient:
143
157
  ext: str, optional
144
158
  A file extension filter to limit the types of files to sync. If not specified, all files are synced.
145
159
 
146
- recursive: bool, optional, default=True
160
+ recursive: bool
147
161
  Whether to sync the directory recursively, including subdirectories. If False, only files in the root of the directory are synced.
148
162
 
149
163
  sync_meta_file: str, optional, default=".sync_meta.json"
150
164
  The name of the metadata file that stores the eTags and sync information for the files. This file is used to track the current
151
165
  state of the local directory and should be present for syncing to work correctly.
152
-
153
- Raises
154
- ------
155
- Exception
156
- If the server response does not contain a multipart body when expected.
157
-
158
- Notes
159
- -----
160
- - If the server returns a `204 No Content` response, it indicates the folder is empty on the remote, so the local directory
161
- will be cleared and the sync metadata will be updated accordingly.
162
- - If a file is marked as "deleted" on the remote server, it will be removed from the local directory.
163
- - The function uses multipart responses for efficient file transfer and parsing of updates.
164
166
  """
165
167
  os.makedirs(dir, exist_ok=True)
166
168
  sync_meta_file_path = Path(dir, sync_meta_file)
@@ -261,23 +263,22 @@ class DatagrokClient:
261
263
  self._request('GET', endpoint, params=params)
262
264
 
263
265
  def create_dashboard(self, name: str, table_ids: str, layout_filename: Optional[str] = None) -> Dict[str, Any]:
264
- '''
265
- Creates a dashboard.
266
-
266
+ """Create a new dashboard in Datagrok.
267
+
267
268
  Parameters
268
269
  ----------
269
- name: str
270
- Name for new dashboard.
271
- table_ids: str
272
- Comma-separated list of table ids
273
- layout_filename: Optional[str]
274
- Optional. Filename for a project layout.
275
-
270
+ name : str
271
+ Name for the new dashboard
272
+ table_ids : str
273
+ Comma-separated list of table IDs to include in the dashboard
274
+ layout_filename : Optional[str]
275
+ Optional path to a JSON file containing the dashboard layout
276
+
276
277
  Returns
277
278
  -------
278
279
  Dict[str, Any]
279
- Identifiers of a project.
280
- '''
280
+ Dictionary containing the dashboard identifiers and metadata
281
+ """
281
282
  name = name.replace(':', '.')
282
283
  table_ids = table_ids.replace(':', '.')
283
284
  endpoint = f"/public/v1/dashboards/{name}/{table_ids}"
@@ -290,21 +291,20 @@ class DatagrokClient:
290
291
  return response.json()
291
292
 
292
293
  def call_function(self, name: str, invocation_parameters: Dict[str, Any]) -> Any:
293
- '''
294
- Performs a call of datagrok function.
295
-
294
+ """Call a Datagrok function with the specified parameters.
295
+
296
296
  Parameters
297
297
  ----------
298
- name: str
299
- Function name.
300
- invocation_parameters: Dict[str, Any]
301
- Dict with parameter values for function call.
302
-
298
+ name : str
299
+ Name of the function to call
300
+ invocation_parameters : Dict[str, Any]
301
+ Dictionary of parameters to pass to the function
302
+
303
303
  Returns
304
304
  -------
305
305
  Any
306
- Result of invocation: either a single value or a list or outputs.
307
- '''
306
+ The result of the function call
307
+ """
308
308
  name = name.replace(':', '.')
309
309
  endpoint = f"/public/v1/{name}/call"
310
310
  response = self._request('POST', endpoint, json=invocation_parameters, content_type="application/json")
@@ -312,25 +312,24 @@ class DatagrokClient:
312
312
 
313
313
  def list_groups(self, smart_filter: Optional[str] = None, include_personal: bool = False,
314
314
  include_members: bool = False, include_memberships: bool = False) -> List[Group]:
315
- '''
316
- Lists user groups from Datagrok.
317
-
315
+ """List groups from Datagrok with optional filtering and inclusion of related data.
316
+
318
317
  Parameters
319
318
  ----------
320
- smart_filter: Optional[str]
321
- Optional. Smart search filter.
322
- include_personal: bool
323
- Optional. Include also personal groups.
324
- include_members: bool
325
- Optional. Include group members.
326
- include_memberships: bool
327
- Optional. Include group memberships.
328
-
319
+ smart_filter : Optional[str]
320
+ Optional smart search filter to apply
321
+ include_personal : bool, default=False
322
+ Whether to include personal groups in the results
323
+ include_members : bool, default=False
324
+ Whether to include group members in the results
325
+ include_memberships : bool, default=False
326
+ Whether to include group memberships in the results
327
+
329
328
  Returns
330
329
  -------
331
330
  List[Group]
332
- A list of Group instances.
333
- '''
331
+ List of Group instances matching the criteria
332
+ """
334
333
  personal = str(include_personal).lower()
335
334
  smart_filter = smart_filter or f"personal={personal}"
336
335
  if smart_filter and not smart_filter.endswith(f"personal={personal}"):
@@ -353,19 +352,18 @@ class DatagrokClient:
353
352
  return [Group(self, **group_data) for group_data in response.json()]
354
353
 
355
354
  def lookup_groups(self, query: str) -> List[Group]:
356
- '''
357
- Looks up groups from Datagrok by name.
358
-
355
+ """Search for groups by name.
356
+
359
357
  Parameters
360
358
  ----------
361
- query: str
362
- Query string to match groups by name.
363
-
359
+ query : str
360
+ Search query to match against group names
361
+
364
362
  Returns
365
363
  -------
366
364
  List[Group]
367
- A list of Group instances that match the query.
368
- '''
365
+ List of Group instances matching the search query
366
+ """
369
367
  params = {"query": query}
370
368
  endpoint = "/public/v1/groups/lookup"
371
369
  response = self._request('GET', endpoint, params=params)
@@ -373,40 +371,38 @@ class DatagrokClient:
373
371
  return [Group(self, **group_data) for group_data in response.json()]
374
372
 
375
373
  def get_group(self, group_id: str) -> Group:
376
- '''
377
- Retrieves the details of a specific group by its ID. Includes group members and memberships.
378
-
374
+ """Get detailed information about a specific group.
375
+
379
376
  Parameters
380
377
  ----------
381
- group_id: str
382
- The ID of the group to retrieve details for.
383
-
378
+ group_id : str
379
+ ID of the group to retrieve
380
+
384
381
  Returns
385
382
  -------
386
383
  Group
387
- A Group instance containing the details of the group.
388
- '''
384
+ Group instance with detailed information
385
+ """
389
386
  endpoint = f"/public/v1/groups/{group_id}"
390
387
  response = self._request('GET', endpoint)
391
388
  return Group(self, **response.json())
392
389
 
393
390
  def get_group_members(self, group_id: str, admin: Optional[bool] = None) -> List[Group]:
394
- '''
395
- Retrieves the members of a specific group by its ID.
396
-
391
+ """Get members of a specific group.
392
+
397
393
  Parameters
398
394
  ----------
399
- group_id: str
400
- The ID of the group to retrieve members for.
401
- admin: Optional[bool]
402
- If set to True, only admin members will be included. If False, only non-admin members will be included.
403
- If not provided, only non-admin members are included.
404
-
395
+ group_id : str
396
+ ID of the group to get members for
397
+ admin : Optional[bool]
398
+ If True, returns only admin members. If False, returns only non-admin members.
399
+ If None, returns all members.
400
+
405
401
  Returns
406
402
  -------
407
403
  List[Group]
408
- A list of Group instances representing the members of the group.
409
- '''
404
+ List of Group instances representing the members
405
+ """
410
406
  endpoint = f"/public/v1/groups/{group_id}/members"
411
407
  params = {}
412
408
  if admin is not None:
@@ -415,22 +411,21 @@ class DatagrokClient:
415
411
  return [Group(self, **group_data) for group_data in response.json()]
416
412
 
417
413
  def get_group_memberships(self, group_id: str, admin: Optional[bool] = None) -> List[Group]:
418
- '''
419
- Retrieves the memberships of a specific group by its ID.
420
-
414
+ """Get memberships of a specific group.
415
+
421
416
  Parameters
422
417
  ----------
423
- group_id: str
424
- The ID of the group to retrieve members for.
425
- admin: Optional[bool]
426
- If set to True, only admin memberships will be included. If False, only non-admin memberships will be included.
427
- If not provided, only non-admin memberships are included.
428
-
418
+ group_id : str
419
+ ID of the group to get memberships for
420
+ admin : Optional[bool]
421
+ If True, returns only admin memberships. If False, returns only non-admin memberships.
422
+ If None, returns all memberships.
423
+
429
424
  Returns
430
425
  -------
431
426
  List[Group]
432
- A list of Group instances representing the members of the group.
433
- '''
427
+ List of Group instances representing the memberships
428
+ """
434
429
  endpoint = f"/public/v1/groups/{group_id}/memberships"
435
430
  params = {}
436
431
  if admin is not None:
@@ -439,103 +434,87 @@ class DatagrokClient:
439
434
  return [Group(self, **group_data) for group_data in response.json()]
440
435
 
441
436
  def request_group_membership(self, group_id: str) -> GroupMembershipRequest:
442
- '''
443
- Submits a request to join a specific group by its ID.
444
-
437
+ """Request membership in a group.
438
+
445
439
  Parameters
446
440
  ----------
447
- group_id: str
448
- The ID of the group to request membership for.
449
-
441
+ group_id : str
442
+ ID of the group to request membership for
443
+
450
444
  Returns
451
445
  -------
452
446
  GroupMembershipRequest
453
- The created membership request.
454
- '''
447
+ The created membership request
448
+ """
455
449
  endpoint = f"/public/v1/groups/{group_id}/request"
456
450
  response = self._request('POST', endpoint)
457
451
  return GroupMembershipRequest(self, **response.json())
458
452
 
459
453
  def get_group_membership_requests(self, group_id: str) -> List[GroupMembershipRequest]:
460
- '''
461
- Retrieves a list of membership requests for a specific group.
462
-
454
+ """Get pending membership requests for a group.
455
+
463
456
  Parameters
464
457
  ----------
465
- group_id: str
466
- The ID of the group to retrieve membership requests for.
467
-
458
+ group_id : str
459
+ ID of the group to get membership requests for
460
+
468
461
  Returns
469
462
  -------
470
463
  List[GroupMembershipRequest]
471
- A list of GroupMembershipRequest instances representing the requests for the group.
472
- '''
464
+ List of pending membership requests
465
+ """
473
466
  endpoint = f"/public/v1/groups/{group_id}/requests"
474
467
  response = self._request('GET', endpoint)
475
468
  return [GroupMembershipRequest(self, **request_data) for request_data in response.json()]
476
469
 
477
470
  def approve_membership_request(self, request_id: str) -> None:
478
- """
479
- Approve a membership request for a group.
480
-
471
+ """Approve a membership request.
472
+
481
473
  Parameters
482
474
  ----------
483
- request_id: str
484
- The ID of the membership request to approve.
485
-
486
- Returns
487
- -------
488
- None
489
- This method performs an approval action and does not return anything.
475
+ request_id : str
476
+ ID of the membership request to approve
490
477
  """
491
478
  endpoint = f"/public/v1/groups/requests/{request_id}/approve"
492
479
  self._request('POST', endpoint)
493
480
 
494
481
  def deny_membership_request(self, request_id: str) -> None:
495
- """
496
- Deny a membership request for a group.
497
-
482
+ """Deny a membership request.
483
+
498
484
  Parameters
499
485
  ----------
500
- request_id: str
501
- The ID of the membership request to deny.
502
-
503
- Returns
504
- -------
505
- None
506
- This method performs a denial action and does not return anything.
486
+ request_id : str
487
+ ID of the membership request to deny
507
488
  """
508
489
  endpoint = f"/public/v1/groups/requests/{request_id}/deny"
509
490
  self._request('POST', endpoint)
510
491
 
511
492
  def get_current_user_group(self) -> Group:
512
- """
513
- Get the current user's group. Includes members and memberships
514
-
493
+ """Get the current user's group.
494
+
515
495
  Returns
516
496
  -------
517
497
  Group
518
- The current user's group instance.
498
+ Group instance representing the current user's group
519
499
  """
520
500
  endpoint = "/public/v1/user/group"
521
501
  response = self._request('GET', endpoint)
522
502
  return Group(self, **response.json())
523
503
 
524
504
  def save_group(self, group: Group, save_relations: bool = False) -> Group:
525
- """
526
- Saves or updates group.
527
-
505
+ """Save or update a group.
506
+
528
507
  Parameters
529
508
  ----------
530
- group: Group
531
- The group to save or update.
532
- save_relations: bool
533
- Whether to save group relations (members and memberships).
534
-
509
+ group : Group
510
+ The group to save or update
511
+ save_relations : bool, default=False
512
+ Whether to save group relations (members and memberships)
513
+
535
514
  Returns
536
515
  -------
537
516
  Group
538
- The saved group instance.
517
+ The saved group instance
539
518
  """
540
519
  group.ensure_id()
541
520
  endpoint = "/public/v1/groups"
@@ -543,24 +522,28 @@ class DatagrokClient:
543
522
  return Group(self, **response.json())
544
523
 
545
524
  def _request(self, method: str, endpoint: str, content_type: str = "application/json", **kwargs: Any) -> requests.Response:
546
- """
547
- Internal method to make HTTP requests to the Datagrok API.
548
-
525
+ """Make an HTTP request to the Datagrok API.
526
+
549
527
  Parameters
550
528
  ----------
551
- method: str
529
+ method : str
552
530
  HTTP method to use (GET, POST, etc.)
553
- endpoint: str
531
+ endpoint : str
554
532
  API endpoint to call
555
- content_type: str
533
+ content_type : str, default="application/json"
556
534
  Content type for the request
557
- **kwargs: Any
535
+ **kwargs : Any
558
536
  Additional arguments to pass to requests.request
559
-
537
+
560
538
  Returns
561
539
  -------
562
540
  requests.Response
563
541
  The response from the API
542
+
543
+ Raises
544
+ ------
545
+ Exception
546
+ If the API returns an error or if the response is not successful
564
547
  """
565
548
  url = f"{self.base_url}{endpoint}"
566
549
  headers = self.headers.copy()
@@ -575,7 +558,21 @@ class DatagrokClient:
575
558
  raise Exception("Something went wrong during request")
576
559
  return response
577
560
 
578
- def _list_files_unix_style(self, root_dir: str, excludes: set = None) -> list[str]:
561
+ def _list_files_unix_style(self, root_dir: str, excludes: Optional[Set] = None) -> List[str]:
562
+ """List files in a directory using Unix-style paths.
563
+
564
+ Parameters
565
+ ----------
566
+ root_dir : str
567
+ Root directory to list files from
568
+ excludes : set, optional
569
+ Set of file paths to exclude from the listing
570
+
571
+ Returns
572
+ -------
573
+ List[str]
574
+ List of file paths relative to the root directory
575
+ """
579
576
  root = Path(root_dir)
580
577
 
581
578
  if excludes is None:
@@ -591,6 +588,13 @@ class DatagrokClient:
591
588
  return files
592
589
 
593
590
  def _clear_directory(self, path: str):
591
+ """Remove all files and subdirectories from a directory.
592
+
593
+ Parameters
594
+ ----------
595
+ path : str
596
+ Path to the directory to clear
597
+ """
594
598
  dir_path = Path(path)
595
599
  for item in dir_path.iterdir():
596
600
  if item.is_file() or item.is_symlink():