2022-05-31 22:47:41 +08:00
|
|
|
let s:HTTP = SpaceVim#api#import('web#http')
|
|
|
|
|
|
|
|
|
2022-04-28 21:46:05 +08:00
|
|
|
""
|
|
|
|
" @public
|
|
|
|
" Get all users
|
|
|
|
"
|
|
|
|
" Github API : GET /users
|
|
|
|
function! github#api#users#GetAllUsers() abort
|
2022-05-29 20:59:18 +08:00
|
|
|
return github#api#util#Get('users', [])
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2022-05-29 21:06:44 +08:00
|
|
|
function! github#api#users#starred(user, page) abort
|
2022-05-31 22:47:41 +08:00
|
|
|
let result = s:HTTP.get('https://api.github.com/users/' .
|
2022-05-29 21:06:44 +08:00
|
|
|
\ a:user . '/starred' . '?page=' . a:page)
|
2022-05-31 22:47:41 +08:00
|
|
|
if result.status ==# 200
|
|
|
|
return json_decode(result.content)
|
2022-05-29 21:06:44 +08:00
|
|
|
endif
|
|
|
|
" if the command run failed, return empty list
|
|
|
|
return []
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! github#api#users#starred_pages(user) abort
|
2022-05-31 22:47:41 +08:00
|
|
|
let result = s:HTTP.get('https://api.github.com/users/' . a:user . '/starred')
|
|
|
|
if result.status ==# 200
|
|
|
|
let i = filter(result.header, 'v:val =~# "^Link"')[0]
|
2022-05-29 20:59:18 +08:00
|
|
|
return split(matchstr(i,'=\d\+',0,2),'=')[0]
|
|
|
|
endif
|
|
|
|
return 0
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! github#api#users#GetStarred(user) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
let rel = []
|
|
|
|
let pages = github#api#users#starred_pages(a:user)
|
|
|
|
for page in range(1,pages)
|
|
|
|
let repos = github#api#users#starred(a:user, page)
|
|
|
|
for repo in repos
|
|
|
|
call add(rel, repo)
|
2022-04-28 21:46:05 +08:00
|
|
|
endfor
|
2022-05-29 20:59:18 +08:00
|
|
|
endfor
|
|
|
|
return rel
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" get a single user
|
|
|
|
" GET /users/:username
|
|
|
|
function! github#api#users#GetUser(username) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
return github#api#util#Get('users/' . a:username, [])
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
"List followers of a user
|
|
|
|
"GET /users/:username/followers
|
|
|
|
function! github#api#users#ListFollowers(username) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
let followers = []
|
|
|
|
for i in range(1,github#api#util#GetLastPage('users/' . a:username . '/followers'))
|
|
|
|
call extend(followers,github#api#util#Get('users/' . a:username . '/followers?page=' . i, []))
|
|
|
|
endfor
|
|
|
|
return followers
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
"List users followed by another user
|
|
|
|
"GET /users/:username/following
|
|
|
|
function! github#api#users#ListFollowing(username) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
let following = []
|
|
|
|
for i in range(1,github#api#util#GetLastPage('users/' . a:username . '/following'))
|
|
|
|
call extend(following,github#api#util#Get('users/' . a:username . '/following?page=' . i, []))
|
|
|
|
endfor
|
|
|
|
return following
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
""
|
|
|
|
" @public
|
|
|
|
" List orgs of a specified user.
|
|
|
|
"
|
|
|
|
" Github API : /users/:username/orgs
|
|
|
|
function! github#api#users#ListAllOrgs(user) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
return github#api#util#Get(join(['users', a:user, 'orgs'], '/'))
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
""
|
|
|
|
" @public
|
|
|
|
" Check if one user follows another
|
|
|
|
"
|
|
|
|
" Github API : GET /users/:username/following/:target_user
|
|
|
|
function! github#api#users#CheckTargetFollow(username,target) abort
|
2022-05-29 20:59:18 +08:00
|
|
|
return github#api#util#GetStatus(join(['users', a:username, 'following', a:target], '/'),[])
|
2022-04-28 21:46:05 +08:00
|
|
|
endfunction
|