-
Notifications
You must be signed in to change notification settings - Fork 0
Fix encoding templates create to send raw YAML body #5
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
Open
dweinber
wants to merge
5
commits into
main
Choose a base branch
from
fix/templates-create-yaml-body
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f83b56a
Fix encoding templates create to send raw YAML body
dweinber 226cdae
fix templates create json output and docs
dweinber 21db527
Merge remote-tracking branch origin/main into fix/templates-create-ya…
dweinber c563e65
Address PR review comments
dweinber 89ae17a
Address template create review comments
dweinber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,25 +1,97 @@ | ||
| import {Args, Flags} from '@oclif/core'; | ||
| import {Args} from '@oclif/core'; | ||
| import {readFileSync} from 'node:fs'; | ||
| import {BaseCommand} from '../../../lib/base-command.js'; | ||
| import {API_BASE_URL, resolveAuth} from '../../../lib/client.js'; | ||
|
|
||
| interface CreateResponse { | ||
| data?: { | ||
| result?: { | ||
| id?: string; | ||
| [key: string]: unknown; | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| interface ErrorResponse { | ||
| developerMessage?: string; | ||
| message?: string; | ||
| requestId?: string; | ||
| data?: { | ||
| developerMessage?: string; | ||
| message?: string; | ||
| requestId?: string; | ||
| }; | ||
| } | ||
|
|
||
| interface ApiError extends Error { | ||
| httpStatusCode: number; | ||
| developerMessage: string; | ||
| requestId?: string; | ||
| } | ||
|
|
||
| export default class EncodingTemplateCreate extends BaseCommand { | ||
| static override description = 'Store an encoding template for reuse'; | ||
| static override description = | ||
| 'Store an encoding template for reuse. The template name is taken from `metadata.name` in the YAML.'; | ||
|
|
||
| static override args = { | ||
| file: Args.string({description: 'Path to YAML template file', required: true}), | ||
| }; | ||
|
|
||
| static override flags = { | ||
| ...BaseCommand.baseFlags, | ||
| name: Flags.string({description: 'Template name', required: true}), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const {args, flags} = await this.parse(EncodingTemplateCreate); | ||
| const content = readFileSync(args.file, 'utf-8'); | ||
| const templatePayload: Record<string, unknown> = {name: flags.name, template: content}; | ||
| const result = await (await this.getApi()).encoding.templates.create(templatePayload as never); | ||
| this.log(`Template created: ${(result as {id?: string}).id}`); | ||
| await this.isJsonMode(); | ||
|
|
||
| const {apiKey, tenantOrgId} = resolveAuth(flags['api-key']); | ||
|
|
||
| // The /encoding/templates endpoint expects a raw YAML body with | ||
| // Content-Type: application/yaml. The SDK only sends application/json, | ||
| // so we issue this request directly. | ||
| const headers: Record<string, string> = { | ||
| 'X-Api-Key': apiKey, | ||
| 'X-Api-Client': '@bitmovin/cli', | ||
| 'Content-Type': 'application/yaml', | ||
| }; | ||
| if (tenantOrgId) headers['X-Tenant-Org-Id'] = tenantOrgId; | ||
|
|
||
| const response = await fetch(`${API_BASE_URL}/encoding/templates`, { | ||
| method: 'POST', | ||
| headers, | ||
| body: content, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const rawBodyText = await response.text(); | ||
| let developerMessage = rawBodyText; | ||
| let requestId = response.headers?.get('X-Request-Id') ?? undefined; | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(rawBodyText) as ErrorResponse; | ||
| developerMessage = | ||
| parsed.developerMessage ?? | ||
| parsed.message ?? | ||
| parsed.data?.developerMessage ?? | ||
| parsed.data?.message ?? | ||
| rawBodyText; | ||
| requestId = requestId ?? parsed.requestId ?? parsed.data?.requestId; | ||
| } catch { | ||
| // leave developerMessage as raw response body | ||
| } | ||
|
|
||
| const error = new Error(`Failed to create template (${response.status}): ${developerMessage}`) as ApiError; | ||
| error.httpStatusCode = response.status; | ||
| error.developerMessage = developerMessage; | ||
| if (requestId) error.requestId = requestId; | ||
| throw error; | ||
| } | ||
|
|
||
| const json = (await response.json()) as CreateResponse; | ||
| const result = json.data?.result ?? {}; | ||
| this.log(`Template created: ${result.id ?? '<unknown id>'}`); | ||
| await this.outputData(result); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.