aboutsummaryrefslogtreecommitdiff
path: root/anime/util.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@protonmail.ch>2022-01-31 11:25:04 +0530
committerVidhu Kant Sharma <vidhukant@protonmail.ch>2022-01-31 11:25:04 +0530
commit0dd65dd062362f913b3028e07e2f0c3afaec8894 (patch)
treef94a35aa5b19886bd39f8dec0acfe34e18d723e4 /anime/util.go
parenta835f9b0b8b714a76d8b2f9c49b84f7042ddbd6a (diff)
Added a URL Generator
Diffstat (limited to 'anime/util.go')
-rw-r--r--anime/util.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/anime/util.go b/anime/util.go
index 319bbc9..f09f189 100644
--- a/anime/util.go
+++ b/anime/util.go
@@ -4,8 +4,11 @@ import (
"io/ioutil"
"log"
"net/http"
+ "errors"
)
+// Handles HTTP request with your OAuth token as a Header
+// TODO: Verify that this function is safe to use
func requestHandler(token string, endpoint string) string {
client := &http.Client{}
@@ -31,3 +34,40 @@ func requestHandler(token string, endpoint string) string {
return string(body)
}
+
+func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) {
+ // TODO: error if cap(names) != cap(values)
+ if cap(names) != cap(values) {
+ return "", errors.New("urlGenerator: Error: Length of names and values don't match.")
+ }
+
+ var fields string
+
+ for index, name := range(names) {
+ var data string
+ /* if the data is the first field in URL,
+ * it goes like ?key=value
+ * else it is &nextkey=value */
+ if isPrimary {
+ data = "?" + name + "="
+ } else {
+ data = "&" + name + "="
+ }
+
+ // add values to data variable
+ for i, j := range values[index] {
+ if i > 0 {
+ data = data + "," + j
+ } else {
+ data = data + j
+ }
+ }
+
+ fields = fields + data
+
+ // from now on all other fields will be secondary
+ isPrimary = false
+ }
+
+ return baseUrl + fields, nil
+}