Skip to content

Commit 47cd1bf

Browse files
committed
Merge branch 'feature/refactoring' into develop
* feature/refactoring: return nil on search result empty realised token is not needed remove get-gfycat-with
2 parents d232b88 + 29b4f00 commit 47cd1bf

File tree

4 files changed

+31
-83
lines changed

4 files changed

+31
-83
lines changed

src/gfycat_api/core.clj

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
(ns gfycat-api.core
22
(:require [clojure.data.json :as json]
33
[clj-http.client :as client]
4-
[gfycat-api.util :as util]
5-
)
4+
[gfycat-api.util :as util])
65
(:gen-class))
76

8-
97
(defn- gfycat-request [path query-params]
108
(-> (client/get (str "https://api.gfycat.com/v1/" path)
119
{:content-type :json
@@ -15,15 +13,18 @@
1513
(json/read-str :key-fn util/clojure-stylify)))
1614

1715
(defn get-token [client-id client-secret]
18-
(try
19-
(gfycat-request "oauth/token"
20-
{"grant_type" "client_credentials"
21-
"client_id" client-id
22-
"client_secret" client-secret})
23-
(catch Exception e (prn (ex-data e) ))))
16+
(if (and client-id client-secret)
17+
(try
18+
(gfycat-request "oauth/token"
19+
{"grant_type" "client_credentials"
20+
"client_id" client-id
21+
"client_secret" client-secret})
22+
(catch Exception e (prn (ex-data e))))))
2423

2524
(defn search
26-
([token query & [count cursor]]
27-
(gfycat-request "gfycats/search" {"search_text" query
28-
"count" count
29-
"cursor" cursor})))
25+
"doesnt require a token"
26+
([query & [count cursor token]]
27+
(let [{found :found :as search-result} (gfycat-request "gfycats/search" {"search_text" query
28+
"count" count
29+
"cursor" cursor})]
30+
(if (> found 0) search-result))))

src/gfycat_api/util.clj

-54
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,3 @@
22

33
(defn clojure-stylify [key]
44
(keyword (clojure.string/replace key #"_" "-")))
5-
6-
(defn get-gfycats-with [keys search-result]
7-
"returns list of gfycats with selected parameters,
8-
removes all other parameters
9-
possible params:
10-
:title
11-
gifs urls:
12-
:gif100px
13-
:max2mbgif
14-
:gifurl
15-
:mp4url
16-
:webmurl
17-
:max1mbgif
18-
other:
19-
:framerate
20-
:gfynumber
21-
:width
22-
:gatekeeper
23-
:mobileposterurl
24-
:source
25-
:likes
26-
:curated
27-
:createdate
28-
:languagetext2
29-
:nsfw
30-
:extralemmas
31-
:hasaudio
32-
:max5mbgif
33-
:languagecategories
34-
:mp4size
35-
:url
36-
:mobileurl
37-
:gfyid
38-
:gfyname
39-
:posterurl
40-
:hastransparency
41-
:webpurl
42-
:username
43-
:published
44-
:rating
45-
:height
46-
:views
47-
:description
48-
:tags
49-
:webmsize
50-
:gfyslug
51-
:miniurl
52-
:numframes
53-
:dislikes
54-
:avgcolor
55-
:miniposterurl
56-
:thumb100posterurl
57-
:domainwhitelist"
58-
(map #(select-keys % keys) (:gfycats search-result)))

test/gfycat_api/core_test.clj

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
(ns gfycat-api.core-test
22
(:require [clojure.test :refer [deftest is testing]]
3-
[clojure.data.json :as json]
3+
[clojure.data.json :as json]
44
[gfycat-api.core :as core]
5-
[gfycat-api.util :as util]
6-
))
5+
[gfycat-api.util :as util]))
76

87
(def client-info (json/read-str (slurp ".client_info.json")
98
:key-fn util/clojure-stylify))
109

1110
(deftest get-token-test
12-
(let [token (core/get-token (:client-id client-info) (:client-secret client-info))]
13-
(is (not (nil? token)))
14-
(is (= (:expires-in token) 3600))))
11+
(let [token (core/get-token (:client-id client-info) (:client-secret client-info))]
12+
(is (not (nil? token)))
13+
(is (= (:expires-in token) 3600)))
14+
(testing "should return nil if no client id or client-secret"
15+
(is (= nil (core/get-token nil nil)))))
1516

1617
(def token (:access-token (core/get-token (:client-id client-info) (:client-secret client-info))))
1718
(deftest search-test
18-
(testing "should return one gfycat search result"
19-
(let [search-result (core/search token "thanos power up" 1)]
20-
(is (= (:cursor search-result) ""))))
21-
(testing "searchnig with cursor"))
19+
(testing "should return one gfycat search result"
20+
(let [search-result (core/search "thanos power up" 1)]
21+
(is (= (:cursor search-result) ""))))
22+
#_(testing "search with token"
23+
(let [search-result (core/search nil "thanos power up" 1)]
24+
(is (= (count (:gfycats search-result)) 1))))
25+
(testing "should return nil on empty search"
26+
(let [search-result (core/search "" 1)]
27+
(is (= search-result nil))))
28+
(testing "searchnig with cursor"))

test/gfycat_api/util_test.clj

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@
33
[clojure.test :refer [deftest is testing]]
44
[gfycat-api.fixtures :as fixtures]
55
[gfycat-api.util :as util]))
6-
7-
(deftest search-result-filtering
8-
(is (= (util/get-gfycats-with [:title :gfyId] fixtures/single-gfycat-search-result)
9-
'({:title
10-
"Canadian Flag waving animated using MIR plug in after effects - free motion graphics"
11-
:gfyId "fondfocusedclownanemonefish"}))))

0 commit comments

Comments
 (0)