Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Refactored Request Functions for Improved Code Maintainability #584

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SahibYar
Copy link
Contributor

Key Optimizations:

  1. Extracted Common Header Logic:

    • The setHeaders function centralizes the logic for setting Content-Type and Accept headers, reducing repetition across functions.
  2. Extracted JSON Marshaling:

    • The marshalBodyJSON and marshalBodyJSONAPI functions handle the logic for marshaling data and setting the body in RequestOptions. This reduces redundant code in individual request functions.
  3. Simplified Functions:

    • The individual request functions (RequestJSON, RequestJSONAPI, RequestJSONAPIBulk) are now streamlined to focus on setting specific headers and calling the relevant helper functions. This enhances readability and maintainability.
  4. Handled RequestOptions Initialization in One Place:

    • The setHeaders function ensures that RequestOptions and its Headers map are properly initialized, eliminating duplicate initialization code.

This refactoring reduces redundancy, improves readability, and makes the codebase easier to maintain while preserving existing functionality.

###Testing
I ran make all with following output.

go-fastly % make all
==> Downloading Go module
==> Downloading development dependencies
==> Downloading https://formulae.brew.sh/api/formula.jws.json
==> Downloading https://formulae.brew.sh/api/cask.jws.json
Warning: semgrep 1.104.0 is already installed and up-to-date.
To reinstall 1.104.0, run:
  brew reinstall semgrep
==> Tidying module
==> Running gofmt
==> Fixing imports
==> Testing go-fastly
?       github.com/fastly/go-fastly/v9/fastly/domains   [no test files]
?       github.com/fastly/go-fastly/v9/fastly/products  [no test files]
?       github.com/fastly/go-fastly/v9/internal/test_utils      [no test files]
ok      github.com/fastly/go-fastly/v9/fastly   4.478s
ok      github.com/fastly/go-fastly/v9/fastly/domains/v1        1.089s
ok      github.com/fastly/go-fastly/v9/fastly/image_optimizer_default_settings  3.706s
ok      github.com/fastly/go-fastly/v9/fastly/products/bot_management   1.509s
ok      github.com/fastly/go-fastly/v9/fastly/products/brotli_compression       4.142s
ok      github.com/fastly/go-fastly/v9/fastly/products/ddos_protection  2.380s
ok      github.com/fastly/go-fastly/v9/fastly/products/domain_inspector 1.941s
ok      github.com/fastly/go-fastly/v9/fastly/products/fanout   3.277s
ok      github.com/fastly/go-fastly/v9/fastly/products/image_optimizer  4.995s
ok      github.com/fastly/go-fastly/v9/fastly/products/log_explorer_insights    4.556s
ok      github.com/fastly/go-fastly/v9/fastly/products/ngwaf    5.253s
ok      github.com/fastly/go-fastly/v9/fastly/products/origin_inspector 5.620s
ok      github.com/fastly/go-fastly/v9/fastly/products/websockets       4.963s
ok      github.com/fastly/go-fastly/v9/internal/productcore     5.037s
==> Running go vet
==> Running staticcheck
staticcheck 2023.1.3 (v0.4.3)
if command -v semgrep &> /dev/null; then semgrep ci --config auto --exclude-rule generic.secrets.security.detected-private-key.detected-private-key ; fi
                  
                  
┌────────────────┐
│ Debugging Info │
└────────────────┘
                  
  SCAN ENVIRONMENT
  versions    - semgrep 1.104.0 on python 3.13.1                       
  environment - running in environment git, triggering event is unknown
                                                                                                                        
  Scanning 1263 files (only git-tracked) with 1057 Code rules:
            
  CODE RULES
                                                                                                                        
  Language      Rules   Files          Origin      Rules                                                                
 ─────────────────────────────        ───────────────────                                                               
  <multilang>      47    1144          Community    1057                                                                
  yaml             31     980                                                                                           
  go               83     145                                                                                           
  bash              4       5                                                                                           
                                                                                                                        
                    
  SUPPLY CHAIN RULES
                  
  No rules to run.
                  
          
  PROGRESS
   
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00                                                                                                                        
                
                
┌──────────────┐
│ Scan Summary │
└──────────────┘
Some files were skipped or only partially analyzed.
  Scan was limited to files tracked by git.
  Partially scanned: 1 files only partially analyzed due to parsing or internal Semgrep errors
  Scan skipped: 1 files larger than 1.0 MB, 118 files matching .semgrepignore patterns
  For a full list of skipped files, run semgrep with the --verbose flag.

(need more rules? `semgrep login` for additional free Semgrep Registry rules)

CI scan completed successfully.
  Found 0 findings (0 blocking) from 1057 rules.
  No blocking findings so exiting with code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants