Skip to content

Commit

Permalink
Cleaner interface and code, more query params
Browse files Browse the repository at this point in the history
Added all possible query parameters that api supports. Added a cleaner and safer url query parameter factory.
  • Loading branch information
NANI committed Jan 31, 2020
1 parent f8db534 commit 1cfc624
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 107 deletions.
81 changes: 60 additions & 21 deletions endpoints.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,87 @@
package ksoftgo

import (
"fmt"
"github.com/google/go-querystring/query"
"net/url"
"strconv"
)

var (
EndpointRest = "https://api.ksoft.si/"

// --------- IMAGES ENDPOINTS ----------------------------------------------

EndpointMemeRandomMeme = EndpointRest + "images/random-meme"
EndpointMemeRandomImage = func(tag string, nsfw bool) string {
return EndpointRest + "images/random-image?tag=" + tag + "&nsfw=" + strconv.FormatBool(nsfw)
EndpointMemeTags = EndpointRest + "images/tags"
EndpointMemeRandomAww = EndpointRest + "images/random-aww"
EndpointMemeImage = func(snowflake string) string { return EndpointRest + "images/image/" + snowflake }
EndpointMemeRandomImage = func(param ParamRandomImage) string {
q, _ := query.Values(param)
return EndpointRest + "images/random-image?" + q.Encode()
}
EndpointMemeWikihow = func(param ParamWikiHow) string {
q, _ := query.Values(param)
return EndpointRest + "images/random-wikihow?" + q.Encode()
}
EndpointMemeRandomReddit = func(param ParamRandomReddit) string {
q, _ := query.Values(param.Options)
return EndpointRest + "images/rand-reddit/" + param.SubReddit + "?" + q.Encode()
}
EndpointMemeImage = func(snowflake string) string { return EndpointRest + "images/image/" + snowflake }
EndpointMemeWikihow = EndpointRest + "images/random-wikihow"
EndpointMemeTags = EndpointRest + "images/tags"
EndpointMemeRandomAww = EndpointRest + "images/random-aww"
EndpointMemeRandomReddit = func(sub string) string { return EndpointRest + "images/rand-reddit/" + sub }
EndpointMemeRandomNSFW = EndpointRest + "images/random-nsfw"
EndpointMemeRandomNSFW = func(param ParamRandomNSFW) string {
q, _ := query.Values(param)
return EndpointRest + "images/random-nsfw?" + q.Encode()
}

// --------- BANS ENDPOINTS ------------------------------------------------

EndpointBansAdd = EndpointRest + "bans/add"
EndpointBansInfo = func(id int64) string { return EndpointRest + "bans/info?user=" + strconv.FormatInt(id, 10) }
EndpointBansCheck = func(id int64) string { return EndpointRest + "bans/check?user=" + strconv.FormatInt(id, 10) }
EndpointBansDelete = func(user int64, force bool) string {
return EndpointRest + "bans/delete?user=" + string(user) + "&force=" + strconv.FormatBool(force)
EndpointBansAdd = EndpointRest + "bans/add"
EndpointBansInfo = func(param ParamBans) string {
q, _ := query.Values(param)
return EndpointRest + "bans/info?" + q.Encode()
}
EndpointBansCheck = func(param ParamBans) string {
q, _ := query.Values(param)
return EndpointRest + "bans/check?" + q.Encode()
}
EndpointBansList = func(page, perpage int) string {
return EndpointRest + "bans/list?page=" + strconv.Itoa(page) + "&per_page=" + strconv.Itoa(perpage)
EndpointBansDelete = func(param ParamDeleteBan) string {
q, _ := query.Values(param)
return EndpointRest + "bans/delete?" + q.Encode()
}
EndpointBansList = func(param ParamListBans) string {
q, _ := query.Values(param)
return EndpointRest + "bans/list?" + q.Encode()
}

// --------- KUMO ENDPOINTS ------------------------------------------------

EndpointKumoGis = func(param ParamGIS) string { return EndpointRest + "kumo/gis?q=" + url.QueryEscape(param.Location) }
EndpointKumoGis = func(param ParamGIS) string {
return EndpointRest + "kumo/gis?q=" + url.QueryEscape(param.Location)
}
EndpointKumoWeather = func(param ParamWeather) string {
return EndpointRest + "kumo/weather/" + param.ReportType + "?q=" + url.QueryEscape(param.Location)
}
EndpointKumoWeatherAdv = func(param ParamAdvWeather) string {
return EndpointRest + "kumo/weather/" + strconv.FormatFloat(param.Latitude, 'f', -1, 64) + "," + strconv.FormatFloat(param.Longitude, 'f', -1, 64) + "/" + param.ReportType
q, _ := query.Values(param.Options)
return EndpointRest + fmt.Sprintf("kumo/weather/%s,%s/%s?%s",
strconv.FormatFloat(param.Latitude, 'f', -1, 64),
strconv.FormatFloat(param.Longitude, 'f', -1, 64),
param.ReportType, q.Encode())
}
EndpointKumoGeoip = func(ip string) string { return EndpointRest + "kumo/geoip?ip=" + ip }
EndpointKumoCurrency = func(value float64, from, to string) string {
return EndpointRest + "kumo/currency?from=" + from + "&to=" + to + "&value=" + strconv.FormatFloat(value, 'f', -1, 64)
EndpointKumoGeoIP = func(param ParamIP) string {
q, _ := query.Values(param)
return EndpointRest + "kumo/geoip?" + q.Encode()
}
EndpointKumoCurrency = func(param ParamCurrency) string {
q, _ := query.Values(param)
return EndpointRest + "kumo/currency?" + q.Encode()
}

// --------- MUSIC ENDPOINTS -----------------------------------------------

EndpointLyricsSearch = func(param ParamSearchLyrics) string {
return EndpointRest + "lyrics/search?q=" + url.QueryEscape(param.Query)
q, _ := query.Values(param)
return EndpointRest + "lyrics/search?" + q.Encode()
}
EndpointLyricsArtist = func(id int64) string { return EndpointRest + "lyrics/artist/" + strconv.FormatInt(id, 10) }
EndpointLyricsAlbum = func(id int64) string { return EndpointRest + "lyrics/album/" + strconv.FormatInt(id, 10) }
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/KSoft-Si/KSoftgo

go 1.13

require github.com/google/go-querystring v1.0.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
129 changes: 62 additions & 67 deletions ksoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"log"
"net/http"
"runtime"
"strconv"
"strings"
"time"
)

// Following https://semver.org/
const VERSION string = "1.1.4"
const VERSION string = "2.0.0"

// New creates a new KSoft instance.
func New(token string) (s *KSession, err error) {
Expand All @@ -32,10 +31,10 @@ func New(token string) (s *KSession, err error) {

// Get a random image
// Example:
// image, err := ksession.RandomImage(kosftgo.ParamTag{Name: "doge"})
func (s *KSession) RandomImage(tag ParamTag) (i Image, err error) {
// image, err := ksession.RandomImage(kosftgo.ParamRandomImage{Tag: "doge"})
func (s *KSession) RandomImage(tag ParamRandomImage) (i Image, err error) {
i = Image{}
res, err := s.request("GET", EndpointMemeRandomImage(tag.Name, tag.NSFW), nil)
res, err := s.request("GET", EndpointMemeRandomImage(tag), nil)
if err != nil {
return
}
Expand All @@ -58,9 +57,9 @@ func (s *KSession) RandomMeme() (r Reddit, err error) {
return
}

// Get a random meme
// Get a random picture that makes you say awwwww
// Example:
// reddit, err := ksession.RandomMeme()
// reddit, err := ksession.RandomAww()
func (s *KSession) RandomAww() (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomAww, nil)
Expand All @@ -74,10 +73,10 @@ func (s *KSession) RandomAww() (reddit Reddit, err error) {

// Get a random reddit post
// Example:
// reddit, err := ksession.RandomReddit("memes")
func (s *KSession) RandomReddit(sub string) (reddit Reddit, err error) {
// reddit, err := ksession.RandomReddit(ksoftgo.ParamRandomReddit{SubReddit: "memes", Options: ksoftgo.OptionalRandomReddit{Span: "month"}})
func (s *KSession) RandomReddit(param ParamRandomReddit) (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomReddit(sub), nil)
res, err := s.request("GET", EndpointMemeRandomReddit(param), nil)
if err != nil {
return
}
Expand All @@ -91,7 +90,21 @@ func (s *KSession) RandomReddit(sub string) (reddit Reddit, err error) {
// reddit, err := ksession.RandomNSFW()
func (s *KSession) RandomNSFW() (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomNSFW, nil)
res, err := s.request("GET", EndpointMemeRandomNSFW(ParamRandomNSFW{}), nil)
if err != nil {
return
}

err = json.Unmarshal(res, &reddit)
return
}

// Get a random NSFW post with options
// Example:
// reddit, err := ksession.RandomNSFW(ksoftgo.ParamRandomNSFW{GIFsOnly: true})
func (s *KSession) RandomNSFWOptions(options ParamRandomNSFW) (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomNSFW(options), nil)
if err != nil {
return
}
Expand All @@ -105,7 +118,21 @@ func (s *KSession) RandomNSFW() (reddit Reddit, err error) {
// image, err := ksession.RandomWikiHow()
func (s *KSession) RandomWikiHow() (i WikiHowImage, err error) {
i = WikiHowImage{}
res, err := s.request("GET", EndpointMemeWikihow, nil)
res, err := s.request("GET", EndpointMemeWikihow(ParamWikiHow{}), nil)
if err != nil {
return
}

err = json.Unmarshal(res, &i)
return
}

// Get a random WikiHow article with options
// Example:
// image, err := ksession.RandomWikiHow(ksoftgo.ParamWikiHow{NSFW: true})
func (s *KSession) RandomWikiHowOptions(options ParamWikiHow) (i WikiHowImage, err error) {
i = WikiHowImage{}
res, err := s.request("GET", EndpointMemeWikihow(options), nil)
if err != nil {
return
}
Expand Down Expand Up @@ -160,10 +187,10 @@ func (s *KSession) AddBan(info ParamAddBan) (err error) {

// Get ban info
// Example:
// baninfo, err := ksession.GetBanInfo(0000000000000000)
func (s *KSession) GetBanInfo(user int64) (info BanInfo, err error) {
// baninfo, err := ksession.GetBanInfo(ksoftgo.ParamBans{UserID: "123456789123456789"})
func (s *KSession) GetBanInfo(param ParamBans) (info BanInfo, err error) {
info = BanInfo{}
res, err := s.request("GET", EndpointBansInfo(user), nil)
res, err := s.request("GET", EndpointBansInfo(param), nil)
if err != nil {
return
}
Expand All @@ -174,9 +201,9 @@ func (s *KSession) GetBanInfo(user int64) (info BanInfo, err error) {

// Check user
// Example:
// isbanned, err := ksession.CheckBan(0000000000000000)
func (s *KSession) CheckBan(user int64) (c bool, err error) {
res, err := s.request("GET", EndpointBansCheck(user), nil)
// isbanned, err := ksession.CheckBan(ksoftgo.ParamBans{UserID: "123456789123456789"})
func (s *KSession) CheckBan(param ParamBans) (c bool, err error) {
res, err := s.request("GET", EndpointBansCheck(param), nil)
if err != nil {
return
}
Expand All @@ -188,20 +215,20 @@ func (s *KSession) CheckBan(user int64) (c bool, err error) {

// Delete ban
// Example:
// ksession.DeleteBan(0000000000000000)
// ksession.DeleteBan(ksoftgo.ParamDeleteBan{User: "123456789123456789", Force: false})
func (s *KSession) DeleteBan(delete ParamDeleteBan) {
_, err := s.request("DELETE", EndpointBansDelete(delete.User, delete.Force), nil)
_, err := s.request("DELETE", EndpointBansDelete(delete), nil)
if err != nil {
return
}
}

// List of bans
// Example:
// banlist, err := ksession.GetBans(1, 20)
func (s *KSession) GetBans(page, perpage int) (banlist BansList, err error) {
// banlist, err := ksession.GetBans(ksoftgo.ParamListBans{Page: 1})
func (s *KSession) GetBans(param ParamListBans) (banlist BansList, err error) {
banlist = BansList{}
res, err := s.request("GET", EndpointBansList(page, perpage), nil)
res, err := s.request("GET", EndpointBansList(param), nil)
if err != nil {
return
}
Expand All @@ -212,21 +239,11 @@ func (s *KSession) GetBans(page, perpage int) (banlist BansList, err error) {

// Search for locations and get maps
// Example:
// gis, err := ksession.GetGis(ksoftgo.ParamGis{Location: "Montreal"})
// gis, err := ksession.GetGis(ksoftgo.ParamGIS{Location: "Montreal"})
func (s *KSession) GetGIS(params ParamGIS) (gis GIS, err error) {
gis = GIS{}
url := EndpointKumoGis(params)
if params.Fast {
url += "&fast=" + strconv.FormatBool(params.Fast)
} else if params.IncludeMap {
url += "&include_map=" + strconv.FormatBool(params.IncludeMap)
} else if params.More {
url += "&more=" + strconv.FormatBool(params.More)
} else if params.Zoom != 0 {
url += "&map_zoom=" + string(params.Zoom)
}

res, err := s.request("GET", url, nil)
res, err := s.request("GET", EndpointKumoGis(params), nil)
if err != nil {
return
}
Expand All @@ -240,16 +257,8 @@ func (s *KSession) GetGIS(params ParamGIS) (gis GIS, err error) {
// weather, err := ksession.GetWeather(ksoftgo.ParamWeather{Location: "Montreal", ReportType: "currently"})
func (s *KSession) GetWeather(params ParamWeather) (weather Weather, err error) {
weather = Weather{}
url := EndpointKumoWeather(params)
if params.Units != "" {
url += "&units=" + params.Units
} else if params.Lang != "" {
url += "&lang=" + params.Lang
} else if params.Icons != "" {
url += "&icons=" + params.Icons
}

res, err := s.request("GET", url, nil)
res, err := s.request("GET", EndpointKumoWeather(params), nil)
if err != nil {
return
}
Expand All @@ -260,19 +269,11 @@ func (s *KSession) GetWeather(params ParamWeather) (weather Weather, err error)

// Weather - advanced
// Example:
// weather, err := ksession.GetAdvWeather(ksoftgo.ParamAdvWeather{Latitude: 0.0, Longitude: 0.0,ReportType: "currently"})
// weather, err := ksession.GetAdvWeather(ksoftgo.ParamAdvWeather{Latitude: 0.0, Longitude: 0.0, ReportType: "currently"})
func (s *KSession) GetAdvWeather(params ParamAdvWeather) (weather Weather, err error) {
weather = Weather{}
url := EndpointKumoWeatherAdv(params)
if params.Units != "" {
url += "&units=" + params.Units
} else if params.Lang != "" {
url += "&lang=" + params.Lang
} else if params.Icons != "" {
url += "&icons=" + params.Icons
}

res, err := s.request("GET", url, nil)
res, err := s.request("GET", EndpointKumoWeatherAdv(params), nil)
if err != nil {
return
}
Expand All @@ -283,10 +284,10 @@ func (s *KSession) GetAdvWeather(params ParamAdvWeather) (weather Weather, err e

// GeoIP
// Example:
// geoip, err := ksession.GeoIP("192.168.0.1")
func (s *KSession) GeoIP(ip string) (geoip GeoIP, err error) {
// geoip, err := ksession.GeoIP(ksoftgo.ParamIP{IP: "8.8.8.8"})
func (s *KSession) GeoIP(param ParamIP) (geoip GeoIP, err error) {
geoip = GeoIP{}
res, err := s.request("GET", EndpointKumoGeoip(ip), nil)
res, err := s.request("GET", EndpointKumoGeoIP(param), nil)
if err != nil {
return
}
Expand All @@ -297,10 +298,10 @@ func (s *KSession) GeoIP(ip string) (geoip GeoIP, err error) {

// Currency conversion
// Example:
// currency, err := ksession.CurrenyConversion(694.20, "USD", "CAD")
func (s *KSession) CurrencyConversion(value float64, from, to string) (curr Currency, err error) {
// currency, err := ksession.CurrenyConversion(ksoftgo.ParamCurrency{FromCurrency: "USD", ToCurrency: "EUR", Value: 1.50})
func (s *KSession) CurrencyConversion(param ParamCurrency) (curr Currency, err error) {
curr = Currency{}
res, err := s.request("GET", EndpointKumoCurrency(value, from, to), nil)
res, err := s.request("GET", EndpointKumoCurrency(param), nil)
if err != nil {
return
}
Expand All @@ -314,14 +315,8 @@ func (s *KSession) CurrencyConversion(value float64, from, to string) (curr Curr
// lyricssearch, err := ksession.SearchLyrics(ksoftgo.ParamSearchLyrics{Query: "Rick never gonna give you up"})
func (s *KSession) SearchLyrics(param ParamSearchLyrics) (results LyricsSearch, err error) {
results = LyricsSearch{}
url := EndpointLyricsSearch(param)
if param.Limit != 0 {
url += "&limit=" + string(param.Limit)
} else if param.TextOnly {
url += "&text_only=" + strconv.FormatBool(param.TextOnly)
}

res, err := s.request("GET", url, nil)
res, err := s.request("GET", EndpointLyricsSearch(param), nil)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 1cfc624

Please sign in to comment.