Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchsan committed Dec 25, 2020
1 parent a3cfb5d commit a9a5102
Show file tree
Hide file tree
Showing 24 changed files with 818 additions and 395 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: CI

on: pull_request

jobs:

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.33
- name: Run gosec
run: |
go get github.com/securego/gosec/v2/cmd/gosec
gosec ./...
test:
name: Test
strategy:
matrix:
go-version: [1.13.x, 1.14.x, 1.15.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Run test
run: go test ./...

coverage:
name: Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Generate coverage
run: go test ./... -coverprofile coverage.txt
- name: Upload coverage
uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
16 changes: 0 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
### https://raw.github.com/github/gitignore/90f149de451a5433aebd94d02d11b0e28843a1af/Go.gitignore

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out


1 change: 0 additions & 1 deletion .go-version

This file was deleted.

6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
run:
timeout: 1m
tests: false

linters:
enable-all: true
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# device-check-go

![golang](https://img.shields.io/badge/golang-1.11-blue.svg?style=flat)
![GitHub release](https://img.shields.io/github/release/snowman-mh/device-check-go.svg?colorB=7E7E7E)
[![GoDoc](https://godoc.org/github.com/snowman-mh/device-check-go?status.svg)](https://godoc.org/github.com/snowman-mh/device-check-go)
[![Build Status](https://travis-ci.org/snowman-mh/device-check-go.svg?branch=master)](https://travis-ci.org/snowman-mh/device-check-go)
[![codecov.io](https://codecov.io/github/snowman-mh/device-check-go/coverage.svg?branch=master)](https://codecov.io/github/snowman-mh/device-check-go?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/snowman-mh/device-check-go)](https://goreportcard.com/report/github.com/snowman-mh/device-check-go)
[![GitHub Actions Status](https://github.com/rinchsan/device-check-go/workflows/build/badge.svg)](https://github.com/rinchsan/device-check-go/actions)
![GitHub release](https://img.shields.io/github/release/rinchsan/device-check-go.svg?colorB=7E7E7E)
[![Go Reference](https://pkg.go.dev/badge/github.com/rinchsan/device-check-go.svg)](https://pkg.go.dev/github.com/rinchsan/device-check-go)
[![codecov.io](https://codecov.io/github/rinchsan/device-check-go/coverage.svg?branch=master)](https://codecov.io/github/rinchsan/device-check-go?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/rinchsan/device-check-go)](https://goreportcard.com/report/github.com/rinchsan/device-check-go)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)

iOS DeviceCheck SDK for Go - query and modify the per-device bits

## Installation

```bash
go get github.com/snowman-mh/device-check-go
go get github.com/rinchsan/device-check-go
```

## Get started

### Initialize SDK

```go
import "github.com/snowman-mh/device-check-go"
import "github.com/rinchsan/device-check-go"

cred := devicecheck.NewCredentialFile("/path/to/private/key/file") // You can create credential also from raw string/bytes
cfg := devicecheck.NewConfig("ISSUER", "KEY_ID", devicecheck.Development)
Expand All @@ -33,7 +32,7 @@ client := devicecheck.New(cred, cfg)
#### Query two bits

```go
result := devicecheck.QueryTwoBitsResult{}
var result devicecheck.QueryTwoBitsResult
if err := client.QueryTwoBits("DEVICE_TOKEN_FROM_CLIENT", &result); err != nil {
// error handling
// Note that SDK returns ErrBitStateNotFound error if no bits found
Expand Down
28 changes: 9 additions & 19 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package devicecheck

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

Expand All @@ -20,7 +20,7 @@ func newBaseURL(env Environment) string {
case Production:
return productionBaseURL
default:
panic("no matching case")
return developmentBaseURL
}
}

Expand All @@ -31,7 +31,7 @@ type api struct {

func newAPI(env Environment) api {
return api{
client: new(http.Client),
client: http.DefaultClient,
baseURL: newBaseURL(env),
}
}
Expand All @@ -43,28 +43,18 @@ func newAPIWithHTTPClient(client *http.Client, env Environment) api {
}
}

func (api api) do(jwt, path string, requestBody interface{}) (int, []byte, error) {
func (api api) do(jwt, path string, requestBody interface{}) (*http.Response, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(requestBody); err != nil {
return http.StatusInternalServerError, nil, err
}

req, err := http.NewRequest(http.MethodPost, api.baseURL+path, buf)
if err != nil {
return http.StatusInternalServerError, nil, err
return nil, fmt.Errorf("json: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt))

resp, err := api.client.Do(req)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, api.baseURL+path, buf)
if err != nil {
return http.StatusInternalServerError, nil, err
return nil, fmt.Errorf("http: %w", err)
}
defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return http.StatusInternalServerError, nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt))

return resp.StatusCode, responseBody, nil
return api.client.Do(req)
}
Loading

0 comments on commit a9a5102

Please sign in to comment.