 (ns swarmpit.registry.client
   (:refer-clojure :exclude [get])
   (:require [org.httpkit.client :as http]
-            [cheshire.core :refer [parse-string]]
+            [cheshire.core :refer [parse-string generate-string]]
             [swarmpit.token :as token]))
 
 (defn- build-url


(defn tags
  [registry repository-name]
  (let [headers (headers registry)]
    (get registry (str "/" repository-name "/tags/list") headers {})))

                 :query-params params}]
    (execute @(http/get url options))))

(defn- post
  [registry api headers body]
  (let [url (build-url registry api)
        options {:headers (merge headers {"Content-Type" "application/json"})
                 :body    (generate-string body)}]
    (execute @(http/post url options))))

(defn- basic-auth
  [registry]
  {:Authorization (token/generate-basic (:username registry)
                                        (:password registry))})

(defn- jwt-auth
  [token]
  {:Authorization (str "JWT " token)})

;; Dockerhub

(defn dockerhub-login
  [registry]
  (let [body (select-keys registry [:username :password])]
    (post registry "/users/login" {} body)))

(defn dockerhub-user-repo
  [registry token]
  (let [api (str "/repositories/" (:username registry))]
    (get registry api {} (jwt-auth token))))

(defn dockerhub-repositories
  [registry query page]
