|
| 1 | +package ai_content |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/hostinger/api-cli/api" |
| 9 | + "github.com/hostinger/api-cli/client" |
| 10 | + "github.com/hostinger/api-cli/output" |
| 11 | + "github.com/hostinger/api-cli/utils" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var GenerationResultCmd = &cobra.Command{ |
| 16 | + Use: "generation-result <username> <software> <generation_id>", |
| 17 | + Short: "Get generation result", |
| 18 | + Long: "Get the result of an AI content generation started for the specified WordPress\ninstallation, such as an image generation started with\nPOST /api/hosting/v1/accounts/{username}/wordpress/{software}/content/images/generate.\n\nWhile the generation is still running only `status: in-progress` is returned; poll every\nfew seconds. Once `completed`, `result` holds the generated content shaped according to\n`type`: temporary image URLs together with the time they expire for `image-generation`, or\nthe generated website document for `website-generation` (started from the Hostinger\nWordPress plugin). A `failed` generation has no `result`. Unknown or expired generation\nIDs return 404.\n\nProvide the WordPress installation (software) identifier in the path. It can\nbe obtained from GET /api/hosting/v1/wordpress/installations (the `id` field).", |
| 19 | + Args: cobra.MatchAll(cobra.ExactArgs(3)), |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + utils.EnumCheck(cmd, "type", []string{"website-generation", "image-generation"}) |
| 22 | + r, err := api.Request().HostingGetGenerationResultV1WithResponse(context.TODO(), args[0], args[1], uuid.MustParse(args[2]), generationResultParams(cmd)) |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + |
| 27 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | +func init() { |
| 32 | + GenerationResultCmd.Flags().StringP("type", "", "", "Generation type to look up. When omitted, every generation type is checked. (one of: website-generation, image-generation)") |
| 33 | +} |
| 34 | + |
| 35 | +func generationResultParams(cmd *cobra.Command) *client.HostingGetGenerationResultV1Params { |
| 36 | + params := &client.HostingGetGenerationResultV1Params{} |
| 37 | + if cmd.Flags().Changed("type") { |
| 38 | + v, _ := cmd.Flags().GetString("type") |
| 39 | + e := client.HostingGetGenerationResultV1ParamsType(v) |
| 40 | + params.Type = &e |
| 41 | + } |
| 42 | + return params |
| 43 | +} |
0 commit comments