Skip to content

Commit

Permalink
Merge pull request #22 from Crown-Commercial-Service/add-cdp-api-client
Browse files Browse the repository at this point in the history
Add CDP API Client to project
  • Loading branch information
tim-s-ccs authored Feb 26, 2025
2 parents 1f335b6 + 13d5490 commit 4ea2f15
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)


This project contains a script for using the API and Search API clients in a IPython shell
This project contains a script for using the API, Search API and Central Digital Platform API clients in a IPython shell

## Setup

Expand All @@ -21,7 +21,7 @@ invoke requirements-dev

## API Client script

To use the scripts you will need the API and/or Search API tokens.
To use the scripts you will need the API, Search API and/or Central Digital Platform API tokens.
Speak to a developer who will be able to share these with you.

To run the API client script, first you need to enter the virtual environment with:
Expand All @@ -43,7 +43,7 @@ In [1]:
You can then use the API client to make calls to the API, for example:
```
In [1]: data.get_framework('g-cloud-14')
Out[1]:
Out[1]:
{
'frameworks': {
'id': 17,
Expand Down Expand Up @@ -74,10 +74,16 @@ To get the Search API client pass in the the search api token (`--search-api-tok
./scripts/api-clients-shell.py development --api-token=theToken --search-api-token=theToken
```

To get the Central Digital Platform API client pass in the the cdp api token (`--cdp-api-token`):
```
./scripts/api-clients-shell.py development --api-token=theToken --cdp-api-token=theToken
```

To list all the possible API client methods and their documentation, you can run the `api-clients-shell-list.py` script:
```
./scripts/api-clients-shell-list.py data
./scripts/api-clients-shell-list.py search
./scripts/api-clients-shell-list.py cdp
```

## Updating Python dependencies
Expand Down
9 changes: 7 additions & 2 deletions scripts/api-clients-shell-list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
./scripts/api-clients-shell.py
./scripts/api-clients-shell.py data
./scripts/api-clients-shell.py search
./scripts/api-clients-shell.py cdp
"""

import argparse
import sys
import pydoc


from dmapiclient import DataAPIClient, SearchAPIClient
from dmapiclient import DataAPIClient, SearchAPIClient, CentralDigitalPlatformAPIClient

sys.path.insert(0, '.')

Expand Down Expand Up @@ -52,7 +53,7 @@ def list_methods(client_class):
parser = argparse.ArgumentParser()
parser.add_argument('client', default='data', help='The client you want to check the methods for', nargs='?',
choices=[
'data', 'search', 'preview', 'pre-production',
'data', 'search', 'cdp',
])

args = parser.parse_args()
Expand All @@ -66,3 +67,7 @@ def list_methods(client_class):
if client == 'search':
print('Listing SearchAPIClient methods...')
list_methods(SearchAPIClient)

if client == 'cdp':
print('Listing CentralDigitalPlatformAPIClient methods...')
list_methods(CentralDigitalPlatformAPIClient)
65 changes: 56 additions & 9 deletions scripts/api-clients-shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
--api-token API key (needed if wanting to use the API)
--search-api-url Override the implicit SearchAPI URL
--search-api-token SearchAPI key (needed if wanting to use the Search API)
-rw --read-write Access to API calls that write data as well as read
--cdp-api-url Override the implicit Central Digtial Platform API URL
--cdp-api-token Override for the Central Digtial Platform API key
-rw --read-write Access to API calls that write data as well as read
Example:
./scripts/api-clients-shell.py
Expand All @@ -27,11 +29,11 @@
from IPython.terminal.prompts import Prompts, Token
from traitlets.config import Config

from dmapiclient import DataAPIClient, SearchAPIClient
from dmapiclient import DataAPIClient, SearchAPIClient, CentralDigitalPlatformAPIClient

sys.path.insert(0, '.')
from dmscripts.helpers.updated_by_helpers import get_user
from dmutils.env_helpers import get_api_endpoint_from_stage
from dmutils.env_helpers import get_api_endpoint_from_stage, get_cdp_api_endpoint_from_stage


def DMEnvironmentPrompt(stage: str, read_write: bool = False):
Expand Down Expand Up @@ -76,11 +78,39 @@ def __dir__(self):
'local', 'development', 'preview', 'pre-production',
])

parser.add_argument('--api-url', help='Override the implicit API URL', type=str)
parser.add_argument('--api-token', help='API key (needed if wanting to use the API)', type=str)
parser.add_argument(
'--api-url',
help='Override the implicit API URL',
type=str
)
parser.add_argument(
'--api-token',
help='API key (needed if wanting to use the API)',
type=str
)

parser.add_argument(
'--search-api-url',
help='Override the implicit SearchAPI URL',
type=str
)
parser.add_argument(
'--search-api-token',
help='SearchAPI key (needed if wanting to use the Search API)',
type=str
)

parser.add_argument(
'--cdp-api-url',
help='Override the implicit Central Digtial Platform API URL',
type=str
)
parser.add_argument(
'--cdp-api-token',
help='Override for the Central Digtial Platform API key (don\'t decrypt from dm-credentials)',
type=str
)

parser.add_argument('--search-api-url', help='Override the implicit SearchAPI URL', type=str)
parser.add_argument('--search-api-token', help='SearchAPI key (needed if wanting to use the Search API)', type=str)
parser.add_argument('--read-write', '-rw',
help='Access to API calls that write data as well as read', action='store_true')

Expand All @@ -94,9 +124,10 @@ def __dir__(self):
print('Setting API tokens...')
api_token = 'myToken' if stage.lower() == 'local' else args.api_token
search_api_token = 'myToken' if stage.lower() == 'local' else args.search_api_token
cdp_api_token = args.cdp_api_token

if not api_token and not search_api_token:
print("Must supply one of --api-token or --search-api-token to access the client")
if not api_token and not search_api_token and not cdp_api_token:
print("Must supply one of --api-token, --search-api-token or --cdp-api-token to access the client")
sys.exit(1)

print('Creating clients...')
Expand All @@ -116,7 +147,10 @@ def __dir__(self):

user_ns["data"] = data

print('Use \'data\' for Data API client')

if search_api_token:
print('Creating Search API client...')
search = SearchAPIClient(
base_url=args.search_api_url or get_api_endpoint_from_stage(stage, app='search-api'),
auth_token=search_api_token,
Expand All @@ -125,6 +159,19 @@ def __dir__(self):

user_ns["search"] = search

print('Use \'search\' for Search API client')

if cdp_api_token:
print('Creating Central Digital Platform API client...')
cdp = CentralDigitalPlatformAPIClient(
base_url=args.cdp_api_url or get_cdp_api_endpoint_from_stage(stage),
api_key=cdp_api_token,
)

user_ns["cdp"] = cdp

print('Use \'cdp\' for Central Digital Platform API client')

ipython_config = Config()
ipython_config.TerminalInteractiveShell.prompts_class = DMEnvironmentPrompt(stage, args.read_write)

Expand Down

0 comments on commit 4ea2f15

Please sign in to comment.