This repository does various data retrievals for trusted sources automatically. It reads data from an input, makes a request, and records the result in a different file.
- Go 1.20 (recommended)
- Make sure to download all Needed tools
- Clone the repository
git clone https://github.com/dasalgadoc/MyApiConsumer.git
- Build up go project
go mod download
go get .
- Make a config.yaml file in ./configs In ./configs/secure_config.yaml has an example. Setup the data source clients and provide the location and name of input data.
The config files the following structure
Father | Node | Description | Possible values / Example |
---|---|---|---|
- | io | Describes Input parameters | Father node |
io | location | Relative path where input can be found | ./file_exchange/ |
io | file_name | Input filename (without extension) | input |
- | clients | Array with following elements, to describe each rest client | Father node |
clients | name | Name to invoke the client | api-shazam |
clients | type | Resource type, see configs.go Resource_XXX constants | GetRestApi / PostRestApi |
clients | path | HTTP URL for client (without headers) * | https://shazam.p.rapidapi.com/shazam-events/list |
clients | headers | Array with following elements, to describe each header | |
header | :HEADER KEY: | Header name : header value | 'X-RapidAPI-Key': 'SIGN-UP-FOR-KEY' |
If your URL contains path parameters, you may specify them with their names within curly brackets in the clients[n].path property. For instance: http://example.com/movies/{movie_name}
Make sure a column with these parameters exists in your input file. The next section contains further information on this input file.
- Create an input file, in config.yaml file, you specified the property io.file_name. This property is a file where you specific the input data to be processed.
Csv files are one choice; the first row contains the name of each parameter, and each row contains one execution that defines all the parameters.
Example:
move_name | year | query |
---|---|---|
split | 2016 | casting |
Inception | 2010 | rating |
The .csv file columns can be used like path parameters, query variables or both.
You can use the first column on the input file to specify a JSON BODY. Just call the header as "JSON_BODY" and each row the json in string format.
Example:
JSON_BODY | move_name | year | query |
---|---|---|---|
{"key":"value"} | split | 2016 | casting |
{"key":"value"} | Inception | 2010 | rating |
In this case, the first column will be treated like a Body in JSON format and no like a parameter.
- Run the API
go run cmd/cli/main.go
- You'll see various errors since not enough parameters were provided; the correct form is as follows.
go run cmd/cli/main.go <DATA SOURCE>
This project use the following flags:
- inputter: Input file type (default "csv")
- outputter: Output file type (default "json")
Note: No more clients are specified, but this code is extensible.
Thats why you don't need to specify the flags, but you can if you want to change the default values.
go run cmd/cli/main.go -inputter <INPUT FILE TYPE> -outputter <OUTPUT FILE TYPE> <DATA SOURCE>
For instance:
go run cmd/cli/main.go api_engine_beta
or
go run cmd/cli/main.go -inputter csv -outputter json api_engine_beta
A helper script is provided to run the API:
chmod +x runner.sh
./runner.sh