<commit_before>package main

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"flag"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/soh335/apnsapi"
	"golang.org/x/crypto/pkcs12"
)

var (
	cerFile     = flag.String("cer", "", "cer")
	keyFile     = flag.String("key", "", "key")
	p12File     = flag.String("p12", "", "p12")
	p12Password = flag.String("password", "", "password of <PASSWORD>")
	production  = flag.Bool("production", false, "connect to production server")
	msg         = flag.String("msg", "hi", "msg")
	token       = flag.String("token", "", "token")
	topic       = flag.String("topic", "", "topic")
)

func main() {
	flag.Parse()
	if err := _main(); err != nil {
		log.Fatal(err)
	}
}

func _main() error {
	cert, err := loadCertificate()
	if err != nil {
		return err
	}
	config := &tls.Config{
		Certificates: []tls.Certificate{cert},
	}
	tr := &http.Transport{
		TLSClientConfig: config,
	}
	c := &http.Client{
		Transport: tr,
	}

	var host string
	if *production {
		host = apnsapi.ProductionServer
	} else {
		host = apnsapi.DevelopmentServer
	}

	client := apnsapi.NewClient(host, c)

	var b bytes.Buffer

	p := map[string]interface{}{
		"aps": map[string]interface{}{
			"alert": *msg,
		},
	}

	if err := json.NewEncoder(&b).Encode(p); err != nil {
		return err
	}

	header := &apnsapi.Header{ApnsTopic: *topic}

	_, err = client.Do(*token, header, b.Bytes())
	if err != nil {
		switch err.(type) {
		case *apnsapi.ErrorResponse:
			log.Println("error response:", err.(*apnsapi.ErrorResponse).Reason)
		default:
			return err
		}
	}

	return nil
}

func loadCertificate() (tls.Certificate, error) {
	if *p12File != "" {
		cert := tls.Certificate{}
		b, err := ioutil.ReadFile(*p12File)
		if err != nil {
			return cert, err
		}
		key, cer, err := pkcs12.Decode(b, *p12Password)
		if err != nil {
			return cert, err
		}
		cert.PrivateKey = key
		cert.Certificate = [][]byte{cer.Raw}
		return cert, nil
	} else {
		return tls.LoadX509KeyPair(*cerFile, *keyFile)
	}
}<commit_msg>update _example/main.go for jwt<commit_after>package main

import (
	"bytes"
	"crypto/ecdsa"
	"crypto/tls"
	"crypto/x509"
	"encoding/json"
	"encoding/pem"
	"flag"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/soh335/apnsapi"
	"golang.org/x/crypto/pkcs12"
	"golang.org/x/net/http2"
)

var (
	cerFile     = flag.String("cer", "", "cer")
	keyFile     = flag.String("key", "", "key")
	p12File     = flag.String("p12", "", "p12")
	p12Password = flag.String("password", "", "<PASSWORD>")
	production  = flag.Bool("production", false, "connect to production server")
	msg         = flag.String("msg", "hi", "msg")
	token       = flag.String("token", "", "token")
	topic       = flag.String("topic", "", "topic")
	kid         = flag.String("kid", "", "key identifier")
	teamId      = flag.String("teamId", "", "team id")
)

func main() {
	flag.Parse()
	var client *http.Client
	var header *apnsapi.Header
	if *kid != "" && *teamId != "" {
		jwtToken, err := createJwtToken()
		if err != nil {
			log.Fatal(err)
		}
		client = http.DefaultClient
		header = &apnsapi.Header{ApnsTopic: *topic, Authorization: "bearer " + jwtToken}
	} else {
		cert, err := loadCertificate()
		if err != nil {
			log.Fatal(err)
		}
		config := &tls.Config{
			Certificates: []tls.Certificate{cert},
		}
		tr := &http.Transport{
			TLSClientConfig: config,
		}
		if err := http2.ConfigureTransport(tr); err != nil {
			log.Fatal(err)
		}

		client = &http.Client{
			Transport: tr,
		}

		header = &apnsapi.Header{ApnsTopic: *topic}
	}
	if err := _main(client, header); err != nil {
		log.Fatal(err)
	}
}

func _main(c *http.Client, header *apnsapi.Header) error {
	var host string
	if *production {
		host = apnsapi.ProductionServer
	} else {
		host = apnsapi.DevelopmentServer
	}

	client := apnsapi.NewClient(host, c)

	var b bytes.Buffer

	p := map[string]interface{}{
		"aps": map[string]interface{}{
			"alert": *msg,
		},
	}

	if err := json.NewEncoder(&b).Encode(p); err != nil {
		return err
	}

	_, err := client.Do(*token, header, b.Bytes())
	if err != nil {
		switch err.(type) {
		case *apnsapi.ErrorResponse:
			log.Println("error response:", err.(*apnsapi.ErrorResponse).Reason)
		default:
			return err
		}
	}

	return nil
}

func loadCertificate() (tls.Certificate, error) {
	if *p12File != "" {
		cert := tls.Certificate{}
		b, err := ioutil.ReadFile(*p12File)
		if err != nil {
			return cert, err
		}
		key, cer, err := pkcs12.Decode(b, *p12Password)
		if err != nil {
			return cert, err
		}
		cert.PrivateKey = key
		cert.Certificate = [][]byte{cer.Raw}
		return cert, nil
	} else {
		return tls.LoadX509KeyPair(*cerFile, *keyFile)
	}
}

func createJwtToken() (string, error) {
	data, err := ioutil.ReadFile(*keyFile)
	if err != nil {
		return "", err
	}
	block, _ := pem.Decode(data)
	key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		return "", err
	}
	return apnsapi.CreateToken(key.(*ecdsa.PrivateKey), *kid, *teamId)
}
{'fraction_non_alphanumeric': 0.17726904376012967, 'fraction_numerical': 0.011547811993517018, 'mean_word_length': 3.447747747747748, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2707133', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1864, 'n_words': 405}