|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package cmd |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" |
| 10 | + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" |
| 11 | + "github.com/context-dot-dev/context-go-sdk/v2" |
| 12 | + "github.com/context-dot-dev/context-go-sdk/v2/option" |
| 13 | + "github.com/tidwall/gjson" |
| 14 | + "github.com/urfave/cli/v3" |
| 15 | +) |
| 16 | + |
| 17 | +var newsSearch = requestflag.WithInnerFlags(cli.Command{ |
| 18 | + Name: "search", |
| 19 | + Usage: "Searches live and historical company news for one company, identified in\nsearchBy by name, domain, ticker (optionally disambiguated by exchange), or\nISIN. Results can be filtered by publisher domain, publisher country, article\nlanguage, article type, and published-at date, and include stable story IDs,\nsource metadata, verified entity relevance, and cursor pagination.", |
| 20 | + Suggest: true, |
| 21 | + Flags: []cli.Flag{ |
| 22 | + &requestflag.Flag[map[string]any]{ |
| 23 | + Name: "search-by", |
| 24 | + Usage: "What to search for.", |
| 25 | + Required: true, |
| 26 | + BodyPath: "searchBy", |
| 27 | + }, |
| 28 | + &requestflag.Flag[*string]{ |
| 29 | + Name: "cursor", |
| 30 | + Usage: "Opaque next_cursor from the previous response, or null for the first page.", |
| 31 | + BodyPath: "cursor", |
| 32 | + }, |
| 33 | + &requestflag.Flag[map[string]any]{ |
| 34 | + Name: "filter-by", |
| 35 | + Usage: "Optional result filters.", |
| 36 | + Default: map[string]any{}, |
| 37 | + BodyPath: "filterBy", |
| 38 | + }, |
| 39 | + &requestflag.Flag[int64]{ |
| 40 | + Name: "limit", |
| 41 | + Usage: "Maximum results to return. Defaults to 10.", |
| 42 | + Default: 10, |
| 43 | + BodyPath: "limit", |
| 44 | + }, |
| 45 | + &requestflag.Flag[map[string]any]{ |
| 46 | + Name: "sort-by", |
| 47 | + Usage: "Result ordering. Defaults to newest.", |
| 48 | + Default: map[string]any{"type": "newest"}, |
| 49 | + BodyPath: "sortBy", |
| 50 | + }, |
| 51 | + &requestflag.Flag[[]string]{ |
| 52 | + Name: "tag", |
| 53 | + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", |
| 54 | + BodyPath: "tags", |
| 55 | + }, |
| 56 | + }, |
| 57 | + Action: handleNewsSearch, |
| 58 | + HideHelpCommand: true, |
| 59 | +}, map[string][]requestflag.HasOuterFlag{ |
| 60 | + "search-by": { |
| 61 | + &requestflag.InnerFlag[map[string]any]{ |
| 62 | + Name: "search-by.entity", |
| 63 | + Usage: "The company to search news for, identified by name, domain, ticker, or ISIN.", |
| 64 | + InnerField: "entity", |
| 65 | + }, |
| 66 | + &requestflag.InnerFlag[string]{ |
| 67 | + Name: "search-by.type", |
| 68 | + Usage: "How to search. Only entity search is supported.", |
| 69 | + InnerField: "type", |
| 70 | + }, |
| 71 | + }, |
| 72 | + "filter-by": { |
| 73 | + &requestflag.InnerFlag[[]string]{ |
| 74 | + Name: "filter-by.article-language", |
| 75 | + Usage: "Article languages to include. Up to 3.", |
| 76 | + InnerField: "articleLanguage", |
| 77 | + }, |
| 78 | + &requestflag.InnerFlag[[]string]{ |
| 79 | + Name: "filter-by.article-type", |
| 80 | + Usage: "Article types to include. Up to 3.", |
| 81 | + InnerField: "articleType", |
| 82 | + }, |
| 83 | + &requestflag.InnerFlag[map[string]any]{ |
| 84 | + Name: "filter-by.date", |
| 85 | + Usage: "Published-at window in epoch milliseconds.", |
| 86 | + InnerField: "date", |
| 87 | + }, |
| 88 | + &requestflag.InnerFlag[[]string]{ |
| 89 | + Name: "filter-by.source-country", |
| 90 | + Usage: "Publisher countries to include, as lowercase ISO 3166-1 alpha-2 codes. Up to 3.", |
| 91 | + InnerField: "sourceCountry", |
| 92 | + }, |
| 93 | + &requestflag.InnerFlag[[]string]{ |
| 94 | + Name: "filter-by.source-domain", |
| 95 | + Usage: "Publisher domains to include. Up to 3.", |
| 96 | + InnerField: "sourceDomain", |
| 97 | + }, |
| 98 | + }, |
| 99 | + "sort-by": { |
| 100 | + &requestflag.InnerFlag[string]{ |
| 101 | + Name: "sort-by.type", |
| 102 | + Usage: "Result ordering.", |
| 103 | + InnerField: "type", |
| 104 | + }, |
| 105 | + }, |
| 106 | +}) |
| 107 | + |
| 108 | +func handleNewsSearch(ctx context.Context, cmd *cli.Command) error { |
| 109 | + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) |
| 110 | + unusedArgs := cmd.Args().Slice() |
| 111 | + |
| 112 | + if len(unusedArgs) > 0 { |
| 113 | + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) |
| 114 | + } |
| 115 | + |
| 116 | + options, err := flagOptions( |
| 117 | + cmd, |
| 118 | + apiquery.NestedQueryFormatBrackets, |
| 119 | + apiquery.ArrayQueryFormatComma, |
| 120 | + ApplicationJSON, |
| 121 | + false, |
| 122 | + ) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + params := contextdev.NewsSearchParams{} |
| 128 | + |
| 129 | + var res []byte |
| 130 | + options = append(options, option.WithResponseBodyInto(&res)) |
| 131 | + _, err = client.News.Search(ctx, params, options...) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + obj := gjson.ParseBytes(res) |
| 137 | + format := cmd.Root().String("format") |
| 138 | + explicitFormat := cmd.Root().IsSet("format") |
| 139 | + transform := cmd.Root().String("transform") |
| 140 | + return ShowJSON(obj, ShowJSONOpts{ |
| 141 | + ExplicitFormat: explicitFormat, |
| 142 | + Format: format, |
| 143 | + RawOutput: cmd.Root().Bool("raw-output"), |
| 144 | + Title: "news search", |
| 145 | + Transform: transform, |
| 146 | + }) |
| 147 | +} |
0 commit comments