Created
November 29, 2020 10:33
-
-
Save RicardoLinck/e6ee45563cfa21f4535b603b5c2b8f65 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package fetch | |
import ( | |
"context" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"net/http" | |
) | |
// NameFetcher fetches data from Name service | |
type NameFetcher struct { | |
url string | |
} | |
func NewNameFetcher(url string) *NameFetcher { | |
return &NameFetcher{url: url} | |
} | |
// NameServiceResponse represents the response from Name Service | |
type NameServiceResponse struct { | |
Name string `json:"name"` | |
Surname string `json:"surname"` | |
} | |
func (n *NameFetcher) Fetch(ctx context.Context, email string, results chan<- Result) error { | |
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s?email=%s", n.url, email), nil) | |
if err != nil { | |
return err | |
} | |
r, err := http.DefaultClient.Do(req) | |
if err != nil { | |
return err | |
} | |
if r.StatusCode != 200 { | |
return errors.New(r.Status) | |
} | |
defer r.Body.Close() | |
nsr := NameServiceResponse{} | |
err = json.NewDecoder(r.Body).Decode(&nsr) | |
if err != nil { | |
return err | |
} | |
results <- Result{Key: "name", Value: nsr} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment