Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b1a7b1bc2 | |||
| b1d38164f8 | |||
| f0f3baaa3d | |||
| 4cec5b55c7 | |||
| dba520dce2 | |||
| c164c4f696 | |||
| c3d4b544ad |
@@ -2,7 +2,7 @@
|
|||||||
(:require [clojure.tools.build.api :as b]))
|
(:require [clojure.tools.build.api :as b]))
|
||||||
|
|
||||||
(def lib 'rcorral/utils)
|
(def lib 'rcorral/utils)
|
||||||
(def version "0.1.0") ;; or read from file, etc
|
(def version "0.2.0") ;; or read from file, etc
|
||||||
(def class-dir "target/classes")
|
(def class-dir "target/classes")
|
||||||
(def jar-file (format "target/%s-%s.jar" (name lib) version))
|
(def jar-file (format "target/%s-%s.jar" (name lib) version))
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,9 @@
|
|||||||
(defn get-and-save-key-from-env
|
(defn get-and-save-key-from-env
|
||||||
"Get api key from environment and store in the KEYS map. It key is not found in
|
"Get api key from environment and store in the KEYS map. It key is not found in
|
||||||
the environment, it tries to load the development key as a fallback. If none api
|
the environment, it tries to load the development key as a fallback. If none api
|
||||||
key is found, return nil"
|
key is found, return nil.
|
||||||
|
|
||||||
|
KEYS is a map of atom values, that stores current loaded keys."
|
||||||
[KEYS default-key key]
|
[KEYS default-key key]
|
||||||
(if-let [value (get-key-from-env key)]
|
(if-let [value (get-key-from-env key)]
|
||||||
(reset! (key KEYS) value)
|
(reset! (key KEYS) value)
|
||||||
@@ -40,7 +42,7 @@
|
|||||||
(if-let [def-key-value (deref (default-key KEYS))]
|
(if-let [def-key-value (deref (default-key KEYS))]
|
||||||
def-key-value
|
def-key-value
|
||||||
(get-and-save-key-from-env KEYS default-key keytype)))
|
(get-and-save-key-from-env KEYS default-key keytype)))
|
||||||
(println "Invalid key type. Available keys:" (keys KEYS))))
|
(println "Invalid key type '" keytype "'. Available keys:" (keys KEYS))))
|
||||||
|
|
||||||
|
|
||||||
(defn set-api-key
|
(defn set-api-key
|
||||||
@@ -172,7 +174,6 @@
|
|||||||
debug false
|
debug false
|
||||||
debug-http @debug-http}
|
debug-http @debug-http}
|
||||||
:as cfg-params}]
|
:as cfg-params}]
|
||||||
(println "Params" cfg-params)
|
|
||||||
(when debug (println "Endpoints:" (count endpoints-cfg) "Key provider:" key-provider))
|
(when debug (println "Endpoints:" (count endpoints-cfg) "Key provider:" key-provider))
|
||||||
(let [; Process path-parameters
|
(let [; Process path-parameters
|
||||||
processed-url (if (keyword? url)
|
processed-url (if (keyword? url)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
"Example about how to build an API client using rcorral.api.core"
|
"Example about how to build an API client using rcorral.api.core"
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Endpoints configuration
|
;; Endpoints configuration
|
||||||
;;
|
;;
|
||||||
@@ -16,7 +15,7 @@
|
|||||||
|
|
||||||
(defn auth-provider
|
(defn auth-provider
|
||||||
"Auth provider that gets user and password and adds a header for basic auth"
|
"Auth provider that gets user and password and adds a header for basic auth"
|
||||||
[user password url params headers]
|
[user password _ params headers]
|
||||||
(if (and (some? user) (some? password))
|
(if (and (some? user) (some? password))
|
||||||
(let [concatenated (str user ":" password)
|
(let [concatenated (str user ":" password)
|
||||||
encoded (.encodeToString (java.util.Base64/getEncoder) (.getBytes concatenated))
|
encoded (.encodeToString (java.util.Base64/getEncoder) (.getBytes concatenated))
|
||||||
@@ -25,17 +24,10 @@
|
|||||||
[params headers]))
|
[params headers]))
|
||||||
|
|
||||||
(defn get-endpoint
|
(defn get-endpoint
|
||||||
[url user passwd & {:keys [path-params query-params header-params verbose]
|
[url user passwd & {:as params}]
|
||||||
:or {path-params {}
|
|
||||||
query-params {}
|
|
||||||
header-params {"Accept" "application/json"}
|
|
||||||
debug false
|
|
||||||
debug-http false}
|
|
||||||
:as params}]
|
|
||||||
(println "Calling with" params)
|
(println "Calling with" params)
|
||||||
(api/call-api ENDPOINTS (partial auth-provider user passwd) url params))
|
(api/call-api ENDPOINTS (partial auth-provider user passwd) url params))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(get-endpoint :licenses nil nil :debug true)
|
(get-endpoint :licenses nil nil :debug true)
|
||||||
(get-endpoint :user-info "user" "passwd" :debug true)
|
(get-endpoint :user-info "user" "passwd" :debug true))
|
||||||
)
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@
|
|||||||
([x]
|
([x]
|
||||||
(to-fancy-hex x 2))
|
(to-fancy-hex x 2))
|
||||||
([x group-size]
|
([x group-size]
|
||||||
(try (when (s/join " "
|
(try (when x (s/join " "
|
||||||
(map s/join
|
(map s/join
|
||||||
(partition group-size (hex/encode (byte-array (map #(b/to-byte %) x)))))))
|
(partition group-size (hex/encode (byte-array (map #(b/to-byte %) x)))))))
|
||||||
(catch Exception e))))
|
(catch Exception e))))
|
||||||
@@ -99,12 +99,13 @@
|
|||||||
More info: https://en.wikipedia.org/wiki/Euclidean_division
|
More info: https://en.wikipedia.org/wiki/Euclidean_division
|
||||||
"
|
"
|
||||||
[a b]
|
[a b]
|
||||||
(when (zero? b)
|
(when (and a b)
|
||||||
(throw (IllegalArgumentException. "You can't divide by zero!")))
|
(when (zero? b)
|
||||||
(let [b-abs (Math/abs b)
|
(throw (IllegalArgumentException. "You can't divide by zero!")))
|
||||||
r (mod a b-abs)] ;; 0 <= r < |b|
|
(let [b-abs (Math/abs b)
|
||||||
(quot (- a r) b) ;; adjusts quotient with the positive remainder
|
r (mod a b-abs)] ;; 0 <= r < |b|
|
||||||
))
|
(quot (- a r) b) ;; adjusts quotient with the positive remainder
|
||||||
|
)))
|
||||||
|
|
||||||
(defn euclidean-rem
|
(defn euclidean-rem
|
||||||
"Modulus for euclidean division for integers
|
"Modulus for euclidean division for integers
|
||||||
|
|||||||
@@ -83,6 +83,11 @@
|
|||||||
(is (= "0F" (to-fancy-hex [15])))
|
(is (= "0F" (to-fancy-hex [15])))
|
||||||
(is (= "0F 7F" (to-fancy-hex [15 127])))))
|
(is (= "0F 7F" (to-fancy-hex [15 127])))))
|
||||||
|
|
||||||
|
(deftest euclidean-quot-test
|
||||||
|
(testing "Invalid values"
|
||||||
|
(is (nil? (euclidean-quot nil nil)))
|
||||||
|
(is (= "" ""))))
|
||||||
|
|
||||||
(deftest decimal->base-test
|
(deftest decimal->base-test
|
||||||
(testing "Convert from decimal base to an arbitrary base"
|
(testing "Convert from decimal base to an arbitrary base"
|
||||||
(is (= [0] (decimal->base nil 10)))
|
(is (= [0] (decimal->base nil 10)))
|
||||||
|
|||||||
14
test/rcorral/util_test.clj
Normal file
14
test/rcorral/util_test.clj
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
(ns rcorral.util-test
|
||||||
|
(:require [clojure.test :refer :all]
|
||||||
|
[rcorral.bin-util :refer :all]
|
||||||
|
[alphabase.bytes :as b]
|
||||||
|
[alphabase.base16 :as hex]
|
||||||
|
[alphabase.base64 :as b64]
|
||||||
|
[alphabase.base32 :as b32])
|
||||||
|
(:import (java.util Arrays)))
|
||||||
|
|
||||||
|
(deftest success-test
|
||||||
|
(testing "Generate a successful case"
|
||||||
|
(is true) ;; Not nill
|
||||||
|
))
|
||||||
|
|
||||||
Reference in New Issue
Block a user