-
Notifications
You must be signed in to change notification settings - Fork 67
Add JSON API for artifacts and docker tag search #272
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
Closed
Closed
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| defmodule Bob.Artifacts.ArtifactSearch do | ||
| @page 100 | ||
| @filter_keys ~w(query kind arch os)a | ||
|
|
||
| def page_size(), do: @page | ||
| def filter_keys(), do: @filter_keys | ||
|
|
||
| def parse_filters(params) do | ||
| %{ | ||
| query: params["query"] || "", | ||
| kind: params["kind"] || "", | ||
| arch: params["arch"] || "", | ||
| os: params["os"] || "" | ||
| } | ||
| end | ||
|
|
||
| def parse_offset(params) do | ||
| case Integer.parse(params["offset"] || "") do | ||
| {offset, ""} when offset > 0 -> offset | ||
| _ -> 0 | ||
| end | ||
| end | ||
| end |
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
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| defmodule BobWeb.Plugs.SearchJson do | ||
| @moduledoc """ | ||
| Serves the artifact and Docker tag search LiveViews as JSON for API clients | ||
| that send `Accept: application/json`, mirroring the LiveView for the same | ||
| query parameters. Other requests, and any other path, fall through untouched | ||
| to the regular browser pipeline and the LiveView. | ||
|
|
||
| This runs ahead of the `:accepts` plug so a JSON request is answered and | ||
| halted before that plug would reject it as not HTML. | ||
| """ | ||
|
|
||
| import Plug.Conn | ||
| import Phoenix.Controller, only: [json: 2] | ||
|
|
||
| alias Bob.Artifacts | ||
| alias Bob.Artifacts.{ArtifactSearch, DockerTagSearch} | ||
|
|
||
| def init(opts), do: opts | ||
|
|
||
| def call(conn, _opts) do | ||
| case conn.request_path do | ||
| "/artifacts" -> maybe_json(conn, &artifacts/1) | ||
| "/docker" -> maybe_json(conn, &docker_tags/1) | ||
| _other -> conn | ||
| end | ||
| end | ||
|
|
||
| defp maybe_json(conn, build) do | ||
| if json_requested?(conn) do | ||
| body = build.(fetch_query_params(conn).query_params) | ||
| conn |> json(body) |> halt() | ||
| else | ||
| conn | ||
| end | ||
| end | ||
|
|
||
| defp artifacts(params) do | ||
| offset = ArtifactSearch.parse_offset(params) | ||
| filters = ArtifactSearch.parse_filters(params) | ||
| page = Artifacts.artifact_page(filters, ArtifactSearch.page_size(), offset) | ||
|
|
||
| envelope(page, :artifacts, offset, ArtifactSearch.page_size(), &artifact/1) | ||
| end | ||
|
|
||
| defp docker_tags(params) do | ||
| offset = DockerTagSearch.parse_offset(params) | ||
| filters = DockerTagSearch.parse_filters(params) | ||
| page = Artifacts.docker_tag_page(filters, DockerTagSearch.page_size(), offset) | ||
|
|
||
| envelope(page, :tags, offset, DockerTagSearch.page_size(), &docker_tag/1) | ||
| end | ||
|
|
||
| defp envelope(%{results: results, total: total}, key, offset, page_size, row) do | ||
| %{key => Enum.map(results, row), total: total, offset: offset, page_size: page_size} | ||
| end | ||
|
|
||
| defp artifact(artifact) do | ||
| %{ | ||
| kind: artifact.kind, | ||
| arch: artifact.arch, | ||
| os: artifact.os, | ||
| name: artifact.name, | ||
| ref: artifact.ref, | ||
| sha256: artifact.sha256, | ||
| built_at: artifact.built_at && DateTime.to_iso8601(artifact.built_at) | ||
| } | ||
| end | ||
|
|
||
| defp docker_tag(tag) do | ||
| %{ | ||
| repo: tag.repo, | ||
| tag: tag.tag, | ||
| archs: tag.archs, | ||
| built_at: tag.built_at && DateTime.to_iso8601(tag.built_at) | ||
| } | ||
| end | ||
|
|
||
| defp json_requested?(conn) do | ||
| conn | ||
| |> get_req_header("accept") | ||
| |> Enum.any?(&String.contains?(&1, "application/json")) | ||
| end | ||
| end | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would fit better as a phoenix controller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with using a controller is that we couldn't mount the LiveView at the router any more. LiveViews rendered from a controller can't use
handle_params, so this would make the code more complex, that's why I decided to go for a plug. Or do you mean keeping the plug to dispatch, but calling a controller from the plug?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking a /api router scope with dedicated API-only controllers to keep the UI and API separate. Similar to how hexpm works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, going the explicit route. Sure, that works!