main.go - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       main.go (2762B)
       ---
            1 // iDeal payment status check.
            2 package main
            3 
            4 import (
            5         "encoding/json"
            6         "fmt"
            7         "io/ioutil"
            8         "net/http"
            9         "time"
           10 )
           11 
           12 type IssuersResponse struct {
           13         Issuers []struct {
           14                 //BankId      string
           15                 BankName    string
           16                 Status      string
           17                 Percent     string
           18                 Details     string
           19                 MoreDetails string
           20         }
           21         Message string
           22         //IssuerMessageCalculationFailed bool
           23         LastUpdate string // NOTE: Dutch format ("d-m-yyyy, H:MM").
           24         AllGreen   bool
           25 }
           26 
           27 type AcquirersResponse struct {
           28         Acquirers []struct {
           29                 BankName string
           30                 Status   string
           31                 Percent  string
           32         }
           33         Message string
           34         //AcquirerMessageCalculationFailed bool
           35         AllGreen        bool
           36         AllGreenMessage string
           37 }
           38 
           39 func process(url string) ([]byte, error) {
           40         client := &http.Client{
           41                 Timeout: 30 * time.Second,
           42         }
           43         req, err := http.NewRequest("GET", url, nil)
           44         if err != nil {
           45                 panic(err)
           46         }
           47         // hide User-Agent.
           48         req.Header.Add("User-Agent", "")
           49 
           50         resp, err := client.Do(req)
           51         if resp != nil {
           52                 defer resp.Body.Close() // err != nil && resp != nil can happen on redirect failure.
           53         }
           54         if err != nil {
           55                 panic(err)
           56         }
           57         if resp.StatusCode != 200 {
           58                 panic(fmt.Errorf("statuscode: %d", resp.StatusCode))
           59         }
           60         return ioutil.ReadAll(resp.Body)
           61 }
           62 
           63 func issuers() {
           64         fmt.Printf("Issuers\n")
           65         fmt.Printf("=======\n")
           66 
           67         // http://beschikbaarheid.ideal.nl/ for Dutch status texts.
           68         data, err := process("https://availability.ideal.nl/api/api/GetIssuers")
           69         if err != nil {
           70                 panic(err)
           71         }
           72 
           73         r := IssuersResponse{}
           74         if err := json.Unmarshal(data, &r); err != nil {
           75                 panic(err)
           76         }
           77 
           78         if !r.AllGreen {
           79                 fmt.Printf("!!! There is an issue !!!\n\n")
           80         }
           81 
           82         fmt.Printf("Last update: %s\n", r.LastUpdate)
           83         if len(r.Message) > 0 {
           84                 fmt.Printf("Message:     %s\n", r.Message)
           85         }
           86         fmt.Printf("\n")
           87 
           88         for _, item := range r.Issuers {
           89                 fmt.Printf("%-25.25s: %s (%s%%)\n", item.BankName, item.Status, item.Percent)
           90                 if len(item.Details) > 0 {
           91                         fmt.Printf("\tDetails: %s\n", item.Details)
           92                 }
           93                 if len(item.MoreDetails) > 0 {
           94                         fmt.Printf("\tMore details: %s\n", item.MoreDetails)
           95                 }
           96         }
           97 }
           98 
           99 func acquirers() {
          100         fmt.Printf("Acquirers\n")
          101         fmt.Printf("=========\n")
          102 
          103         // http://beschikbaarheid.ideal.nl/ for Dutch status texts.
          104         data, err := process("https://availability.ideal.nl/api/api/GetAcquirers")
          105         if err != nil {
          106                 panic(err)
          107         }
          108 
          109         r := AcquirersResponse{}
          110         if err := json.Unmarshal(data, &r); err != nil {
          111                 panic(err)
          112         }
          113 
          114         if !r.AllGreen {
          115                 fmt.Printf("!!! There is an issue !!!\n\n")
          116         }
          117         if len(r.AllGreenMessage) > 0 {
          118                 fmt.Printf("%s\n", r.AllGreenMessage)
          119         }
          120         if len(r.Message) > 0 {
          121                 fmt.Printf("Message:     %s\n", r.Message)
          122         }
          123         fmt.Printf("\n")
          124 
          125         for _, item := range r.Acquirers {
          126                 fmt.Printf("%-25.25s: %s (%s%%)\n", item.BankName, item.Status, item.Percent)
          127         }
          128 }
          129 
          130 func main() {
          131         issuers()
          132 
          133         fmt.Printf("\n\n")
          134 
          135         acquirers()
          136 }