Skip to content

Commit 92b3a6c

Browse files
author
Miqueas Martinez
committed
Writed fetch method, removed std/uri and added basic commandline support
1 parent cfb0e72 commit 92b3a6c

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

Github.nim

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# TODO: write the example
22
import std/httpclient
33
import std/json
4-
import std/uri
4+
# import std/parseopt
5+
from std/os import commandLineParams
56

6-
const BASE_URL = parseUri("https://api.github.com/users")
7+
const BASE_URL = "https://api.github.com/users/"
78

89
type
910
GthUserItem* = ref object
@@ -13,7 +14,7 @@ type
1314
GthUser* = ref object
1415
# Private
1516
obj: JsonNode
16-
url: Uri
17+
url: string
1718
# Public
1819
name*: string
1920
bio*: string
@@ -25,9 +26,9 @@ type
2526

2627
proc newGthUser*(username: string): GthUser =
2728
var self = GthUser()
28-
self.url = parseUri($(BASE_URL / username))
29+
self.url = BASE_URL & username
2930

30-
var res = newHttpClient().getContent($self.url)
31+
var res = newHttpClient().getContent(self.url)
3132
self.obj = parseJson(res)
3233

3334
self.name = self.obj["name"].getStr()
@@ -41,5 +42,37 @@ proc newGthUser*(username: string): GthUser =
4142

4243
return self
4344

44-
var user = newGthUser("Miqueas")
45-
echo user[]
45+
method fetch*(self: GthUser, thing: string): void {.base.} =
46+
var
47+
url = self.url & '/' & thing
48+
res = newHttpClient().getContent(url)
49+
arr = parseJson(res)
50+
51+
case thing
52+
of "repos":
53+
for v in arr.items():
54+
self.repos.arr.add(v["name"].getStr())
55+
of "gists":
56+
for v in arr.items():
57+
self.gists.arr.add(v["description"].getStr())
58+
of "followers":
59+
for v in arr.items():
60+
self.followers.arr.add(v["login"].getStr())
61+
of "following":
62+
for v in arr.items():
63+
self.following.arr.add(v["login"].getStr())
64+
else:
65+
echo "Unsupported endpoint: ", thing
66+
67+
let
68+
args = commandLineParams()
69+
# TODO: handle commandline switches
70+
# opts = initOptParser(args)
71+
72+
if args.len() > 0:
73+
for username in args:
74+
var user = newGthUser(username)
75+
echo user.name
76+
echo user.bio
77+
echo user.link
78+
echo ""

0 commit comments

Comments
 (0)