forked from vieter-v/vieter
				
			feat(client): added client code for logs API
							parent
							
								
									407b226955
								
							
						
					
					
						commit
						fa6603bd45
					
				| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
module client
 | 
					module client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import net.http
 | 
					import net.http { Method }
 | 
				
			||||||
 | 
					import net.urllib
 | 
				
			||||||
import response { Response }
 | 
					import response { Response }
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,24 +18,35 @@ pub fn new(address string, api_key string) Client {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// send_request<T> is a convenience method for sending requests to the repos
 | 
					// send_request<T> just calls send_request_with_body<T> with an empty body.
 | 
				
			||||||
// API. It mostly does string manipulation to create a query string containing
 | 
					fn (c &Client) send_request<T>(method Method, url string, params map[string]string) ?Response<T> {
 | 
				
			||||||
// the provided params.
 | 
						return c.send_request_with_body<T>(method, url, params, '')
 | 
				
			||||||
fn (c &Client) send_request<T>(method http.Method, url string, params map[string]string) ?Response<T> {
 | 
					}
 | 
				
			||||||
	mut full_url := '${c.address}$url'
 | 
					
 | 
				
			||||||
 | 
					// send_request_with_body<T> is a convenience method for sending requests to
 | 
				
			||||||
 | 
					// the repos API. It mostly does string manipulation to create a query string
 | 
				
			||||||
 | 
					// containing the provided params.
 | 
				
			||||||
 | 
					fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) ?Response<T> {
 | 
				
			||||||
 | 
						mut full_url := '$c.address$url'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if params.len > 0 {
 | 
						if params.len > 0 {
 | 
				
			||||||
		params_str := params.keys().map('$it=${params[it]}').join('&')
 | 
							mut params_escaped := map[string]string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Escape each query param
 | 
				
			||||||
 | 
							for k, v in params {
 | 
				
			||||||
 | 
								params_escaped[k] = urllib.query_escape(v)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							params_str := params_escaped.keys().map('$it=${params[it]}').join('&')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		full_url = '$full_url?$params_str'
 | 
							full_url = '$full_url?$params_str'
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mut req := http.new_request(method, full_url, '') ?
 | 
						mut req := http.new_request(method, full_url, body) ?
 | 
				
			||||||
	req.add_custom_header('X-API-Key', c.api_key) ?
 | 
						req.add_custom_header('X-Api-Key', c.api_key) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res := req.do() ?
 | 
						res := req.do() ?
 | 
				
			||||||
	data := json.decode(Response<T>, res.text) ?
 | 
						data := json.decode(Response<T>, res.text) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data
 | 
						return data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,19 +1,19 @@
 | 
				
			||||||
module client
 | 
					module client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import db
 | 
					import db { GitRepo }
 | 
				
			||||||
import net.http
 | 
					import net.http { Method }
 | 
				
			||||||
import response { Response }
 | 
					import response { Response }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// get_repos returns the current list of repos.
 | 
					// get_repos returns the current list of repos.
 | 
				
			||||||
pub fn (c &Client) get_git_repos() ?[]db.GitRepo {
 | 
					pub fn (c &Client) get_git_repos() ?[]GitRepo {
 | 
				
			||||||
	data := c.send_request<[]db.GitRepo>(http.Method.get, '/api/repos', {}) ?
 | 
						data := c.send_request<[]GitRepo>(Method.get, '/api/repos', {}) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data.data
 | 
						return data.data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// get_repo returns the repo for a specific ID.
 | 
					// get_repo returns the repo for a specific ID.
 | 
				
			||||||
pub fn (c &Client) get_git_repo(id int) ?db.GitRepo {
 | 
					pub fn (c &Client) get_git_repo(id int) ?GitRepo {
 | 
				
			||||||
	data := c.send_request<db.GitRepo>(http.Method.get, '/api/repos/$id', {}) ?
 | 
						data := c.send_request<GitRepo>(Method.get, '/api/repos/$id', {}) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data.data
 | 
						return data.data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -30,14 +30,14 @@ pub fn (c &Client) add_git_repo(url string, branch string, repo string, arch []s
 | 
				
			||||||
		params['arch'] = arch.join(',')
 | 
							params['arch'] = arch.join(',')
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data := c.send_request<string>(http.Method.post, '/api/repos', params) ?
 | 
						data := c.send_request<string>(Method.post, '/api/repos', params) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data
 | 
						return data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// remove_repo removes the repo with the given ID from the server.
 | 
					// remove_repo removes the repo with the given ID from the server.
 | 
				
			||||||
pub fn (c &Client) remove_git_repo(id int) ?Response<string> {
 | 
					pub fn (c &Client) remove_git_repo(id int) ?Response<string> {
 | 
				
			||||||
	data := c.send_request<string>(http.Method.delete, '/api/repos/$id', {}) ?
 | 
						data := c.send_request<string>(Method.delete, '/api/repos/$id', {}) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data
 | 
						return data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ pub fn (c &Client) remove_git_repo(id int) ?Response<string> {
 | 
				
			||||||
// patch_repo sends a PATCH request to the given repo with the params as
 | 
					// patch_repo sends a PATCH request to the given repo with the params as
 | 
				
			||||||
// payload.
 | 
					// payload.
 | 
				
			||||||
pub fn (c &Client) patch_git_repo(id int, params map[string]string) ?Response<string> {
 | 
					pub fn (c &Client) patch_git_repo(id int, params map[string]string) ?Response<string> {
 | 
				
			||||||
	data := c.send_request<string>(http.Method.patch, '/api/repos/$id', params) ?
 | 
						data := c.send_request<string>(Method.patch, '/api/repos/$id', params) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return data
 | 
						return data
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,42 @@
 | 
				
			||||||
 | 
					module client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import db { BuildLog }
 | 
				
			||||||
 | 
					import net.http { Method }
 | 
				
			||||||
 | 
					import response { Response }
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn (c &Client) get_build_logs() ?Response<[]BuildLog> {
 | 
				
			||||||
 | 
						data := c.send_request<[]BuildLog>(Method.get, '/api/logs', {}) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn (c &Client) get_build_logs_for_repo(repo_id int) ?Response<[]BuildLog> {
 | 
				
			||||||
 | 
						params := {
 | 
				
			||||||
 | 
							'repo': repo_id.str()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data := c.send_request<[]BuildLog>(Method.get, '/api/logs', params) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn (c &Client) get_build_log(id int) ?Response<BuildLog> {
 | 
				
			||||||
 | 
						data := c.send_request<BuildLog>(Method.get, '/api/logs/$id', {}) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn (c &Client) add_build_log(repo_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response<string> {
 | 
				
			||||||
 | 
						params := {
 | 
				
			||||||
 | 
							'repo':      repo_id.str()
 | 
				
			||||||
 | 
							'startTime': start_time.str()
 | 
				
			||||||
 | 
							'endTime':   end_time.str()
 | 
				
			||||||
 | 
							'arch':      arch
 | 
				
			||||||
 | 
							'exitCode':  exit_code.str()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data := c.send_request_with_body<string>(Method.post, '/api/logs', params, content) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -31,7 +31,7 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Daemon {
 | 
					pub struct Daemon {
 | 
				
			||||||
mut:
 | 
					mut:
 | 
				
			||||||
	client client.Client
 | 
						client                  client.Client
 | 
				
			||||||
	base_image              string
 | 
						base_image              string
 | 
				
			||||||
	builder_images          []string
 | 
						builder_images          []string
 | 
				
			||||||
	global_schedule         CronExpression
 | 
						global_schedule         CronExpression
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -154,9 +154,7 @@ pub fn (db &VieterDb) update_git_repo_archs(repo_id int, archs []GitRepoArch) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn (db &VieterDb) git_repo_exists(repo_id int) bool {
 | 
					pub fn (db &VieterDb) git_repo_exists(repo_id int) bool {
 | 
				
			||||||
	db.get_git_repo(repo_id) or {
 | 
						db.get_git_repo(repo_id) or { return false }
 | 
				
			||||||
		return false
 | 
					
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,7 @@ pub:
 | 
				
			||||||
	repo_id    int       [nonull]
 | 
						repo_id    int       [nonull]
 | 
				
			||||||
	start_time time.Time [nonull]
 | 
						start_time time.Time [nonull]
 | 
				
			||||||
	end_time   time.Time [nonull]
 | 
						end_time   time.Time [nonull]
 | 
				
			||||||
	arch string [nonull]
 | 
						arch       string    [nonull]
 | 
				
			||||||
	exit_code  int       [nonull]
 | 
						exit_code  int       [nonull]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -160,7 +160,6 @@ fn patch(conf Config, id string, params map[string]string) ? {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	id_int := id.int()
 | 
						id_int := id.int()
 | 
				
			||||||
	if id_int != 0 {
 | 
						if id_int != 0 {
 | 
				
			||||||
 | 
					 | 
				
			||||||
		c := client.new(conf.address, conf.api_key)
 | 
							c := client.new(conf.address, conf.api_key)
 | 
				
			||||||
		res := c.patch_git_repo(id_int, params) ?
 | 
							res := c.patch_git_repo(id_int, params) ?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -176,7 +175,7 @@ fn info(conf Config, id string) ? {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		c := client.new(conf.address, conf.api_key)
 | 
						c := client.new(conf.address, conf.api_key)
 | 
				
			||||||
	repo := c.get_git_repo(id_int) ?
 | 
						repo := c.get_git_repo(id_int) ?
 | 
				
			||||||
	println(repo)
 | 
						println(repo)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,7 +29,7 @@ fn (mut app App) get_single_log(id int) web.Result {
 | 
				
			||||||
	if !app.is_authorized() {
 | 
						if !app.is_authorized() {
 | 
				
			||||||
		return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
 | 
							return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	log := app.db.get_build_log(id) or { return app.not_found() }
 | 
						log := app.db.get_build_log(id) or { return app.not_found() }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return app.json(http.Status.ok, new_data_response(log))
 | 
						return app.json(http.Status.ok, new_data_response(log))
 | 
				
			||||||
| 
						 | 
					@ -43,7 +43,8 @@ fn (mut app App) get_log_contents(id int) web.Result {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log := app.db.get_build_log(id) or { return app.not_found() }
 | 
						log := app.db.get_build_log(id) or { return app.not_found() }
 | 
				
			||||||
	file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
 | 
						file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
 | 
				
			||||||
	full_path := os.join_path(app.conf.data_dir, server.logs_dir_name, log.repo_id.str(), log.arch, file_name)
 | 
						full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.repo_id.str(), log.arch,
 | 
				
			||||||
 | 
							file_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return app.file(full_path)
 | 
						return app.file(full_path)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue