Skip to content

Instantly share code, notes, and snippets.

@reedho
Created April 14, 2023 03:47
Show Gist options
  • Save reedho/9f42dfff5271551493cafebd257a6008 to your computer and use it in GitHub Desktop.
Save reedho/9f42dfff5271551493cafebd257a6008 to your computer and use it in GitHub Desktop.
Example request with contentful and sanity http api

Contentful.com

(require 'org.httpkit.client)
(do
  (def _base_url "https://cdn.contentful.com")
  (def _env "master")
  (def _space_id "xyz")
  (def _token "xyz")
  )

All entries

(def _rsp
  @(org.httpkit.client/get
     (format "%s/spaces/%s/environments/%s/entries"
             _base_url _space_id _env)
     {:query-params {"access_token" _token}}
     ))

Assets

(def _rsp
  @(org.httpkit.client/get
     (format "%s/spaces/%s/environments/%s/assets"
             _base_url _space_id _env)
     {:query-params {"access_token" _token}}
     ))

(def _rsp
  @(org.httpkit.client/get
     (format "%s/spaces/%s/environments/%s/assets"
             _base_url _space_id _env)
     {:query-params {"access_token" _token
                     "skip" 100}}
     ))

Sanity.io

GROQ query for searching unused assets:

*[ _type in ["sanity.imageAsset", "sanity.fileAsset"] ]
  {_id, "refs": count(*[ references(^._id) ])}
  [ refs == 0 ]
  ._id
(do
  (def _api_domain "api.sanity.io")
  (def _api_version "v2021-10-21")
  (def _pid "bqhurrh3") ;; projectId
  (def _dsid "demo")
  (def _base_url (format "https://%s.%s/%s" _pid _api_domain _api_version))
  (def _token "")
  )

Query documents

(def _rsp
  @(org.httpkit.client/get
     (format "%s/data/query/" _base_url _dsid)
     {:query-params {"query" "*[_type == 'sanity.imageAsset']"}}))

Uploading assets

(def _rsp
  @(org.httpkit.client/post
     (format "%s/assets/images/%s" _base_url _dsid)
     {:headers {"Authorization" (str "Bearer " _token)
                "Content-Type" "image/jpeg"}
      :body (clojure.java.io/input-stream "/tmp/img2/0174042f-0007-4e4c-a04b-cca543c6c45b.jpg")
      }))

Delete assets

Documentation link is here.

(def _rsp
    @(org.httpkit.client/post
       (format "%s/data/mutate/" _base_url _dsid)
       {:headers {"Authorization" (str "Bearer " _token)
                  "Content-Type"  "application/json"}
        :body    (cheshire.core/generate-string
                   {:mutations
                    (->> "image-0f292a72f272ded913e13f4a9573c4d83f45e628-1202x1161-jpg
image-11e588561b4e27a65f2508c7ce0dd1619957923a-1280x1054-jpg
image-156a9ce6593510514d1a9663733bb8c7fee36220-2322x4128-jpg
image-1c53e4869cd878c412285844103cff4b38c5bf72-716x576-jpg
image-290d91aa4d26ab8e807eded9c0c24f747496e0ca-1280x960-jpg
image-30f4d80b71b1bc96785cc8ecb21d349dcaa2c348-720x435-jpg
image-361e90859aaf7aebbed2c063a2348fc05ba0e034-586x300-jpg
image-3701002c0bf14bde3a11379f0fda7a80533f03ce-720x715-jpg
image-3c35115da50eb30acbc14b1cfe9bfd938198248d-326x326-jpg
image-419a03546ceacbecd8712290f6476314d92f26c5-780x1040-jpg
image-42f7f365b93e015adeee0cf2f43fde65f865d060-4128x2322-jpg
image-4455b6a3d799262dccde94d9b5319924ac963f6f-540x1080-jpg
image-47d0d760977b6e1f34bee5c735e80cf1e0cfbb85-1070x1058-jpg
image-4a126c553f3c088238496810462d6198cd288387-720x531-jpg
image-5130b2394e881bafefbeacbfac9777612365f82d-720x768-jpg
image-5281133661b4184bd5261cf895179ea9757d8801-1200x922-jpg
image-6ca593adf425c05388e98d587556c1e800d8b398-960x847-jpg
image-7849e921874b5ce4d8c6cac8e8e3422f83a25665-1280x960-jpg
image-7e1f6b8c3e260d81d6bc9166b2519304140fbf1e-720x862-jpg
image-7e69990d0aab43e45207a380253d82efb65ef10b-720x637-jpg
image-8a1552fe21694d4bd45e5092b554df00fc76a077-640x640-jpg
image-8a2e005f5bbf4cf7456febb6c6ff678a258598f1-780x1040-jpg
image-8d12ce0aa6ebf98354d358a5610282e7f32c7429-1032x774-jpg
image-94a428b91ddbdb8699e941b464667a9aff629570-719x495-jpg
image-9973778ba507c47a18d0951f0827a975e957d0c0-960x960-jpg
image-a6880172222c0bd68174fa9d4c133fdf14d0f3af-720x1280-jpg
image-ab9a5c9d19a9565ce1732918c326a824065903ac-4128x3096-jpg
image-ace251778aafcd27a8590207f0449e7bb8f4fc6d-1255x796-jpg
image-b8150b29de47cb62abe37905f605e5a0eddfa424-1200x1096-jpg
image-baa2eae4b1e034f00c4421e8000abef9f09e47c2-720x517-jpg
image-bb7850f09ed2dc3de8e58610462d57a320f58508-712x949-jpg
image-c51d07de966a3494df96d00b23d2871416ca5da2-720x648-jpg
image-c9dd09df40a1dc2d8d3e06b7e325ec8c315f51e5-660x81-jpg
image-cf885501c183ed7cd690910bae524b5515c5aab5-1080x1077-jpg
image-de4b63155989664d1537e298a94788979c76afa8-1024x1024-jpg
image-f1b8d208ab1f3b1f4babbacb27233347afad074b-1032x774-jpg
image-f5c3165f735bc633ad273b248184c56a733ee667-1040x780-jpg"
                         (clojure.string/split-lines)
                         (mapv (fn [x]
                                 {:delete {:id x}})))
                    })}))

Batch upload example

(->> _xs ;; ["x1.jpg" ...]
     (run! (fn [x]
             (println x)
             (-> @(org.httpkit.client/post
                    (format "%s/assets/images/%s" _base_url _dsid)

                    {:headers {"Authorization" (str "Bearer " _token)
                               "Content-Type" "image/jpeg"}
                     :body (clojure.java.io/input-stream (str "/tmp/img2/" x))
                     })
                 :status println)
             (println)
             )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment