Skip to content

Commit

Permalink
[WIP] Add more information to the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Willig authored and iwillig committed Aug 23, 2022
1 parent 2480a7d commit 872a555
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 34 deletions.
40 changes: 39 additions & 1 deletion dev-resources/test-0.0.3-SNAPSHOT/test.pom
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,52 @@
<artifactId>test</artifactId>
<version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>asdf</name>

<name>asdf</name>
<description>TEST</description>
<url>http://example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<contributors>
<contributor>
<name>Noelle</name>
<email>[email protected]</email>
<url>http://noellemarie.com</url>
<organization>Noelle Marie</organization>
<organizationUrl>http://noellemarie.com</organizationUrl>
<roles>
<role>tester</role>
</roles>
<timezone>America/Vancouver</timezone>
<properties>
<gtalk>[email protected]</gtalk>
</properties>
</contributor>
</contributors>
<developers>
<developer>
<id>juherr</id>
<name>Julien Herr</name>
<email>[email protected]</email>
</developer>
<developer>
<id>flgourie</id>
<name>Florian Gourier</name>
<email>[email protected]</email>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<scm>
<connection>scm:git:git://github.com/fake/test.git</connection>
<developerConnection>scm:git:ssh://[email protected]/fake/test.git</developerConnection>
Expand Down
24 changes: 20 additions & 4 deletions resources/queries/queryfile.sql
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,16 @@ SET token_hash = :token_hash
WHERE id = :token_id AND token_hash IS NULL

--name: find-groups-jars-information
SELECT j.jar_name, j.group_name, homepage, description, "user",
j.version AS latest_version, r2.version AS latest_release
SELECT j.jar_name,
j.group_name,
j.scm,
j.authors,
licenses,
homepage,
description,
"user",
j.version AS latest_version,
r2.version AS latest_release
FROM jars j
-- Find the latest version
JOIN (SELECT jar_name, group_name, MAX(created) AS created
Expand Down Expand Up @@ -466,8 +474,16 @@ WHERE j.group_name = :group_id
ORDER BY j.group_name ASC, j.jar_name ASC;

--name: find-jars-information
SELECT j.jar_name, j.group_name, homepage, description, "user",
j.version AS latest_version, r2.version AS latest_release
SELECT j.jar_name,
j.group_name,
j.scm,
j.authors,
licenses,
homepage,
description,
"user",
j.version AS latest_version,
r2.version AS latest_release
FROM jars j
-- Find the latest version
JOIN (SELECT jar_name, group_name, MAX(created) AS created
Expand Down
75 changes: 46 additions & 29 deletions src/clojars/routes/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,65 @@
[db :as db]
[stats :as stats]
[http-utils :refer [wrap-cors-headers]]]
[clojure.edn :as edn]
[compojure
[core :as compojure :refer [ANY context GET]]
[route :refer [not-found]]]
[ring.middleware.format-response :refer [wrap-restful-response]]
[ring.util.response :refer [response]]))

(defn hyrdate-artifact
[db stats group-id artifact-id artifact]
(-> artifact
(update :scm edn/read-string)
(update :licenses edn/read-string)
(update-in [:recent_versions]
(fn [versions]
(map (fn [version]
(assoc version
:downloads (stats/download-count stats group-id artifact-id (:version version))))
versions)))
response))

(defn hyrdate-artifact-stats
[artifact])

(comment
(fn [artifact]
(assoc
artifact
:recent_versions (db/recent-versions db group-id artifact-id)
:downloads (stats/download-count stats group-id artifact-id)))

(-> artifact
(hyrdate-artifact db stats group-id artifact-id artifact)))

(defn get-artifact [db stats group-id artifact-id]
(if-let [artifact (first (db/find-jars-information db group-id artifact-id))]
(-> artifact
(assoc
:recent_versions (db/recent-versions db group-id artifact-id)
:downloads (stats/download-count stats group-id artifact-id))
(update-in [:recent_versions]
(fn [versions]
(map (fn [version]
(assoc version
:downloads (stats/download-count stats group-id artifact-id (:version version))))
versions)))
response)
(response artifact)
(not-found nil)))

(defn handler [db stats]
(compojure/routes
(context "/api" []
(GET ["/groups/:group-id", :group-id #"[^/]+"] [group-id]
(if-let [jars (seq (db/find-jars-information db group-id))]
(response
(map (fn [jar]
(assoc jar
:downloads (stats/download-count stats group-id (:jar_name jar))))
jars))
(not-found nil)))
(GET ["/artifacts/:artifact-id", :artifact-id #"[^/]+"] [artifact-id]
(get-artifact db stats artifact-id artifact-id))
(GET ["/artifacts/:group-id/:artifact-id", :group-id #"[^/]+", :artifact-id #"[^/]+"] [group-id artifact-id]
(get-artifact db stats group-id artifact-id))
(GET "/users/:username" [username]
(if-let [groups (seq (db/find-groupnames db username))]
(response {:groups groups})
(not-found nil)))
(ANY "*" _
(not-found nil)))))
(GET ["/groups/:group-id", :group-id #"[^/]+"] [group-id]
(if-let [jars (seq (db/find-jars-information db group-id))]
(response
(map (fn [jar]
(assoc jar
:downloads (stats/download-count stats group-id (:jar_name jar))))
jars))
(not-found nil)))
(GET ["/artifacts/:artifact-id", :artifact-id #"[^/]+"] [artifact-id]
(get-artifact db stats artifact-id artifact-id))
(GET ["/artifacts/:group-id/:artifact-id", :group-id #"[^/]+", :artifact-id #"[^/]+"] [group-id artifact-id]
(get-artifact db stats group-id artifact-id))
(GET "/users/:username" [username]
(if-let [groups (seq (db/find-groupnames db username))]
(response {:groups groups})
(not-found nil)))
(ANY "*" _
(not-found nil)))))

(defn routes [db stats]
(-> (handler db stats)
Expand Down
3 changes: 3 additions & 0 deletions test/clojars/integration/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
(testing "list group artifacts"
(let [resp (get-api [:groups "org.clojars.dantheman"] {:accept :json})
body (json/parse-string (:body resp) true)]

(is (= {:latest_version "0.0.3-SNAPSHOT"
:latest_release "0.0.2"
:jar_name "test"
Expand All @@ -70,6 +71,8 @@
(is (= {:latest_version "0.0.3-SNAPSHOT"
:latest_release "0.0.2"
:jar_name "test"
:license ""
:scm_url ""
:group_name "org.clojars.dantheman"
:user "dantheman"
:description "TEST"
Expand Down

0 comments on commit 872a555

Please sign in to comment.