-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from BenB196/staging
Staging to master
- Loading branch information
Showing
5 changed files
with
352 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.3.0 | ||
0.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package code42 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// Code42 Auth | ||
|
||
//Structs of Crashplan FFS API Authentication Token Return | ||
type AuthData struct { | ||
Data AuthToken `json:"data"` | ||
Error string `json:"error,omitempty"` | ||
Warnings string `json:"warnings,omitempty"` | ||
} | ||
type AuthToken struct { | ||
V3UserToken string `json:"v3_user_token"` | ||
} | ||
|
||
/* | ||
GetAuthData - Function to get the Authentication data (mainly the authentication token) which will be needed for the rest of the API calls | ||
The authentication token is good for up to 1 hour before it expires | ||
*/ | ||
func GetAuthData(uri string, username string, password string) (*AuthData, error) { | ||
//Build HTTP GET request | ||
req, err := http.NewRequest("GET", uri, nil) | ||
|
||
//Return nil and err if Building of HTTP GET request fails | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//Set Basic Auth Header | ||
req.SetBasicAuth(username, password) | ||
//Set Accept Header | ||
req.Header.Set("Accept", "application/json") | ||
|
||
//Make the HTTP Call | ||
resp, err := http.DefaultClient.Do(req) | ||
|
||
//Return nil and err if Building of HTTP GET request fails | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
//Return err if status code != 200 | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.New("Error with Authentication Token GET: " + resp.Status) | ||
} | ||
|
||
//Create AuthData variable | ||
var authData AuthData | ||
|
||
respData := resp.Body | ||
|
||
responseBytes, _ := ioutil.ReadAll(respData) | ||
|
||
if strings.Contains(string(responseBytes), "Service Under Maintenance") { | ||
return nil, errors.New("error: auth api service is under maintenance") | ||
} | ||
|
||
//Decode the resp.Body into authData variable | ||
err = json.Unmarshal(responseBytes, &authData) | ||
|
||
//Return nil and err if decoding of resp.Body fails | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//Return AuthData | ||
return &authData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package code42 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
//Structs for FFS Queries | ||
type Query struct { | ||
Groups []Group `json:"groups"` | ||
GroupClause string `json:"groupClause,omitempty"` | ||
PgNum int `json:"pgNum,omitempty"` | ||
PgSize int `json:"pgSize,omitempty"` | ||
PgToken *string `json:"pgToken,omitempty"` | ||
SrtDir string `json:"srtDir,omitempty"` | ||
SrtKey string `json:"srtKey,omitempty"` | ||
} | ||
|
||
type Group struct { | ||
Filters []SearchFilter `json:"filters"` | ||
FilterClause string `json:"filterClause,omitempty"` | ||
} | ||
|
||
type SearchFilter struct { | ||
Operator string `json:"operator"` | ||
Term string `json:"term"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type QueryProblem struct { | ||
BadFilter SearchFilter `json:"badFilter,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
} | ||
|
||
func ExecQuery(authData AuthData, ffsURI string, query Query) (*http.Response, error) { | ||
//Validate jsonQuery is valid JSON | ||
ffsQuery, err := json.Marshal(query) | ||
if err != nil { | ||
return nil, errors.New("jsonQuery is not in a valid json format") | ||
} | ||
|
||
//Make sure authData token is not "" | ||
if authData.Data.V3UserToken == "" { | ||
return nil, errors.New("authData cannot be nil") | ||
} | ||
|
||
//Query ffsURI with authData API token and jsonQuery body | ||
req, err := http.NewRequest("POST", ffsURI, bytes.NewReader(ffsQuery)) | ||
|
||
//Handle request errors | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//Set request headers | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "v3_user_token "+authData.Data.V3UserToken) | ||
|
||
//Get Response | ||
resp, err := http.DefaultClient.Do(req) | ||
|
||
//Handle response errors | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//defer body close | ||
defer resp.Body.Close() | ||
|
||
//Make sure http status code is 200 | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.New("Error with gathering file events POST: " + resp.Status) | ||
} | ||
|
||
return resp, nil | ||
} |
Oops, something went wrong.