Skip to content

Commit

Permalink
Extract unsubmit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Sep 20, 2014
1 parent 25873dc commit b638a9a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 71 deletions.
39 changes: 37 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ var (
UserAgent string
)

// PayloadError represents an error message from the API.
type PayloadError struct {
Error string `json:"error"`
}

// PayloadProblems represents a response containing problems.
type PayloadProblems struct {
Problems []*Problem
Error string `json:"error"`
PayloadError
}

// PayloadSubmission represents metadata about a successful submission.
type PayloadSubmission struct {
*Submission
Error string `json:"error"`
PayloadError
}

// Fetch retrieves problems from the API.
Expand Down Expand Up @@ -105,3 +110,33 @@ func Submit(url string, iter *Iteration) (*Submission, error) {

return ps.Submission, nil
}

// Unsubmit deletes a submission.
func Unsubmit(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", UserAgent)

res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}

pe := &PayloadError{}
err = json.Unmarshal(body, pe)
if err != nil {
return err
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf(`unable to unsubmit (HTTP: %d) - %s`, res.StatusCode, pe.Error)
}
return nil
}
31 changes: 31 additions & 0 deletions handlers/unsubmit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handlers

import (
"fmt"
"log"

"github.com/codegangsta/cli"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
)

// Unsubmit deletes an iteration from the api.
// If no iteration is specified, the most recent iteration
// is deleted.
func Unsubmit(ctx *cli.Context) {
c, err := config.Read(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}

if !c.IsAuthenticated() {
log.Fatal(msgPleaseAuthenticate)
}

url := fmt.Sprintf("%s/api/v1/user/assignments?key=%s", c.API, c.APIKey)
err = api.Unsubmit(url)
if err != nil {
log.Fatal(err)
}
fmt.Println("Your most recent submission was successfully deleted.")
}
72 changes: 3 additions & 69 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"log"
"os"
"runtime"

"github.com/codegangsta/cli"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/exercism/cli/handlers"
)

Expand All @@ -21,8 +18,6 @@ const (
// lot of things get out of hand.
Version = "1.7.0"

msgPleaseAuthenticate = "You must be authenticated. Run `exercism configure --key=YOUR_API_KEY`."

descDebug = "Outputs useful debug information."
descConfigure = "Writes config values to a JSON file."
descDemo = "Fetches a demo problem for each language track on exercism.io."
Expand Down Expand Up @@ -115,72 +110,11 @@ func main() {
Name: "unsubmit",
ShortName: "u",
Usage: descUnsubmit,
Action: func(ctx *cli.Context) {
c, err := config.Read(ctx.GlobalString("config"))
if err != nil {
fmt.Println(err)
return
}

if !c.IsAuthenticated() {
fmt.Println(msgPleaseAuthenticate)
return
}

err = UnsubmitAssignment(c)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("The last submission was successfully deleted.")
},
Action: handlers.Unsubmit,
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Errorf("%v", err)
os.Exit(1)
}
}

func UnsubmitAssignment(c *config.Config) error {
path := "api/v1/user/assignments"

url := fmt.Sprintf("%s/%s?key=%s", c.API, path, c.APIKey)

req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
log.Fatal(err)
}

req.Header.Set("User-Agent", api.UserAgent)

resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("Error destroying submission: [%v]", err)
return err
}

body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {

var ur struct {
Error string
}

err = json.Unmarshal(body, &ur)
if err != nil {
return err
}

err = fmt.Errorf("Status: %d, Error: %v", resp.StatusCode, ur.Error)
return err
}

return nil
}

0 comments on commit b638a9a

Please sign in to comment.