Okay, I've reviewed the code in the kalshigo repository and have some feedback. Here's a breakdown of my observations, along with suggestions for improvement:
General Observations:
- Good Start: The project has a clear purpose: to provide low-level bindings to the Kalshi API. The structure is reasonable, with separate files for client setup, market interactions, request handling, data structures, and signature generation.
- Inspired by DiscordGo: The README explicitly mentions inspiration from
discordgo, which is a good approach. This likely means the library will aim for similar ease of use and idiomatic Go style.
- Basic Functionality Covered: The
market functionality seems to be implemented, allowing users to fetch series, markets, and lists of markets.
- Missing Features: The README clearly outlines that
exchange, auth, collection, portfolio, and sockets are not implemented yet. This is fine for an early-stage project, but users should be aware of the missing features.
- Tests: The project includes basic tests, which is great. These tests cover the happy path for fetching data. However, they are not comprehensive.
Specific Feedback and Suggestions:
-
Error Handling:
APIError: The APIError struct and its Error() method are a good start for handling API errors. However, you may want to include more information in the error, such as a timestamp or request ID if the API provides it.
- Inconsistent Error Handling: In some places, like the
GetMarkets function, you return an APIError with the err.Error() as the body if the initial request fails. This is not consistent with the error handling of other endpoints, where the response body is returned.
- Consider wrapping errors: Consider wrapping the low-level errors returned from the
http package with more context for easier debugging.
- Avoid panics in tests: The tests currently use
panic in TestMain. While this works, it's usually better practice to use t.Fatal to fail the test gracefully.
-
Client Initialization:
NewFromKeyPath: The NewFromKeyPath function is convenient, but consider adding an option to pass the key as a string for environments where reading from a file might not be ideal (e.g., CI/CD).
- Base URL: It's good that the base URL is configurable via the
New function.
-
Request Handling (requests.go):
- Timestamp: The timestamp generation seems correct, but it's vital to ensure that the server and client clocks are reasonably synchronized. Any deviation can cause signature errors. Consider adding a configurable tolerance for clock skew to the client.
- Code duplication: The request creation code has some duplication. This could be refactored into a helper function or method.
- Hardcoded Headers: The
Content-Type and Accept headers are hardcoded. While fine for now, consider making them configurable via a client config struct.
- HTTP Client: The http client is re-created in every request. It's recommended to create it once and re-use it for all requests.
-
Markets (markets.go):
- Path Construction: The use of
url.Parse and JoinPath is good for constructing URLs. This makes the code more robust when dealing with edge cases with paths.
- String Conversions: There are some
string() conversions for MarketStatus that are not needed. This could be removed for better readability.
- Param Handling: The
GetMarkets function is good at handling query params, but it could be more concise with the use of a loop to iterate over fields of GetMarketsParams.
- Response handling: The code currently uses the same logic to parse responses across all market endpoints, which is fine, but consider extracting this into a separate method or function to avoid duplication.
-
Structs (structs.go):
- MarketStatus enum: The use of a custom type
MarketStatus is good and allows for type-safe use of status values. Consider adding documentation for each const.
- API Structs: The structs are well-defined, matching the API response.
- Naming: It's worth noting that there are some inconsistencies in naming for fields coming from the API. For example, there's
subTitle and subtitle
- Comments: There is a comment
// TODO: Parse this for functional strike. This needs to be addressed.
-
Tests (markets_test.go, requests_test.go):
- Basic Validation: The tests do basic validation (e.g., checking the ticker). You should add more comprehensive tests, including edge cases, error scenarios, and parameter variations.
- Test Data: Tests are dependent on real data, which can make them fragile. Consider using a mocking framework like
httptest to simulate API responses for more stable tests.
- No Negative Testing: The tests don't cover the scenario where the API returns an error.
-
Signature Generation (sig.go):
- PEM Parsing: The PEM parsing is good.
- Error Handling: The error handling during PEM parsing is fine, but can be improved by returning a more descriptive error. For example, the
ErrPEMParse can be formatted to include details of the parsing error.
Recommendations for Improvement:
- Implement Missing Features: Prioritize implementing the missing features based on user needs.
- Comprehensive Tests: Add more comprehensive tests, using a mocking framework.
- Configuration: Create a configuration struct for the client, to allow customization of things like request headers, timeout, clock skew tolerance and more.
- Documentation: Add more comments and documentation for the library itself, and the structs.
- Refactor Duplicated Code: Refactor any duplicated code for more consistency.
- Logging: Consider adding logging using the Go's standard library's
log package or a dedicated logger like logrus or zap. This will help debug the application.
Summary:
The kalshigo library is a good start to providing Go bindings for the Kalshi API. However, there is room for improvement in areas like error handling, test coverage, code organization, and API feature coverage. By addressing the points above, you can create a more robust, reliable, and user-friendly library.