Skip to content

Commit

Permalink
Fix for not being dependent of .env inside the functions and improve …
Browse files Browse the repository at this point in the history
…abstraction
  • Loading branch information
Isaac Guerreiro committed Aug 30, 2021
1 parent 4ec0bd9 commit 76510c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
USERNAME=friendzymes
PASSWORD=Open3000
ENCONDED_AUTH="Basic ZnJpZW5kenltZXMwMDE6NjZkMDU0YmUtZjA1Ni00YTYyLTg0NmEtZTRiMWY4NGJhYmE3"
USERNAME=
PASSWORD=
ENCODED_AUTH="Basic ZnJpZW5kenltZXMwMDE6NjZkMDU0YmUtZjA1Ni00YTYyLTg0NmEtZTRiMWY4NGJhYmE3"
TOKEN_URL="https://www.idtdna.com/Identityserver/connect/token"
COMPLEXITY_URL="https://www.idtdna.com/api/complexities/screengBlockSequences"
23 changes: 4 additions & 19 deletions idtapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"encoding/json"
"net/http"
"net/url"
"os"
"strconv"
"strings"

"github.com/joho/godotenv"
)

type Authentication struct {
Expand All @@ -29,23 +26,13 @@ type Problem struct {
StartIndex int `json:"StartIndex"`
}

func GetComplexityScore(sequences []string) [][]Problem {
func GetComplexityScore(sequences []string, username string, password string, encondedAuth string, urlPath string, urlToken string) [][]Problem {
var sequencesInput []Sequence
for i, sequence := range sequences {
sequencesInput = append(sequencesInput, Sequence{"#" + strconv.Itoa(i), sequence})
}
err := godotenv.Load(".env")

if err != nil {
panic("Error loading .env file")
}

username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
encondedAuth := os.Getenv("ENCONDED_AUTH")
urlPath := os.Getenv("COMPLEXITY_URL")

auth := getToken(username, password, encondedAuth)
auth := getToken(username, password, encondedAuth, urlToken)
requestByte, _ := json.Marshal(sequencesInput)
req, _ := http.NewRequest("POST", urlPath, bytes.NewReader(requestByte))
req.Header.Set("Content-Type", "application/json")
Expand All @@ -71,16 +58,14 @@ func GetComplexityScore(sequences []string) [][]Problem {
return gBlockAnalyzed

}
func getToken(username string, password string, encondedAuth string) Authentication {
urlPath := os.Getenv("TOKEN_URL")

func getToken(username string, password string, encondedAuth string, urlToken string) Authentication {
data := url.Values{}
data.Set("grant_type", "password")
data.Set("username", username)
data.Set("password", password)
data.Set("scope", "test")

req, err := http.NewRequest("POST", urlPath, strings.NewReader(data.Encode()))
req, err := http.NewRequest("POST", urlToken, strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", encondedAuth)

Expand Down
19 changes: 16 additions & 3 deletions idtapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ import (
)

func ExampleGetComplexityScore() {
fmt.Println(GetComplexityScore([]string{"TGGTACGAAAATTAGGGGATCTACCTAGAAAGCCACAAGGCGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATAGGTCAAGCTTAAAGAACCCTTACATGGATCTTACAGATTCTGAAAGTAAAGAAACAACAGAGGTTAAACAAACAGAACCAAAAAGAAAAAAAGCATTGTTGAAAACAATGAAAGTTGATGTTTCAATCCATAATAAGATTAAATCGCTGCACGAAATTCTGGCAGCATCCGAAGGAAAAA"}))
err := godotenv.Load(".env")

if err != nil {
panic("Error loading .env file")
}

username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
encodedAuth := os.Getenv("ENCODED_AUTH")
urlPath := os.Getenv("COMPLEXITY_URL")
urlToken := os.Getenv("TOKEN_URL")
sequences := []string{"TGGTACGAAAATTAGGGGATCTACCTAGAAAGCCACAAGGCGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATAGGTCAAGCTTAAAGAACCCTTACATGGATCTTACAGATTCTGAAAGTAAAGAAACAACAGAGGTTAAACAAACAGAACCAAAAAGAAAAAAAGCATTGTTGAAAACAATGAAAGTTGATGTTTCAATCCATAATAAGATTAAATCGCTGCACGAAATTCTGGCAGCATCCGAAGGAAAAA"}
fmt.Println(GetComplexityScore(sequences, username, password, encodedAuth, urlPath, urlToken))
//Output: H
}

Expand All @@ -21,9 +33,10 @@ func ExampleGetToken() {

username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
encondedAuth := os.Getenv("ENCONDED_AUTH")
encondedAuth := os.Getenv("ENCODED_AUTH")
urlToken := os.Getenv("TOKEN_URL")

auth := getToken(username, password, encondedAuth)
auth := getToken(username, password, encondedAuth, urlToken)
fmt.Println(auth)
//Output: Hello
}

0 comments on commit 76510c8

Please sign in to comment.