Skip to content
Snippets Groups Projects
Commit 385b0af1 authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

fixed error respons

parent 0132c299
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,11 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
fmt.Println(URL3) // **************************************************
resp := DoRequest(Client, w, URL3) //request the /prodjects link with 100 repositoris per page
resp, err := DoRequest(Client, w, URL3) //request the /prodjects link with 100 repositoris per page
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
PageTot, err := strconv.Atoi(resp.Header.Get("X-Total-Pages")) //Get the total of pages that have repositoris
if err != nil { //check for error
......@@ -36,15 +40,13 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(PageTot)
RepTot, err := strconv.Atoi(resp.Header.Get("X-Total")) // This is for testing and will be deleted *********************************************
if err != nil { //check for error
log.Fatal(err)
}
fmt.Println(RepTot) //***************************************************************
for i := 1; i <= PageTot; i++ { // loop true all pages
URL4 := URL3 + "&page=" + strconv.Itoa(i) // adds page numer to link
resp = DoRequest(Client, w, URL4) // request link
resp, err := DoRequest(Client, w, URL4) // request link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var TempID []IDS // makes a temp for IDS
......@@ -61,7 +63,11 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
var TempRep Commits // creates temp for commits
resp = DoRequest(Client, w, URL4) // request link
resp, err = DoRequest(Client, w, URL4) // request link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = json.NewDecoder(resp.Body).Decode(&TempRep.Repos) // decode to temp
if err != nil { //check for error
......@@ -81,7 +87,11 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
URL = URL + "?private_token=" + auth // adds token to link for auth
}
resp := DoRequest(Client, w, URL) // request link
resp, err := DoRequest(Client, w, URL) // request link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
TempCommit, err := strconv.Atoi(resp.Header.Get("X-Total")) // gets total of commits
if err != nil { //check for error
......@@ -109,6 +119,9 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
Payload := &WebhooksInvocation{}
Payload.Event = "Commits"
json.NewEncoder(w).Encode(C) // encode C
}
......@@ -23,20 +23,19 @@ func QueryGet(s string, t string, r *http.Request) string { // funk to read limi
}
func DoRequest(Client *http.Client, w http.ResponseWriter, API string) *http.Response { // Request api
func DoRequest(Client *http.Client, w http.ResponseWriter, API string) (*http.Response, error) { // Request api
req, err := http.NewRequest(http.MethodGet, API, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
resp, err := Client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
return resp
return resp, err
}
// func HandlerWebhooks(w http.ResponseWriter, r *http.Request) {
......
......@@ -44,7 +44,11 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
fmt.Println(URL3)
resp := DoRequest(Client, w, URL3) // request link
resp, err := DoRequest(Client, w, URL3) // request link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
PageTot, err := strconv.Atoi(resp.Header.Get("X-Total-Pages")) // gets count of total pages
if err != nil { // check for error
......@@ -56,11 +60,15 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
for i := 1; i <= PageTot; i++ { // loops true total pages
URL := URL3 + "&page=" + strconv.Itoa(i) // sets url to one page
resp := DoRequest(Client, w, URL)
resp, err := DoRequest(Client, w, URL)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var TempID []IDLang //creat temp for id
err := json.NewDecoder(resp.Body).Decode(&TempID) // decode to I
err = json.NewDecoder(resp.Body).Decode(&TempID) // decode to I
if err != nil { // check for error
http.Error(w, err.Error(), http.StatusBadRequest)
return
......@@ -82,7 +90,11 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
var CodeTemp = make(map[string]interface{})
resp = DoRequest(Client, w, URL) // request link
resp, err = DoRequest(Client, w, URL) // request link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = json.NewDecoder(resp.Body).Decode(&CodeTemp) // decode to language
if err != nil { // check for error
......
package assignment2
import "time"
//************* Commits ******************
type IDS struct {
......@@ -48,8 +50,10 @@ type Status struct {
//************ Webhook ************
type WebhookRegistration struct {
Event string `json:"event"`
URL string `json:"urk2`
ID int `json:"id"`
Event string `json:"Event"`
URL string `json:"url"`
Time time.Time `json:"time"`
}
type Webhooks struct {
......
......@@ -7,9 +7,11 @@ import (
"io/ioutil"
"net/http"
"strconv"
"time"
)
var webhooks []WebhookRegistration //Webhook DB
var IDNum = 1
func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
......@@ -21,6 +23,10 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
webhook.ID = IDNum
webhook.Time = time.Now()
IDNum++
webhooks = append(webhooks, webhook)
fmt.Fprintln(w, len(webhooks)-1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment