Skip to content
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
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# Changelog

## 1.2.0 (unreleased)

- `Client.lastError()` returns a `serpapi.ErrorInfo` with the details of the
last failure, mirroring serpapi-ruby's `SerpApiError`: `message` (as
`errorMessage()`), the HTTP `status`, the request `url` without the
`api_key`, and the `search_id` from the error payload
- `Client.format`: printing a client with `{f}` shows its configuration
with the API key masked, as serpapi-ruby's `inspect` does
(`abcd****wxyz`, `****` for a key of eight characters or fewer)
- README documents `engine()`, `apiKey()` and `serpapi.version`
- `rake release` runs `lint`, `test`, `itest` and `oobt` before tagging,
and refuses to start unless it runs from `master` with `SERPAPI_KEY` set
and an empty `git status --porcelain` (the previous `git diff --quiet`
missed staged and untracked files)

### Behavior changes

- `errorMessage()` and `lastError()` describe the most recent request only:
every request clears the previous failure, so they return null after a
success or a transport error. Previously the last failure lingered, and a
DNS error after a 404 still reported the 404

### Breaking changes

These ship in a minor release by decision; pin `v1.1.0` to keep the old API.

- The API key is no longer kept in `Client.params`: it moved to its own
`api_key` field, which `{any}` prints as an address, so dumping the
client state no longer leaks it. `apiKey()` is unchanged; code that read
the key from `client.params.items` must call `apiKey()` instead
- `Client.last_error` is now a `?serpapi.ErrorInfo` instead of `?[]u8`;
`errorMessage()` is unchanged

## 1.1.0 (2026-09-23)

- Markdown output: `md` (Search API) and `searchArchiveMd` (Search Archive
Expand Down
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ All fields are optional. Parameters passed to a method call override the
defaults provided to the constructor. Call `client.deinit()` when the client
is no longer needed; it closes the connection and frees all resources.

The client reports its configuration back:

```zig
client.engine() // ?[]const u8: default engine, "google" here
client.apiKey() // ?[]const u8: the API key given to the constructor
serpapi.version // the library version, "1.2.0", reported to serpapi.com
```

Printing the client masks the API key, as serpapi-ruby's `inspect` does —
the first and last four characters of a key longer than eight, `****`
otherwise:

```zig
std.debug.print("{f}\n", .{client});
// serpapi.Client{ .engine = "google", .timeout = 30, .persistent = true, .api_key = "abcd****wxyz" }
```

The key is stored apart from the other defaults, so dumping the client's
fields with `{any}` does not reveal it either.

`timeout` bounds the whole request — connecting, TLS handshake, and reading
the response — and a request that overruns it fails with `error.Timeout`
(details in `client.errorMessage()`), dropping the stalled connection so the
Expand Down Expand Up @@ -298,8 +318,30 @@ requested type), `error.Timeout` (the request overran the client's `timeout`),
plus any network / TLS / allocation errors propagated from the standard
library. The four client errors are grouped in `serpapi.Error`.

A `Client` is not thread-safe — `errorMessage()` reports the last failure seen
by that instance — so use one client per thread.
`client.lastError()` returns the details of the last failure, mirroring
serpapi-ruby's `SerpApiError`: the `message` (same as `errorMessage()`), the
HTTP `status`, the request `url` with the `api_key` left out — safe to log —
and the `search_id` when serpapi.com assigned one to the failed search.
`status` is null when no response arrived (`error.Timeout`).

Both describe the most recent request only: every request clears them
first, so they are null after a success, and after a transport error (DNS,
connection, TLS) — such errors carry no details beyond the Zig error itself.

```zig
var archived = client.searchArchive(search_id) catch |err| {
if (client.lastError()) |info| {
// SerpApiError: HTTP 404 https://serpapi.com/searches/<id>.json?source=serpapi-zig%3A1.2.0: Search not found.
std.debug.print("{t}: HTTP {?d} {?s}: {s}\n", .{
err, if (info.status) |s| @intFromEnum(s) else null, info.url, info.message,
});
}
return err;
};
```

A `Client` is not thread-safe — `errorMessage()` and `lastError()` report the
last failure seen by that instance — so use one client per thread.

## Search asynchronous

Expand Down
23 changes: 18 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,28 @@ namespace :install do
end
end

desc 'release: tag the current version and push it to GitHub'
task :release do
namespace :release do
desc 'fail fast unless on a clean master with SERPAPI_KEY set'
task :preflight do
# release pushes master and the tag, so it must run from master
branch = `git rev-parse --abbrev-ref HEAD`.strip
raise 'git rev-parse failed' unless $?.success?
raise "release runs from master, not #{branch}" unless branch == 'master'

# `git diff --quiet` misses staged and untracked files; porcelain lists all
dirty = `git status --porcelain`
raise 'git status failed' unless $?.success?
raise "working tree is dirty, commit first:\n#{dirty}" unless dirty.strip.empty?
raise 'SERPAPI_KEY is required: without it the integration tests skip' if ENV['SERPAPI_KEY'].to_s.empty?
end
end

desc 'release: run every check, then tag the current version and push it to GitHub'
task release: %i[release:preflight lint test itest oobt] do
version = File.read('build.zig.zon')[/\.version\s*=\s*"([^"]+)"/, 1]
raise 'cannot read version from build.zig.zon' if version.nil?

puts "releasing v#{version}"
sh 'git diff --quiet' do |ok, _|
raise 'working tree is dirty, commit first' unless ok
end
sh "git tag -a v#{version} -m 'Release v#{version}'"
sh 'git push origin master --tags'
puts "released: https://github.com/serpapi/serpapi-zig/releases/tag/v#{version}"
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.name = .serpapi,
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "1.1.0",
.version = "1.2.0",
// Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the
// package is first created, and then *never changes*. This allows
Expand Down
Loading
Loading