generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- The api key comes from the enviroment variable `FOREM_API_KEY` - API calls don't get the api key as a parameter anymore - All the get calls are asynchronous
- Loading branch information
Showing
11 changed files
with
510 additions
and
400 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,58 +1,98 @@ | ||
(local curl (require :plenary.curl)) | ||
(local notify (require :forem-nvim.notify)) | ||
(local job (require :plenary.job)) | ||
(local M {}) | ||
(local base-url "https://dev.to/api") | ||
|
||
(λ key [] | ||
vim.env.FOREM_API_KEY) | ||
|
||
(λ handle-async-error [response] | ||
(notify.error (.. "Error: " response.body.error))) | ||
|
||
(λ get-article-template [title] | ||
(string.format "--- | ||
title: %s | ||
published: false | ||
description: | ||
description: | ||
tags: | ||
# cover_image: https://direct_url_to_image.jpg | ||
# Use a ratio of 100:42 for best results. | ||
--- | ||
" title)) | ||
|
||
(λ request [request-fun api-key endpoint options] | ||
(λ request [request-fun endpoint options] | ||
(let [parameters (vim.tbl_extend :force | ||
{:url (.. base-url endpoint) | ||
:headers {: api-key | ||
:content_type :application/json}} | ||
:headers {:api-key (key) | ||
:content_type :application/json | ||
:accept :application/vnd.forem.api-v1+json}} | ||
options) | ||
response (request-fun parameters)] | ||
(if response.body | ||
(vim.tbl_extend :force response | ||
{:body (vim.fn.json_decode response.body)}) | ||
response))) | ||
|
||
(λ get [api-key endpoint] | ||
(request curl.get api-key endpoint {})) | ||
(λ request-async [method endpoint options on-success ?on-error] | ||
(: (job:new {:command :curl | ||
:args [:-X | ||
method | ||
:-H | ||
"Content-Type: application/json" | ||
:-H | ||
"Accept: application/vnd.forem.api-v1+json" | ||
:-H | ||
(.. "api-key: " (key)) | ||
:-d | ||
(vim.fn.json_encode options) | ||
(.. base-url endpoint)] | ||
:on_exit (fn [this code] | ||
(vim.schedule #(let [results (this:result) | ||
result (vim.fn.join results "\n") | ||
response (vim.fn.json_decode result)] | ||
(if (= code 0) | ||
(on-success response) | ||
(do | ||
(handle-async-error response) | ||
(when ?on-error | ||
(?on-error response)))))))}) | ||
:start)) | ||
|
||
(λ put [api-key endpoint body] | ||
(request curl.put api-key endpoint {: body})) | ||
(λ get [endpoint on-sucess ?on-error] | ||
(request-async :GET endpoint {} on-sucess ?on-error)) | ||
|
||
(λ post [api-key endpoint body] | ||
(request curl.post api-key endpoint {: body})) | ||
(λ put [endpoint body] | ||
(request curl.put endpoint {: body})) | ||
|
||
(λ M.my-articles [api-key] | ||
(get api-key :/articles/me/all)) | ||
(λ post [endpoint body] | ||
(request curl.post endpoint {: body})) | ||
|
||
(λ M.save-article [api-key id content] | ||
(put api-key (.. :/articles/ id) | ||
(λ M.my-articles [on-success ?on-error] | ||
(get :/articles/me/all on-success ?on-error)) | ||
|
||
(λ M.save-article [id content] | ||
(put (.. :/articles/ id) | ||
(vim.fn.json_encode {:article {:body_markdown content}}))) | ||
|
||
(λ M.new-article [api-key title] | ||
(post api-key :/articles | ||
(λ M.new-article [title] | ||
(post :/articles | ||
(vim.fn.json_encode {:article {:body_markdown (get-article-template title)}}))) | ||
|
||
(λ M.feed [api-key] | ||
(get api-key :/articles)) | ||
(λ M.feed [on-success ?on-error] | ||
(get :/articles on-success ?on-error)) | ||
|
||
(λ M.get-article [id on-success ?on-error] | ||
(get (.. :/articles/ id) on-success ?on-error)) | ||
|
||
(λ M.get-article [api-key id] | ||
(get api-key (.. :/articles/ id))) | ||
(λ M.get-article-by-path [path on-success ?on-error] | ||
(get (.. :/articles/ path) on-success ?on-error)) | ||
|
||
(λ M.get-article-by-path [api-key path] | ||
(get api-key (.. :/articles/ path))) | ||
(λ M.handle-api-error [response on-success] | ||
(let [start-status (-> response.status (tostring) (string.sub 1 2))] | ||
(if (not= start-status :20) | ||
(notify.error (.. "Error: " response.body.error)) | ||
(on-success response)))) | ||
|
||
M |
This file contains 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.