-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-client, react-api-client): Add client support for `/runs/:ru…
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { PUT, request } from '../request' | ||
|
||
import type { HostConfig } from '../types' | ||
import type { ResponsePromise } from '../request' | ||
import type { | ||
ErrorRecoveryPolicy, | ||
IfMatchType, | ||
UpdateErrorRecoveryPolicyRequest, | ||
UpdateErrorRecoveryPolicyResponse, | ||
} from './types' | ||
import type { RunCommandError, RunTimeCommand } from '@opentrons/shared-data' | ||
|
||
export type RecoveryPolicyRulesParams = Array<{ | ||
commandType: RunTimeCommand['commandType'] | ||
errorType: RunCommandError['errorType'] | ||
ifMatch: IfMatchType | ||
}> | ||
|
||
export function updateErrorRecoveryPolicy( | ||
config: HostConfig, | ||
runId: string, | ||
policyRules: RecoveryPolicyRulesParams | ||
): ResponsePromise<UpdateErrorRecoveryPolicyResponse> { | ||
const policy = buildErrorRecoveryPolicyBody(policyRules) | ||
|
||
return request< | ||
UpdateErrorRecoveryPolicyResponse, | ||
UpdateErrorRecoveryPolicyRequest | ||
>(PUT, `/runs/${runId}/errorRecoveryPolicy`, { data: policy }, config) | ||
} | ||
|
||
function buildErrorRecoveryPolicyBody( | ||
policyRules: RecoveryPolicyRulesParams | ||
): ErrorRecoveryPolicy { | ||
return { | ||
policyRules: policyRules.map(rule => ({ | ||
matchCriteria: { | ||
command: { | ||
commandType: rule.commandType, | ||
error: { | ||
errorType: rule.errorType, | ||
}, | ||
}, | ||
}, | ||
ifMatch: rule.ifMatch, | ||
})), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useMutation } from 'react-query' | ||
|
||
import { updateErrorRecoveryPolicy } from '@opentrons/api-client' | ||
|
||
import { useHost } from '../api' | ||
|
||
import type { | ||
UseMutationOptions, | ||
UseMutationResult, | ||
UseMutateFunction, | ||
} from 'react-query' | ||
import type { AxiosError } from 'axios' | ||
import type { | ||
RecoveryPolicyRulesParams, | ||
UpdateErrorRecoveryPolicyResponse, | ||
HostConfig, | ||
} from '@opentrons/api-client' | ||
|
||
export type UseUpdateErrorRecoveryPolicyResponse = UseMutationResult< | ||
UpdateErrorRecoveryPolicyResponse, | ||
AxiosError, | ||
RecoveryPolicyRulesParams | ||
> & { | ||
updateErrorRecoveryPolicy: UseMutateFunction< | ||
UpdateErrorRecoveryPolicyResponse, | ||
AxiosError, | ||
RecoveryPolicyRulesParams | ||
> | ||
} | ||
|
||
export type UseUpdateErrorRecoveryPolicyOptions = UseMutationOptions< | ||
UpdateErrorRecoveryPolicyResponse, | ||
AxiosError, | ||
RecoveryPolicyRulesParams | ||
> | ||
|
||
export function useUpdateErrorRecoveryPolicy( | ||
runId: string, | ||
options: UseUpdateErrorRecoveryPolicyOptions = {} | ||
): UseUpdateErrorRecoveryPolicyResponse { | ||
const host = useHost() | ||
|
||
const mutation = useMutation< | ||
UpdateErrorRecoveryPolicyResponse, | ||
AxiosError, | ||
RecoveryPolicyRulesParams | ||
>( | ||
[host, 'runs', runId, 'errorRecoveryPolicy'], | ||
(policyRules: RecoveryPolicyRulesParams) => | ||
updateErrorRecoveryPolicy(host as HostConfig, runId, policyRules) | ||
.then(response => response.data) | ||
.catch(e => { | ||
throw e | ||
}), | ||
options | ||
) | ||
|
||
return { | ||
...mutation, | ||
updateErrorRecoveryPolicy: mutation.mutate, | ||
} | ||
} |