Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth for httpx client #133

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ directory as the json files).
3. Ensure that you have the `bnlroot.crt` file (which is deployed to all BNL managed machines) in the location specified
within the `.env` file.

### Code Style

This project uses `ruff` to format the code - in order to run the formatter simply type:
```bash
uvx ruff format
```
### Code Linting

This project uses `ruff` to check the code - in order to run the linter simply type:
```bash
uvx ruff check
```


### Updating Dependencies

The project uses `uv pip compile` to manage the `requirements.txt` and `requirements-dev.txt` files.
Expand Down
9 changes: 6 additions & 3 deletions src/nsls2api/services/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __call__(self):

async def _call_async_webservice(
url: str,
auth: tuple = None,
headers: dict = None,
) -> Response:
transport = None
Expand All @@ -47,6 +48,8 @@ async def _call_async_webservice(
timeout=httpx.Timeout(None, connect=30.0),
transport=transport,
limits=httpx.Limits(max_keepalive_connections=5, max_connections=5),
auth=auth,
headers=headers,
) as client:
logger.logger(f"Calling {url} using unshared client.")
resp: Response = await client.get(url)
Expand All @@ -58,13 +61,13 @@ async def _call_async_webservice(


async def _call_async_webservice_with_client(
url: str, headers: dict = None, client: httpx.AsyncClient = None
url: str, auth: tuple = None, headers: dict = None, client: httpx.AsyncClient = None
) -> Response:
if client is None:
# Then just use the general method that creates a client each time
return await _call_async_webservice(url, headers)
return await _call_async_webservice(url, auth, headers)
else:
resp: Response = await client.get(url, timeout=90.0)
resp: Response = await client.get(url, timeout=90.0, auth=auth, headers=headers)
resp.raise_for_status()
results = resp.json()
return results
Expand Down
16 changes: 12 additions & 4 deletions src/nsls2api/services/sync_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,26 @@ async def worker_synchronize_proposal_types_from_pass(
)


async def synchronize_proposal_from_pass(proposal_id: str, facility_name: FacilityName = FacilityName.nsls2) -> None:
async def synchronize_proposal_from_pass(
proposal_id: str, facility_name: FacilityName = FacilityName.nsls2
) -> None:
beamline_list = []
user_list = []
saf_list = []

try:
pass_proposal: PassProposal = await pass_service.get_proposal(proposal_id, facility_name)
pass_proposal: PassProposal = await pass_service.get_proposal(
proposal_id, facility_name
)
except pass_service.PassException as error:
error_message = f"Error retrieving proposal {proposal_id} from PASS"
logger.exception(error_message)
raise Exception(error_message) from error

# Get the SAFs for this proposal
pass_saf_list: list[PassSaf] = await pass_service.get_saf_from_proposal(proposal_id, facility_name)
pass_saf_list: list[PassSaf] = await pass_service.get_saf_from_proposal(
proposal_id, facility_name
)
for saf in pass_saf_list:
saf_beamline_list = []
for resource in saf.Resources:
Expand Down Expand Up @@ -351,7 +357,9 @@ async def update_proposals_with_cycle(
logger.warning(error)


async def worker_synchronize_proposal_from_pass(proposal_id: str, facility: FacilityName = FacilityName.nsls2) -> None:
async def worker_synchronize_proposal_from_pass(
proposal_id: str, facility: FacilityName = FacilityName.nsls2
) -> None:
start_time = datetime.datetime.now()

await synchronize_proposal_from_pass(proposal_id, facility)
Expand Down
Loading